编译器中和64位编程有关的预定义宏
本⽂对分别测试VC,MinGW,GCC 三种编译器,32位和64位模式,共6种情况下,和64位编程有关的与预定义宏的值。对跨平台编程具有参考意义。
Agner Fog 在他的《Calling conventions for different C++ compilers and operating systems》提到⼀些预订宏。这⾥摘录如下。
注:下⾯的内容来⾃《Calling conventions for different C++ compilers and operating systems》,Last updated 2012-02-29,作者:By Agner Fog. Copenhagen University College .如何原⽂内容不符,以原⽂为准。
Most C++ compilers have a predefined macro containing the version number of the
compiler. Programmers can use preprocessing directives 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-documented macros telling which hardware
platform and operating system they are compiling for. The following macros may or may notgnu编译器
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__
下⾯的代码主要测试和64编程有关的宏
[cpp]
1. void test()
2. {
3. int len=sizeof(int)*8;
4. printf("sizeof(int)=%d\n",len);
5.
6. len=sizeof(int *)*8;
7. printf("sizeof(int*)=%d\n",len);
8.
9. #ifdef _MSC_VER
10. printf("_MSC_VER is defined\n");
11. #endif
12.
13. #ifdef __GNUC__
14. printf("__GNUC__ is defined\n");
15. #endif
16.
17. #ifdef __INTEL__
18. printf("__INTEL__ is defined\n");
19. #endif
20.
21. #ifdef __i386__
22. printf("__i386__ is defined\n");
23. #endif
23. #endif
24.
25. #ifdef __x86_64__
26. printf("__x86_64__ is defined\n");
27. #endif
28.
29. #ifdef _WIN32
30. printf("_WIN32 is defined\n");
31. #endif
32.
33. #ifdef _WIN64
34. printf("_WIN64 is defined\n");
35. #endif
36.
37.
38. #ifdef __linux__
39. printf("__linux__ is defined\n");
40. #endif
41.
42. #ifdef __LP64__
43. printf("__LP64__ is defined\n");
44. #endif
45.
46.
47. #ifdef __amd64
48. printf("__amd64 is defined\n");
49. #endif
50. }
51.
52. int main(int argc, char* argv[])
53. {
54. test();
55. return 0;
56. }
最后给出结果.
1. 在VC2010, 32位模式下编译,输出下⾯的信息sizeof(int)=32
sizeof(int*)=32
_MSC_VER is defined
_WIN32 is defined
2. 在MinGW下编译 输出下⾯的信息
sizeof(int)=32
sizeof(int*)=32
__GNUC__ is defined
__i386__ is defined
_WIN32 is defined
3. 在64位Fedora19, 使⽤gcc -m32 编译,输出下⾯的信息
sizeof(int)=32
sizeof(int*)=32
__GNUC__ is defined
__i386__ is defined
__linux__ is defined
4. 在VC2010, 64位模式下编译,输出下⾯的信息
sizeof(int)=32
sizeof(int*)=64
_MSC_VER is defined
_WIN32 is defined
_WIN64 is defined
5. 在MinGW64下编译 输出下⾯的信息
sizeof(int)=32
sizeof(int*)=64
__GNUC__ is defined
__x86_64__ is defined
_WIN32 is defined
_WIN64 is defined
__amd64 is defined
6. 在64位Fedora19, 使⽤gcc -m64 编译,输出下⾯的信息
sizeof(int)=32
sizeof(int*)=64
__GNUC__ is defined
__x86_64__ is defined
__linux__ is defined
__LP64__ is defined
__amd64 is defined
注:在VC下直接⽤集成环境编译,在MinGW和Linux下直接使⽤ gcc来编译。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论