博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
编译器、操作系统、CPU相关的预处理宏定义
阅读量:2394 次
发布时间:2019-05-10

本文共 2735 字,大约阅读时间需要 9 分钟。

疑问一、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  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  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/

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

你可能感兴趣的文章
Installing Kubernetes on Linux with kubeadm
查看>>
分布式存储系统GlusterFS初体验
查看>>
GlusterFS常用命令小结
查看>>
GlusterFS分布式文件系统使用简介
查看>>
Use Docker Engine plugins
查看>>
Using Gluster for a Distributed Docker Storage Volume
查看>>
有容云老司机带路, 使用Docker实现丝般顺滑的持续集成
查看>>
如何让Ubuntu系统支持WebP图片格式
查看>>
变态的静态资源缓存与更新(超详细好文)
查看>>
关于lvs均衡负载socket服务的配置实现
查看>>
Qt学习旅程(1)
查看>>
[转]CentOS 5.4挂载可读写NTFS
查看>>
SmartChineseAnalyzer的对中文开源社区是一大贡献
查看>>
[转]Apache Mahout 简介
查看>>
[转]分布式key-value存储方案介绍:Cassandra,LightCloud,TokyoCabinet
查看>>
[转]HDFS+MapReduce+Hive+HBase十分钟快速入门
查看>>
stdlib中的xmalloc,xfree,xinit_mempool
查看>>
关于Java Advanced Imaging(JAI)的一点积累
查看>>
Spirit越狱iPhone/iPod touch/iPad 3.1.3/3.2固件(Windows)
查看>>
[转]Adobe发布Puppet Recipes for Hadoop
查看>>