简体   繁体   English

如何在编译时测试当前版本的GCC?

[英]How to test the current version of GCC at compile time?

I would like to include a different file depending on the version of GCC. 我想根据GCC的版本包含不同的文件。 More precisely I want to write: 更准确地说,我想写:

#if GCC_VERSION >= 4.2
#  include <unordered_map>
#  define EXT std
#elif GCC_VERSION >= 4
#  include <tr1/unordered_map>
#  define EXT std
#else
#  include <ext/hash_map>
#  define unordered_map __gnu_cxx::hash_map
#  define EXT __gnu_cxx
#endif

I don't care about gcc before 3.2. 我不关心3.2之前的gcc。

I am pretty sure there is a variable defined at preprocessing time for that, I just can't find it again. 我很确定在预处理时间定义了一个变量,我再也找不到了。

There are a number of macros that should be defined for your needs: 应根据您的需要定义许多宏:

__GNUC__              // major
__GNUC_MINOR__        // minor
__GNUC_PATCHLEVEL__   // patch

The version format is major.minor.patch, eg 4.0.2 版本格式为major.minor.patch,例如4.0.2

The documentation for these can be found here . 这些文档可以在这里找到。

Ok, after more searches, it one possible way of doing it is using __GNUC_PREREQ defined in features.h . 好了,经过搜索,它一个这样做的可能的方式是使用__GNUC_PREREQ定义features.h

#ifdef __GNUC__
#  include <features.h>
#  if __GNUC_PREREQ(4,0)
//      If  gcc_version >= 4.0
#  elif __GNUC_PREREQ(3,2)
//       If gcc_version >= 3.2
#  else
//       Else
#  endif
#else
//    If not gcc
#endif

As a side note: 作为旁注:

To find all the predefined macros: 要查找所有预定义的宏:

  • Create empty file t.cpp 创建空文件t.cpp
  • g++ -E -dM t.cpp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM