【问题标题】:Flag to check whether the compiler is gcc/mingw or VC++ cl? [duplicate]检查编译器是 gcc/mingw 还是 VC++ cl 的标志? [复制]
【发布时间】:2011-04-13 01:06:25
【问题描述】:

可能重复:
Detect compiler with #ifdef

大家好,

我一直在做一个 C++ 项目,在 linux 上使用 gcc,在 windows 上使用 mingw。现在我想在 Windows 上使用 VC++ cl 编译器。 我是为了保持相同的源代码树只更改编译器特定的逻辑,例如:

#ifdef VC_CL_COMPILER
 //do vc++ related
#elif MINGW_FLAG
 //do mingw related
#elseif GCC_FLAG
  //do gc related    
#endif

有什么建议吗?

【问题讨论】:

  • 重新打开的原因:我认为 gcc 值得一个特殊情况,因为 __GNUC__ 标志也由其他编译器定义。见here

标签: c++ visual-c++ compiler-construction mingw


【解决方案1】:

编译器通常为此有一个预定义的宏。

#if defined(__GCC__)
  //do gcc related
#elif defined(_MSC_VER)
  //do msvc related
#else

#endif

【讨论】:

  • Gcc v4.1(可能还有其他)没有定义 __GCC__ 。应该测试的是__GNUC__
猜你喜欢
  • 1970-01-01
  • 2012-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多