LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-06-2012, 12:35 PM   #1
Lsatenstein
Member
 
Registered: Jul 2005
Location: Montreal Canada
Distribution: Fedora 31and Tumbleweed) Gnome versions
Posts: 311
Blog Entries: 1

Rep: Reputation: 59
Determining if a system is 32bit or 64bit via C language.


I need to do something like this

If (sizeof(long) == 8)
typedef unsigned int uintxx;
else
typedef unsigned long uintxx;

The problem is that a typedef may not be part of a if/else expression.

Is there a defined compiler library constant that I may use for linux gcc compiler.

Is there a preprocessor variable I could test?


I found this, which does not work with gcc

#if (!(defined __LP64__ || defined __LLP64__)||( defined _WIN32 && !defined _WIN64 ))
// we are compiling for a 32-bit system
typedef unsigned long uintxx;
#else
// we are compiling for a 64-bit system
typedef unsigned int uintxx;
#endif
 
Old 09-06-2012, 05:32 PM   #2
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,281

Rep: Reputation: 320Reputation: 320Reputation: 320Reputation: 320
Quote:
The problem is that a typedef may not be part of a if/else expression.
Yes, because types must be fully defined during compilation, without that compiler doesn't known how many memory should reserve for variable, and "if" statement is executing when program is already compiled and running. Anyway, determining architecture by size of long type is not a reliable way, this size is dependend on compiler and can change in the future. Better method is using predefined macros, in gcc you have that
Code:
#ifdef __x86_64
  // 64bit
#elif defined __i386
  // 32bit
#else
  // Unknown
#endif
Also if you need an exactly bit length variable, you can use types created for that, there are int64_t, int32_t, int16_t or int8_t, with specified bit length - they are defined in <stdint.h>

Last edited by eSelix; 09-06-2012 at 05:33 PM.
 
1 members found this post helpful.
Old 09-06-2012, 06:23 PM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,649
Blog Entries: 4

Rep: Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934
The "#ifdef" approach is the only one that will actually work, because it determines at compile time which block of source-code will actually be submitted to the compiler.

However, "don't make work for yourself." Every language system out there has "platform independence" capabilities and there's a lot of very detailed documentation about both the relevant issues and their "don't ask why, just do this..." solutions. There's also a boat-load of existing open source software out there which shows you, for your particular environment whatever-it-is, exactly how it's correctly done.

"Actum Ne Agas ... Do Not Do A Thing Already Done."

Last edited by sundialsvcs; 09-06-2012 at 06:24 PM.
 
Old 09-07-2012, 07:36 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Note: For a start, what is 32 or 64 bit? Size of int? or that of long / long long / ptr?

Different possibilities are possible, eg:

short=int=ptr=16 long=32 (code: SIP16-L32 or simply L32, eg: MS-DOS 'small' modell)
short=int=16 long=ptr=32 (code: SI16-LP32 or simply LP32, eg: MS-DOS 'large' modell)
short=16, int=long=ptr=32 (code: S16-ILP32 or simply ILP32, eg: Unix32, Windows32 (older versions without any 64-bit type))
short=16, int=long=ptr=32, long long=64 (code: S16-ILP32-LL64 or simply LL64, eg: Unix32, Windows32)
short=16, int=32, long=long long=ptr=64 (code: S16-I32-LP64 or simply LP64, eg: Unix64)
short=16, int=long=32, long long=ptr=64 (code: S16-I32-LP64 or simply LLP64, eg: Windows64)

conclusion: if you want fixed-size integers, use intNN_t (uintNN_t) from stdint.h/inttypes.h, and if you want a 'pointer compatible' integer, use predefined types like 'ptrdiff_t', 'size_t', 'ssize_t', 'intptr_t', 'uintptr_t'
 
  


Reply

Tags
gcc, preprocessor variable, sizeof



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
upgrading a 32bit hardware system to 64bit frankkky Linux - Hardware 7 10-03-2010 11:12 AM
Determining 32bit vs 64bit o/s gammy Linux - Software 2 09-13-2007 03:11 PM
building 32bit app on 64bit system nori Linux - Software 1 07-20-2007 01:10 PM
Running LimeWire 32bit on a 64bit System? BiPolarPenguin Linux - Software 9 12-07-2006 12:07 PM
how do i know if system is 32bit or 64bit? greythorne SUSE / openSUSE 5 09-15-2005 08:56 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration