473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
Jun 27 '08 #1
8 3615
On Apr 14, 3:05 pm, Gowtham <gowthamgowt...@gmail.comwrote:
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?

Jun 27 '08 #2
Sam
Gowtham writes:
On Apr 14, 3:05 pm, Gowtham <gowthamgowt...@gmail.comwrote:
>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.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQBIAzoHx9p3GYHlUOIRAm/bAJ0acGaaKqPVVJ2h3I7NE19xQyHhBQCeNhOJ
XXz1z0Xur+/BE5TfD2pIo8E=
=stJZ
-----END PGP SIGNATURE-----

Jun 27 '08 #3
Michael,

The version with templates is more elegant, robust and easy to
support.
Jun 27 '08 #4
On Apr 14, 4:03 pm, Sam <s...@email-scan.comwrote:
Gowtham writes:
On Apr 14, 3:05 pm, Gowtham <gowthamgowt...@gmail.comwrote:
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?
Jun 27 '08 #5
On 2008-04-14 12:05, Gowtham wrote:
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.

--
Erik Wikström
Jun 27 '08 #6
Sam
Gowtham writes:
On Apr 14, 4:03 pm, Sam <s...@email-scan.comwrote:
>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?
No. If you need to code to work with other compilers, you'll have to
research those too. As someone else pointed out, checking INT_MAX would
probably be more portable.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQBIA/E9x9p3GYHlUOIRAkoTAJkBhiUPnh5blS6FXb5O4VrZVrbX0gCe JcMv
C/ZCempZyYrnBvEx68K6W6c=
=ulYV
-----END PGP SIGNATURE-----

Jun 27 '08 #7
Michael DOUBEZ wrote:
>
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.

--
Ian Collins.
Jun 27 '08 #8
Ian Collins wrote:
Michael DOUBEZ wrote:
>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.
.... which of course points out that the first question should be:

What do you mean by 64-bit?

(but I like your templates...)

Andy
Jun 27 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Steven T. Hatton | last post by:
I've made no secret of the fact that I really dislike the C preprocessor in C++. No aspect of the language has caused me more trouble. No aspect of the language has cause more code I've read to be...
2
by: Steve Jorgensen | last post by:
To begin with an example... Let's say you were wanting to write code usign early binding to the MSXML library, but then be able to switch between early and late binding at will. Conditional...
12
by: wanghz | last post by:
Hi, Could I ask some questions about the conditional compilaion? Suppose I have three simple files: a.c, b.c and c.h /* --------a.c--------- */ #include <stdio.h> #include "c.h" int...
2
by: FireStarter | last post by:
Guys, in the code that follows, why does the method F() still compile, even if DBG is undefined? Inside method G(), the code inside <#if DBG> does not compile (notice that I can write whatever I...
4
by: Bob | last post by:
Hi, In VS2003 conditional compilation constants and their state could be defined at project level. I was using this to control what features where offered by various builds. i.e....
10
by: Dave | last post by:
I'm a C++ programmer of many years, trying to get my feet wet in C#. I have a question about conditional compilation. In C++, I would sometimes define a constant in an include file, and then...
4
by: sam_cit | last post by:
Hi Everyone, I have a structure typedefed as typedef strcut { #if(MACRO == TRUE) int a; int b; #endif
6
by: maxwell | last post by:
I'm trying to use the gpp utility (Gnu points to http://en.nothingisreal.com/wiki/GPP) to do conditional compilation in Python, and I'm running into a problem: the same '#' character introduces...
2
by: Filips Benoit | last post by:
Dear All, Access 2003 adp on SQL_server 2005 A continious form showing 1 month based on table 'CALENDAR_MONTH_GRID' and fill with a SP. Fields: Companyname, Day1, day2, etc. The value in the...
3
by: =?Utf-8?B?U3VyZXNo?= | last post by:
Anyway to do the following in aspx HTML? Currently in my codebehind: #if SOMECONDITION using MYBL=MyNamespace.Biz #else using MYBL=MyNamespace.WebServiceBiz #endif
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.