编译器中和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 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_VER
	printf("_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;
}

最后给出结果.

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来编译。

编译器中和64位编程有关的预定义宏

时间: 2024-08-11 21:24:34

编译器中和64位编程有关的预定义宏的相关文章

用VC进行64位编程

用VC进行64位编程 分类: C/C++2014-04-30 15:14 532人阅读 评论(0) 收藏 举报 本文转自:http://www.usidcbbs.com/read-htm-tid-5247.html   献给c/c++的同学.它包括创建一个64位安全的应用程序或者是从32位迁移到64系统的所有步骤.该介绍一共包括28课,涉及的内容有64位系统,64位应用程序的构建,如何找64位代码的问题和如何优化.第一课:64位的系统是什么 在写这个课程的时候,有2个流行的微处理器的64位架构:

五种常用的C/C++编译器对64位整型的支持

变量定义 输出方式 gcc(mingw32) g++(mingw32) gcc(linux i386) g++(linux i386) MicrosoftVisual C++ 6.0 long long "%lld" 错误 错误 正确 正确 无法编译 long long "%I64d" 正确 正确 错误 错误 无法编译 __int64 "%lld" 错误 错误 无法编译 无法编译 错误 __int64 "%I64d" 正确 正

预定义宏,C语言预定义的宏详解

1.预定义宏 对于预定义宏,相信大家并不陌生.为了方便处理一些有用的信息,预处理器定义了一些预处理标识符,也就是预定义宏.预定义宏的名称都是以"__"(两条下划线)开头和结尾的,如果宏名是由两个单词组成,那么中间以"_"(一条下划线)进行连接.并且,宏名称一般都由大写字符组成. 在日常项目编程中,预定义宏尤其对多目标平台代码的编写通常具有重大意义. 通过预定义宏,程序员使用"#ifdef"与"#endif"等预处理指令,就可使

iOS 64位编程

1.拒绝基本数据类型和隐式转换 1)基本数据类型 64位下,基本类型long从占用4字节变为占用8字节.要注意看一下程序中出现sizeof函数的地方,并注意尽量不要使用基本数据类型,改用对象类型: int -> NSInteger unsigned -> NSUInteger float -> CGFloat 动画时间 -> NSTimeInterval … 2)隐式转换 NSArray *items = @[<a href="http://www.jobbole.

关于标准C语言的预定义宏

标准C语言预处理要求定义某些对象宏,每个预定义宏的名称一两个下划线字符开头和结尾,这些预定义宏不能被取消定义(#undef)或由编程人员重新定义.下面预定义宏表,被我抄了下来.__LINE__  当前程序行的行号,表示为十进制整型常量__FILE__  当前源文件名,表示字符串型常量__DATE__转换的日历日期,表示为Mmm dd yyyy 形式的字符串常量,Mmm是由asctime产生的.__TIME__ 转换的时间,表示"hh:mm:ss"形式的字符串型常量,是有asctime产

C - 常用预定义宏

预定义宏 __DATE__    字符串, 进行预处理的日期("Mmm dd yyyy", 如May 27 2006) __TIME__     字符串, 源文件的编译时间("hh:mm:ss", 如09:11:10) __FILE__      字符串, 代表当前源代码文件名(包含详细路径, 如F:/a.c) __LINE__      整数值, 代表当前源代码文件中的行号 __STDC__    布尔值, 表示该实现严格遵循ANSIC标准 __STDC_VERS

C标准预定义宏,调试时加打印很有用

#include<stdio.h> int main(int argc, char *argv[]) { printf("File:[%s]\r\n", __FILE__); printf("Function:[%s]\r\n", __FUNCTION__); printf("LineNum:[%d]\r\n", __LINE__); printf("Data:[%s]\r\n", __DATE__); print

C++预定义宏

C/C++宏体中出现的#,#@,##: - #的功能是将其后面的宏参数进行字符串化操作(stringfication),就是对它所引用的宏变量通过替换后在其左右各加上一个双引号 -##被称为连接符(concatenator),用来将两个token连接为一个token.注意这里连接的对象是token就行,而不一定是宏的变量. 举例: Caffe中Layer的创建: #define REGISTER_LAYER_CREATOR(type, creator) \ static LayerRegiste

【转】win32,win64编程永恒;语言编程需要注意的64位和32机器的区别

原文网址:http://www.cnblogs.com/kex1n/archive/2010/10/06/1844737.html 一.数据类型特别是int相关的类型在不同位数机器的平台下长度不同.C99标准并不规定具体数据类型的长度大小,只规定级别.作下比较: 16位平台 char         1个字节8位 short        2个字节16位 int            2个字节16位 long         4个字节32位 指针         2个字节 32位平台 char