473,614 Members | 2,361 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3635
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)

iD8DBQBIAzoHx9p 3GYHlUOIRAm/bAJ0acGaaKqPVVJ 2h3I7NE19xQyHhB QCeNhOJ
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/E9x9p3GYHlUOIRA koTAJkBhiUPnh5b lS6FXb5O4VrZVrb X0gCeJcMv
C/ZCempZyYrnBvEx6 8K6W6c=
=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
2457
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 difficult to understand. I've described it as GOTO's on steroids, and that's what it is!. One argument against abolishing it it that it is useful for conditional compilation when porting code, etc. Well, it seems to me C++ supports that...
2
4310
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 compilation is one possibility, but it's not practical. 1. You have to put a precompiler condition block around every Dim and every function declaration that might need to change, 2. If the code spans multiple modules, the flags have to be changed...
12
2480
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 main()
2
2583
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 want in there, I will not receive a compilation error). I do get such an error in F() - because of the garbage I intentionally put there - but F() should not compile in the first place. Am I misusing this attribute?! #undef DBG
4
2794
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. Feature1=true,Feature2=false, ... VS2005 seems to either allow the existance of the constant or not. ie. You must omit a constant not declare it to be false This means that the list no longer declares the state of all the options.
10
3084
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 have blocks of code in different source files that were conditionally compiled based on that constant. Now that C# has done away with include files, is there any way of doing the same thing, short of defining the constant multiple times at the head...
4
2083
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
2847
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 Python comments and is used by default to introduce #ifdef etc. lines. Here's an example of what I'm trying to do: #ifdef DEBUG stderr.write("variable is...") #details of msg omitted #endif
2
3109
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 day-fields is like WK (weekend), NH (National holiday), etc. Depending on the fieldvalue the field is colored using conditional
3
2336
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
8621
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8272
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8427
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7050
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5538
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4049
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2565
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1712
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1421
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.