#ifdef for 32-bit platform

2019-02-04 06:54发布

In an application I maintain, we've encountered a problem with file descriptor limitations affecting the stdlib. This problem only affects the 32-bit version of the standard lib.

I have devised a fix for my code and would like to implement it, but only when compiling for 32-bit executable. What pre-processor symbol can I #ifdef for to determine whether the code is being compiled for a 32 or 64-bit target?

EDIT

Sorry, didn't mention, the code is cross-platform, linux, windows, solaris and a few other unix flavors, mostly using GCC for compilation. Any de-facto standards I can use cross-platform?

EDIT 2

I've found some definitions "__ILP23" and "__LP64" that seem like they may work... a discussion here explains the background on the unix platform. Anyone had any experience with using these defines? Is this going to be usable?

11条回答
Juvenile、少年°
2楼-- · 2019-02-04 07:20

There is no such symbol defined by the C++ standard - your specific platform (which you haven't specified in your question) may provide one.

查看更多
forever°为你锁心
3楼-- · 2019-02-04 07:26

I recommend bookmarking the predef SourceForge. There's no one answer, but it can certainly help you get started.

EDIT: For GCC-only code, you can use __i386__ to check for 32-bit x86 chips, and I suggest trying __X86_64__ or something similar to check for 64-bit x86 chips. (Note: It has come to my attention that the previous answer involving __ia86__ is actually a different chip, not a 64-bit x86 chip. This just shows my lack of hardware experience. For those more knowledgeable about hardware than I, consule the SourceForge page on predefined macros that I link to above. It's much more accurate than I am.) There are some other ones that would work, but those two should be fairly universal amongs GCC versions.

查看更多
一夜七次
4楼-- · 2019-02-04 07:30

I use a construction like this for Windows:

#if defined(_WIN64)
   //64-bit code
#elif  defined(_M_IX86)
   //32-bit code
#else
#error "Unknown platform"
#endif

Versus:

#if defined(_WIN64)
  // 64 bit code
#else
  // 32-bit code
#endif

In the former solution, because of the #error the compiler will be able to tell you where you need to add code for a new platform. This aids maintainability should you ever encounter a platform that is neither 64-bit nor 32-bit. Yes, the _M_IX86 is not exactly synonymous with 32-bit, but I think the only 32-bit platform most of us are supporting is in fact x86. So as a practical measure it suffices.

In the later solution you'll have to manually figure out where you need code for your new platform using grep or something like that. This is tedious and error prone.

It occurs to me that the following construction would also be acceptable, although I have not tested it in production nor I have really thought about it very much.

#if defined(_WIN64)
   //64-bit code
#elif  defined(_WIN32)
   //32-bit code
#else
#error "Unknown platform"
#endif
查看更多
兄弟一词,经得起流年.
5楼-- · 2019-02-04 07:31

Have a look at that:

i386 macros
AMD64 macros

查看更多
做自己的国王
6楼-- · 2019-02-04 07:31

You could check a well known type for it's size e.g. sizeof(int*) == 4 for a 32 bit platform.

As sizeof is known at compiletime I believe a

if(sizeof(int*) == 4)
{
  ...
}

should do the trick

Edit: the comments are right, you need to use a regular if, #if won't work.

If you are using C++ You could create templated code and let the compiler choose the specialization for you based on the sizeof() call. If you build for a 32 bit platform the compiler would only instantiate the code for the 32 bit platform. If you build for a 654 bit platform the compiler would only instantiate the code for the 64 bit platform.

查看更多
登录 后发表回答