This article tests three compilers, namely VC, mingw, and GCC, in 32-bit and 64-bit modes. In six cases, the predefined macro values related to 64-bit programming are used. It is of reference significance for cross-platform programming.
Agner fog mentioned some reservation Macros in his calling conventions for different C ++ compilers and operating systems. The following is an excerpt.
Note: The following content is from calling conventions for different C ++ compilers and operating systems, last updated, by agner fog. copenhagen University College. the content of the original document shall prevail.
Most C ++ compilers have a predefined macro containing the version number of
Compiler. programmers can use preprocessing ctictives to check for the existence of these
Macros in order to detect which compiler the program is compiled on and thereby fix
Problems with incompatible compilers.
Table 23. compiler version predefined macros
Compiler |
Predefined macro |
Borland |
_ BorlandC __ |
Codeplay vectorc |
_ Vectorc __ |
Digital Mars |
_ DMC __ |
GNU |
_ Gnuc __ |
Intel |
_ Intel_compiler |
Microsoft |
_ Msc_ver |
Pathscale |
_ Pathscale __ |
Symantec |
_ Symantecc __ |
Watcom |
_ Watcomc __ |
Unfortunately, not all compilers have well-defined ented macros telling which hardware
Platform and operating system they are compiling for. The following macros may or may not
Be defined:
Table 24. hardware platform predefined macros
Platform |
Predefined macro |
X86 |
_ M_ix86 |
_ Intel __ |
_ I386 __ |
X86-64 |
_ M_x64 |
_ X86_64 __ |
_ Amd64 |
IA64 |
_ IA64 __ |
|
|
DEC Alpha |
_ Alpha __ |
|
|
Motorola Power PC |
_ PowerPC __ |
|
|
Any little endian |
_ Little_endian __ |
|
|
Any big endian |
_ Big_endian __ |
|
|
Table 25. Operating System predefined macros
Operating System |
Predefined macro |
DOS 16 Bit |
_ Msdos __ |
_ Msdos |
|
|
Windows 16 Bit |
_ Win16 |
|
|
|
Windows 32 bit |
_ Win32 |
_ WINDOWS __ |
|
|
Windows 64 bit |
_ Win64 |
_ Win32 |
|
|
Linux 32 bit |
_ UNIX __ |
_ Linux __ |
|
|
Linux 64 bit |
_ UNIX __ |
_ Linux __ |
_ Lp64 __ |
_ Amd64 |
BSD |
_ UNIX __ |
_ BSD __ |
_ FreeBSD __ |
|
Mac OS |
_ Apple __ |
_ Darwin __ |
_ Mach __ |
|
OS/2 |
_ Os2 __ |
|
|
|
The following code mainly tests macros related to 64 programming.
void test(){int len=sizeof(int)*8;printf("sizeof(int)=%d\n",len);len=sizeof(int *)*8;printf("sizeof(int*)=%d\n",len);#ifdef _MSC_VERprintf("_MSC_VER is defined\n");#endif#ifdef __GNUC__printf("__GNUC__ is defined\n");#endif#ifdef __INTEL__ printf("__INTEL__ is defined\n");#endif#ifdef __i386__ printf("__i386__ is defined\n");#endif#ifdef __x86_64__ printf("__x86_64__ is defined\n");#endif#ifdef _WIN32 printf("_WIN32 is defined\n");#endif#ifdef _WIN64 printf("_WIN64 is defined\n");#endif#ifdef __linux__ printf("__linux__ is defined\n");#endif#ifdef __LP64__ printf("__LP64__ is defined\n");#endif#ifdef __amd64 printf("__amd64 is defined\n");#endif}int main(int argc, char* argv[]){test();return 0;}
Finally, the result is given.
1. Compile in vc2010 and 32-Bit mode and output the following information.
Sizeof (INT) = 32
Sizeof (int *) = 32
_ Msc_ver is defined
_ Win32 is defined
2. Compile and output the following information under mingw.
Sizeof (INT) = 32
Sizeof (int *) = 32
_ Gnuc _ is defined
_ I386 _ is defined
_ Win32 is defined
3. In 64-bit fedora19, use gcc-M32 to compile and output the following information.
Sizeof (INT) = 32
Sizeof (int *) = 32
_ Gnuc _ is defined
_ I386 _ is defined
_ Linux _ is defined
4. Compile in vc2010 and 64-Bit mode and output the following information.
Sizeof (INT) = 32
Sizeof (int *) = 64
_ Msc_ver is defined
_ Win32 is defined
_ Win64 is defined
5. Compile and output the following information under mingw64
Sizeof (INT) = 32
Sizeof (int *) = 64
_ Gnuc _ is defined
_ X86_64 _ is defined
_ Win32 is defined
_ Win64 is defined
_ Amd64 is defined
6. Compile the 64-bit fedora19 with GCC-M64 and output the following information.
Sizeof (INT) = 32
Sizeof (int *) = 64
_ Gnuc _ is defined
_ X86_64 _ is defined
_ Linux _ is defined
_ Lp64 _ is defined
_ Amd64 is defined
Note: compile using the integrated environment directly in VC and use GCC for compilation in mingw and Linux.