編譯器中和64位編程有關的預定義宏

來源:互聯網
上載者:User

標籤:style   blog   color   os   strong   io   

本文對分別測試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 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__

 

 

 

 

 

下面的代碼主要測試和64編程有關的宏

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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.