GCC 转储预处理器定义
gcc/g++ 有没有办法从命令行转储其默认预处理器定义? 我的意思是诸如 __GNUC__
、__STDC__
等。
Is there a way for gcc/g++ to dump its default preprocessor defines from the command line?
I mean things like __GNUC__
, __STDC__
, and so on.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
是的,使用
-E
-dM
选项而不是 -c。示例(将它们输出到标准输出):
对于 C++
来自 GCC手册:
Yes, use
-E
-dM
options instead of -c.Example (outputs them to standard output):
For C++
From the GCC manual:
我通常这样做:
请注意,某些预处理器定义依赖于命令行选项 - 您可以通过将相关选项添加到上述命令行来测试这些选项。例如,要查看默认启用哪些 SSE3/SSE4 选项:
然后在指定
-msse4
时进行比较:同样,您可以查看两组不同的命令行选项之间哪些选项不同,例如比较预处理器定义优化级别
-O0
(无)和-O3
(完整):I usually do it this way:
Note that some preprocessor defines are dependent on command line options - you can test these by adding the relevant options to the above command line. For example, to see which SSE3/SSE4 options are enabled by default:
and then compare this when
-msse4
is specified:Similarly you can see which options differ between two different sets of command line options, e.g. compare preprocessor defines for optimisation levels
-O0
(none) and-O3
(full):迟到的答案 - 我发现其他答案很有用 - 并且想添加一点额外的内容。
如何转储来自特定头文件的预处理器宏?
或(感谢 @ mymedia 的建议):
特别是,我想了解 SOMAXCONN 在我的系统上的定义。我知道我可以打开标准头文件,但有时我必须四处搜索才能找到头文件位置。相反,我可以只使用这一行:
Late answer - I found the other answers useful - and wanted to add a bit extra.
How do I dump preprocessor macros coming from a particular header file?
or (thanks to @mymedia for the suggestion):
In particular, I wanted to see what SOMAXCONN was defined to on my system. I know I could just open up the standard header file, but sometimes I have to search around a bit to find the header file locations. Instead I can just use this one-liner:
简单的方法 (
gcc -dM -E - ) 对于 gcc 来说效果很好,但对于 g++ 却失败了。最近我需要测试 C++11/C++14 功能。相应宏名称的建议发布于 https://isocpp.org/std /stand-documents/sd-6-sg10-feature-test-recommendations。但是:
总是失败,因为它静默调用C驱动程序(就像由
gcc
调用)。您可以通过将其输出与 gcc 的输出进行比较或添加特定于 g++ 的命令行选项(例如 (-std=c++11))来看到这一点,该选项会发出错误消息cc1: warning: command line option '-std =c++11' 对于 C++/ObjC++ 有效,但对于 C 无效
。因为(非 C++)gcc 将永远支持“模板别名”(参见http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2258.pdf)您必须添加
-x c++< /code> 选项强制调用 C++ 编译器(使用
-x c++
选项而不是空虚拟文件的学分请参见 yuyichao,见下文):不会有输出,因为 g++ (修订版 4.9.1,默认为 -std=gnu++98)默认情况下不启用 C++11 功能。为此,使用
它最终会产生
注意到 g++ 4.9.1 在使用
-std=c++11
调用时确实支持“模板别名”。The simple approach (
gcc -dM -E - < /dev/null
) works fine for gcc but fails for g++. Recently I required a test for a C++11/C++14 feature. Recommendations for their corresponding macro names are published at https://isocpp.org/std/standing-documents/sd-6-sg10-feature-test-recommendations. But:always fails, because it silently invokes the C-drivers (as if invoked by
gcc
). You can see this by comparing its output against that of gcc or by adding a g++-specific command line option like (-std=c++11) which emits the error messagecc1: warning: command line option ‘-std=c++11’ is valid for C++/ObjC++ but not for C
.Because (the non C++) gcc will never support "Templates Aliases" (see http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2258.pdf) you must add the
-x c++
option to force the invocation of the C++ compiler (Credits for using the-x c++
options instead of an empty dummy file go to yuyichao, see below):There will be no output because g++ (revision 4.9.1, defaults to -std=gnu++98) does not enable C++11-features by default. To do so, use
which finally yields
noting that g++ 4.9.1 does support "Templates Aliases" when invoked with
-std=c++11
.一种在 Linux 或 Windows 上同样有效的便携式方法(其中没有任何 /dev/null ):
对于 C++,您可以使用(将
c++11
替换为您使用的任何版本):它的工作原理是告诉 GCC 预处理 标准输入(由 echo) 和 打印全部预处理器定义(搜索
-dletters
)。如果您想知道在包含头文件时添加哪些定义,可以使用-dD
选项,类似于 -dM,但它不包括预定义的宏:但是请注意,空输入仍然会产生大量带有
- 的定义dD 选项。
A portable approach that works equally well on Linux or Windows (where there isn't any /dev/null):
For C++, you may use (replace
c++11
with whatever version you use):It works by telling GCC to preprocess standard input (which is produced by echo) and print all preprocessor defines (search for
-dletters
). If you want to know what defines are added when you include a header file, you can use the-dD
option which is similar to -dM, but it does not include predefined macros:Note, however, that empty input still produces lots of defines with the
-dD
option.在处理具有复杂构建系统并且很难直接获取(或修改)gcc/g++ 命令的大型项目时,还有另一种方法可以查看宏扩展的结果。
只需重新定义宏,您将获得类似于以下内容的输出:
While working in a big project which has complex build system and where it is hard to get (or modify) the gcc/g++ command directly there is another way to see the result of macro expansion.
Simply redefine the macro, and you will get output similiar to following: