编译器、操作系统、CPU相关的预处理宏定义
2014-12-16 18:19
645 查看
疑问一、How to list compiler predefined macros?
编译器预处理宏定义用途:
参考: http://nadeausoftware.com/articles/2011/12/c_c_tip_how_list_compiler_predefined_macros
摘:All C/C++ compilers predefine macros indicating the target
processor, operating system, language features, compiler name and version, and more. Cross-platform code can use
wrap OS-specific
compiler-specific code (such as inline assembly), or processor-specific optimizations (such as SSE instructions on x86).
编译器的预处理宏定义使程序可以用于跨平台操作,包括跨编译器(典型:G++
VS VISUAL C++),跨操作系统(典型:linux vs mac vs windows),跨CPU(典型arm vs x86),也可使特定的代码用于特定的环境。算法开发需要考虑多种运行环境时可以采用这些预处理宏定义。
获得预处理宏定义方法:
1.采用命令行:
测试:在UBUNTU14.04LTS + GCC4.8.2+ intel cpu 环境下,使用命令 gcc -dM -E -x c /dev/null 可以得到一些有用的宏定义值,举例(各行内宏定义非常相似或关联):
#define __unix__ 1 #define
__unix 1 #define
unix 1 (UNIX系统)
#define __pentiumpro__ 1 (CPU TYPE)
#define __linux 1 #define
__linux__ 1 #define __gnu_linux__ 1 #define linux 1 (LINUX系统)
#define __GNUC__ 4 #define __GNUC_MINOR__ 8 (GNU GCC/G++)
#define __STDC__ 1 (编译器遵循ANSI C 则值赋为1 摘自《C和指针》)
#define __i686 1 #define __i686__ 1 #define
__i386 1 #define __i386__ 1 #define i386 1
(CPU TYPE)
2.查看源代码:
Clang and LLVM source code is available for free download from llvm.org. Once
downloaded, OS and processor macros are defined in
GCC and G++ source code is available for free download from gnu.org. Once
downloaded, OS and processor macros are defined in
3.类UNIX系统上使用strings命令: strings /usr/bin/gcc 里面可以获得预处理宏定义
测试:在UBUNTU14.04LTS + GCC4.8.2+ intel cpu 环境下,使用命令 strings /usr/bin/gcc 可以得到一些有用的宏定义值,举例:
__FILE__ __LINE__
__DATE__ __TIME__ __STDC__ 《C和指针》预处理器章节部分对这几个宏定义有详细介绍
疑问二、How
to detect the compiler name and version using compiler predefined macros?
参考这篇文章,里面相近介绍了如何利用宏定义实现跨编译器代码:http://nadeausoftware.com/articles/2012/10/c_c_tip_how_detect_compiler_name_and_version_using_compiler_predefined_macros
实际使用中跨的比较多的编译器可能是GNU GCC/G++ 和 MSVC++ ,这种情况下可用下来情况来跨编译(稍显不足):
#if defined(__GNUC__)
...
#endif
#if defined(_MSC_VER)
...
#endif
http://nadeausoftware.com/articles/2012/01/c_c_tip_how_use_compiler_predefined_macros_detect_operating_system
笔者使用最多的是LINUX操作系统和WINDOW操作系统 ,若涉及到操作系统的编程,用到的宏定义可以是unix 以及 WIN32.
疑问四、How to detect the processor type using compiler predefined macros?
参考这篇文章,里面相近介绍了各种CPU类型的宏定义:
http://nadeausoftware.com/articles/2012/02/c_c_tip_how_detect_processor_type_using_compiler_predefined_macros
如果针对CPU编写代码,可以利用这一部分宏定义。
疑问五、补充预处理编译器宏定义,用于源代码学习及跨平台代码开发
参考:
http://sourceforge.net/p/predef/wiki/Home/
里面包含了更多详细的资料可下载。但资料比较旧,建议查阅各种编译器的官方文档。
编译器预处理宏定义用途:
参考: http://nadeausoftware.com/articles/2011/12/c_c_tip_how_list_compiler_predefined_macros
摘:All C/C++ compilers predefine macros indicating the target
processor, operating system, language features, compiler name and version, and more. Cross-platform code can use
#if/#endifto
wrap OS-specific
#includes (such as
<Windows.h>vs.
<unistd.h>),
compiler-specific code (such as inline assembly), or processor-specific optimizations (such as SSE instructions on x86).
编译器的预处理宏定义使程序可以用于跨平台操作,包括跨编译器(典型:G++
VS VISUAL C++),跨操作系统(典型:linux vs mac vs windows),跨CPU(典型arm vs x86),也可使特定的代码用于特定的环境。算法开发需要考虑多种运行环境时可以采用这些预处理宏定义。
获得预处理宏定义方法:
1.采用命令行:
测试:在UBUNTU14.04LTS + GCC4.8.2+ intel cpu 环境下,使用命令 gcc -dM -E -x c /dev/null 可以得到一些有用的宏定义值,举例(各行内宏定义非常相似或关联):
#define __unix__ 1 #define
__unix 1 #define
unix 1 (UNIX系统)
#define __pentiumpro__ 1 (CPU TYPE)
#define __linux 1 #define
__linux__ 1 #define __gnu_linux__ 1 #define linux 1 (LINUX系统)
#define __GNUC__ 4 #define __GNUC_MINOR__ 8 (GNU GCC/G++)
#define __STDC__ 1 (编译器遵循ANSI C 则值赋为1 摘自《C和指针》)
#define __i686 1 #define __i686__ 1 #define
__i386 1 #define __i386__ 1 #define i386 1
(CPU TYPE)
2.查看源代码:
Clang and LLVM source code is available for free download from llvm.org. Once
downloaded, OS and processor macros are defined in
llvm/tools/clang/lib/Basic/Targets.cpp.
GCC and G++ source code is available for free download from gnu.org. Once
downloaded, OS and processor macros are defined in
gcc/config/*.
3.类UNIX系统上使用strings命令: strings /usr/bin/gcc 里面可以获得预处理宏定义
测试:在UBUNTU14.04LTS + GCC4.8.2+ intel cpu 环境下,使用命令 strings /usr/bin/gcc 可以得到一些有用的宏定义值,举例:
__FILE__ __LINE__
__DATE__ __TIME__ __STDC__ 《C和指针》预处理器章节部分对这几个宏定义有详细介绍
疑问二、How
to detect the compiler name and version using compiler predefined macros?
参考这篇文章,里面相近介绍了如何利用宏定义实现跨编译器代码:http://nadeausoftware.com/articles/2012/10/c_c_tip_how_detect_compiler_name_and_version_using_compiler_predefined_macros
实际使用中跨的比较多的编译器可能是GNU GCC/G++ 和 MSVC++ ,这种情况下可用下来情况来跨编译(稍显不足):
#if defined(__GNUC__)
...
#endif
#if defined(_MSC_VER)
...
#endif
疑问三、How to detect the operating system type using compiler predefined macros?
参考这篇文章,里面相近介绍了如何利用宏定义实现跨操作系统代码,包含各个编译器下在不同操作系统下针对操作系统而包含的预处理宏定义:http://nadeausoftware.com/articles/2012/01/c_c_tip_how_use_compiler_predefined_macros_detect_operating_system
笔者使用最多的是LINUX操作系统和WINDOW操作系统 ,若涉及到操作系统的编程,用到的宏定义可以是unix 以及 WIN32.
疑问四、How to detect the processor type using compiler predefined macros?
参考这篇文章,里面相近介绍了各种CPU类型的宏定义:
http://nadeausoftware.com/articles/2012/02/c_c_tip_how_detect_processor_type_using_compiler_predefined_macros
如果针对CPU编写代码,可以利用这一部分宏定义。
疑问五、补充预处理编译器宏定义,用于源代码学习及跨平台代码开发
参考:
http://sourceforge.net/p/predef/wiki/Home/
里面包含了更多详细的资料可下载。但资料比较旧,建议查阅各种编译器的官方文档。
相关文章推荐
- 32位与64位 CPU、编译器、操作系统与应用程序的区别
- 手机操作系统知识整理(一):CPU相关概念详解
- Cosmos的基石:IL2CPU编译器--C#开源操作系统学习系列三(转)
- 编译器,CPU和操作系统的宏
- CPU位数、操作系统位数和编译器位数关系
- SqlServer如何通过SQL语句获取处理器(CPU)、内存(Memory)、磁盘(Disk)以及操作系统相关信息
- CPU位数、操作系统位数和编译器位数关系
- 【C 语言】编译过程 分析 ( 预处理 | 编译 | 汇编 | 链接 | 宏定义 | 条件编译 | 编译器指示字 )
- CPU位数、操作系统位数和编译器位数关系
- 32位与64位 CPU、编译器、操作系统、应用程序、arm和X86关系
- Cosmos的基石:IL2CPU编译器--C#开源操作系统学习系列三
- 操作系统和编译器宏定义
- CPU位数、操作系统位数和编译器位数关系
- CPU位数、操作系统位数和编译器位数关系
- 操作系统,CPU,编译器,运行时库,系统调用的关系 之 编译器篇
- 编译器,CPU和操作系统的宏
- SqlServer如何通过SQL语句获取处理器(CPU)、内存(Memory)、磁盘(Disk)以及操作系统相关信息
- 宏定义妙用之: 让编译器(预处理)干尽可能多的事情
- Linux下查看cpuinfo相关信息注释
- linux查看硬件信息、cpu、内存、操作系统、发行版本、机器型号、网卡信息等