在编译时检测 ICC 与 GCC
如何在编译时检测我使用的是 gcc 还是 icc?
(我很困惑地发现 icc 定义了 __GNUC__
——甚至 __GNUC_MINOR__
和 __GNUC_PATCHLEVEL__
!为什么?)
How to detect at compile time if I'm using gcc or icc?
(I was quite puzzled to find out that icc defines __GNUC__
-- and even __GNUC_MINOR__
and __GNUC_PATCHLEVEL__
! why?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我们使用
分离
icc
,假设gcc
作为默认值。We use
to split
icc
off, assuminggcc
as a default.我相信您可以根据this检查
__INTEL_COMPILER
。I believe you could check for
__INTEL_COMPILER
according to this.ICC 定义
__GNUC__
等的原因是因为像您这样的代码正在检查特定于编译器的宏并期望看到它们......The reason ICC defines
__GNUC__
etc. is because of code like yours that is inspecting compiler-specific macros and expects to see them...传统上,编译器将自己的符号及其版本定义为预处理器符号,以便可以调整代码(通常是为了解决错误或特殊性)。
CLang 以
__has_feature
查询的形式引入了一种我迄今为止从未见过的机制。它不会取代“解决错误”实践(这就是 CLang 仍然公开特定符号的原因),但允许使用更自然的样式来查询编译器功能。我不知道其他编译器是否计划定义这样的设施。Traditionally, compilers have defined a symbol of their own as well as their version as preprocessor symbols so that the code could be adapted (generally to work around bugs or specificities).
CLang has introduced a mechanism I had not seen so far, under the form of the
__has_feature
query. It does not replace the "work around bugs" practices (which is why CLang still exposes specific symbols) but allows a more natural style for querying the compiler capacities. I don't know if other compilers plan on defining such a facility.您可以使处理器输出预处理器输出中定义的宏,并寻找适合您的宏。您可以像这样生成预处理器输出:
然后查看
foo.P
(因为它是一个文本文件)。就我而言,我发现icc
使用编译器的版本定义了一个__ICC
宏。但它没有定义任何__INTEL_COMPILER
。You can make the processor output the defined macros in the preprocessor output and look for a macro that suits you. You can generated the preprocessor output like this:
Then look at
foo.P
(since it is a text file). In my case, I foundicc
defined an__ICC
macro with the version of the compiler. It didn't define any__INTEL_COMPILER
though.