Conditional compilation depending on 32-bit or 64-bit architecture

G

Gowtham

Hi, I am trying to write some code which acts differently when
compiled on
32 bit and 64 bit machines. To identify the machine type, I am trying
to find
the sizeof( int ) and comparing it with 32 and 64.

But, the compiler is complaining about syntax errors in the #if lines.

Errors:
32-64.cpp:7:13: missing binary operator before '('
32-64.cpp:12:13: missing binary operator before '('

g++ version: 3.2.3

sizeof() is evaluated at compile time, and hence I am not able to
understand
why this code is not working:

code:

#include <iostream>

using namespace std;

int main( int argc, char *argv[] )
{
#if ( sizeof( int ) == 32 )
cout << "32 bit" << endl;
return 0;
#endif

#if ( sizeof( int ) == 64 )
cout << "64 bit" << endl;
return 0;
#endif

cout << "Neither 32 bit nor 64 bit" << endl;
}

Can somebody please tell me what I have missed?

Thanks
Gowtham
 
G

Gowtham

Hi, I am trying to write some code which acts differently when
compiled on
32 bit and 64 bit machines. To identify the machine type, I am trying
to find
the sizeof( int ) and comparing it with 32 and 64.

But, the compiler is complaining about syntax errors in the #if lines.

Errors:
32-64.cpp:7:13: missing binary operator before '('
32-64.cpp:12:13: missing binary operator before '('

g++ version: 3.2.3

sizeof() is evaluated at compile time, and hence I am not able to
understand
why this code is not working:

code:

#include <iostream>

using namespace std;

int main( int argc, char *argv[] )
{
#if ( sizeof( int ) == 32 )
cout << "32 bit" << endl;
return 0;
#endif

#if ( sizeof( int ) == 64 )
cout << "64 bit" << endl;
return 0;
#endif

cout << "Neither 32 bit nor 64 bit" << endl;

}

Can somebody please tell me what I have missed?

Thanks
Gowtham

Oops, I should have compared sizeof(int) with 4 and 8.
What is the best way of achieving this?
 
M

Michael DOUBEZ

Sam a écrit :
Gowtham said:
Hi, I am trying to write some code which acts differently when
compiled on
32 bit and 64 bit machines. To identify the machine type, I am trying
to find
the sizeof( int ) and comparing it with 32 and 64.

But, the compiler is complaining about syntax errors in the #if lines.

Errors:
32-64.cpp:7:13: missing binary operator before '('
32-64.cpp:12:13: missing binary operator before '('

g++ version: 3.2.3

sizeof() is evaluated at compile time, and hence I am not able to
understand
why this code is not working: [snuip]
#if ( sizeof( int ) == 32 )
[snip]

Oops, I should have compared sizeof(int) with 4 and 8.
What is the best way of achieving this?

The sizeof() operator gets evaluated at compile time, not in the
preprocessor phase, as such it cannot be used in preprocessor directives.

Check the documentation for your compiler or C library. It's fairly
likely that there are some preprocessor macros #define-ed somewhere,
that give you the target platform's bitness.

For a more portable solution, you might look into the <climit> header.
Something like:

#if INT_MAX > ((int)(0x7FFFFFFF))
//>32 bits version - verify 64 bits
#if INT_MAX == ((int)(0x7FFFFFFFFFFFFFFF))
//64 bits
#define SIZEOF_INT 8
#else
//SIZEOF_INT == 0 means not 4 or 8
#define SIZEOF_INT 0
#elif INT_MAX == (int)(0x7FFFFFFF))
#define SIZEOF_INT 4
#else
//SIZEOF_INT == 0 means not 4 or 8
#define SIZEOF_INT 0
#endif

#if SIZEOF_INT == 8
cout << "64 bit" << endl;
#elif SIZEOF_INT == 4
cout << "32 bit" << endl;
#else
cout << "Neither 32 bit nor 64 bit" << endl;
#endif

If you want to make the decision at compile time, with the actual
sizeof(int) call, you could use templates.

template<int SZ_INT>
void foo()
{
cout << "Neither 32 bit nor 64 bit" << endl;
}

template<>
void foo<4>()
{
cout << "32 bit" << endl;
}

template<>
void foo<8>()
{
cout << "64 bit" << endl;
}


int main()
{
foo<sizeof(int)>();
return 0;
}


Michael
 
V

Vladislav.Lazarenko

Michael,

The version with templates is more elegant, robust and easy to
support.
 
G

Gowtham

Gowtham said:
Hi, I am trying to write some code which acts differently when
compiled on
32 bit and 64 bit machines. To identify the machine type, I am trying
to find
the sizeof( int ) and comparing it with 32 and 64.
But, the compiler is complaining about syntax errors in the #if lines.
Errors:
32-64.cpp:7:13: missing binary operator before '('
32-64.cpp:12:13: missing binary operator before '('
g++ version: 3.2.3
sizeof() is evaluated at compile time, and hence I am not able to
understand
why this code is not working:
code:
#include <iostream>
using namespace std;
int main( int argc, char *argv[] )
{
#if ( sizeof( int ) == 32 )
cout << "32 bit" << endl;
return 0;
#endif
#if ( sizeof( int ) == 64 )
cout << "64 bit" << endl;
return 0;
#endif
cout << "Neither 32 bit nor 64 bit" << endl;
}
Can somebody please tell me what I have missed?
Thanks
Gowtham
Oops, I should have compared sizeof(int) with 4 and 8.
What is the best way of achieving this?

The sizeof() operator gets evaluated at compile time, not in the
preprocessor phase, as such it cannot be used in preprocessor directives.

Check the documentation for your compiler or C library. It's fairly likely
that there are some preprocessor macros #define-ed somewhere, that give you
the target platform's bitness.

application_pgp-signature_part
1KDownload

Thanks. There is an identifier __WORDSIZE defined in gcc environment.
This will have
values 32 and 64.

I could handle this in my code. Thanks again.

Wondering if this is truly portable?
 
E

Erik Wikström

Hi, I am trying to write some code which acts differently when
compiled on
32 bit and 64 bit machines. To identify the machine type, I am trying
to find
the sizeof( int ) and comparing it with 32 and 64.

But, the compiler is complaining about syntax errors in the #if lines.

Errors:
32-64.cpp:7:13: missing binary operator before '('
32-64.cpp:12:13: missing binary operator before '('

g++ version: 3.2.3

sizeof() is evaluated at compile time, and hence I am not able to
understand
why this code is not working:

code:

#include <iostream>

using namespace std;

int main( int argc, char *argv[] )
{
#if ( sizeof( int ) == 32 )
cout << "32 bit" << endl;
return 0;
#endif

#if ( sizeof( int ) == 64 )
cout << "64 bit" << endl;
return 0;
#endif

cout << "Neither 32 bit nor 64 bit" << endl;
}

Can somebody please tell me what I have missed?

In addition to what others have said I would like to point out that
relying on sizeof(int) is doomed to fail. On many 64-bit platforms the
int is still 32-bits.
 
I

Ian Collins

Michael said:
int main()
{
foo<sizeof(int)>();
return 0;
}
sizeof(void*) would be a better choice. In the two most popular 64 bit
memory models (LP64 and LLP64), sizeof(int) is 4.
 
M

Michael DOUBEZ

Ian Collins a écrit :
sizeof(void*) would be a better choice. In the two most popular 64 bit
memory models (LP64 and LLP64), sizeof(int) is 4.

True.
Which might be a limit of the <climits>/INT_MAX solution.

IIRC 'long' is not guaranteed to be large enough to hold a pointer so
the template solution of the compiler defines are the only solution i
could think of.

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top