现在的位置: 首页 > 综合 > 正文

编译器、操作系统、CPU相关的预处理宏定义

2018年03月30日 ⁄ 综合 ⁄ 共 2585字 ⁄ 字号 评论关闭

疑问一、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 
#if/#endif to
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/

里面包含了更多详细的资料可下载。但资料比较旧,建议查阅各种编译器的官方文档。



抱歉!评论已关闭.