GCC 转储预处理器定义

发布于 2024-08-20 12:16:10 字数 91 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

烈酒灼喉 2024-08-27 12:16:10

是的,使用 -E -dM 选项而不是 -c
示例(将它们输出到标准输出):

 echo | gcc -dM -E -
 echo | clang -dM -E -

对于 C++

 echo | g++ -dM -E -x c++ -
 echo | clang++ -dM -E -x c++ -

来自 GCC手册

代替正常输出,生成
所有指令的“#define”列表
期间定义的宏
预处理器的执行,
包括预定义的宏。这
为您提供一种了解是什么的方法
在您的版本中预定义的
预处理器。假设你没有
文件foo.h,命令

touch foo.h; cpp -dM foo.h

将显示所有预定义的宏。

如果您使用 -dM 而不使用 -E 选项,
-dM 被解释为 -fdump-rtl-mach 的同义词。

Yes, use -E -dM options instead of -c.
Example (outputs them to standard output):

 echo | gcc -dM -E -
 echo | clang -dM -E -

For C++

 echo | g++ -dM -E -x c++ -
 echo | clang++ -dM -E -x c++ -

From the GCC manual:

Instead of the normal output, generate
a list of `#define' directives for all
the macros defined during the
execution of the preprocessor,
including predefined macros. This
gives you a way of finding out what is
predefined in your version of the
preprocessor. Assuming you have no
file foo.h, the command

touch foo.h; cpp -dM foo.h

will show all the predefined macros.

If you use -dM without the -E option,
-dM is interpreted as a synonym for -fdump-rtl-mach.

少女情怀诗 2024-08-27 12:16:10

我通常这样做:

$ gcc -dM -E - < /dev/null

请注意,某些预处理器定义依赖于命令行选项 - 您可以通过将相关选项添加到上述命令行来测试这些选项。例如,要查看默认启用哪些 SSE3/SSE4 选项:

$ gcc -dM -E - < /dev/null | grep SSE[34]
#define __SSE3__ 1
#define __SSSE3__ 1

然后在指定 -msse4 时进行比较:

$ gcc -dM -E -msse4 - < /dev/null | grep SSE[34]
#define __SSE3__ 1
#define __SSE4_1__ 1
#define __SSE4_2__ 1
#define __SSSE3__ 1

同样,您可以查看两组不同的命令行选项之间哪些选项不同,例如比较预处理器定义优化级别 -O0(无)和 -O3(完整):

$ gcc -dM -E -O0 - < /dev/null > /tmp/O0.txt
$ gcc -dM -E -O3 - < /dev/null > /tmp/O3.txt
$ sdiff -s /tmp/O0.txt /tmp/O3.txt 
#define __NO_INLINE__ 1        <
                               > #define __OPTIMIZE__ 1

I usually do it this way:

$ gcc -dM -E - < /dev/null

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:

$ gcc -dM -E - < /dev/null | grep SSE[34]
#define __SSE3__ 1
#define __SSSE3__ 1

and then compare this when -msse4 is specified:

$ gcc -dM -E -msse4 - < /dev/null | grep SSE[34]
#define __SSE3__ 1
#define __SSE4_1__ 1
#define __SSE4_2__ 1
#define __SSSE3__ 1

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):

$ gcc -dM -E -O0 - < /dev/null > /tmp/O0.txt
$ gcc -dM -E -O3 - < /dev/null > /tmp/O3.txt
$ sdiff -s /tmp/O0.txt /tmp/O3.txt 
#define __NO_INLINE__ 1        <
                               > #define __OPTIMIZE__ 1
記憶穿過時間隧道 2024-08-27 12:16:10

迟到的答案 - 我发现其他答案很有用 - 并且想添加一点额外的内容。


如何转储来自特定头文件的预处理器宏?

echo "#include <sys/socket.h>" | gcc -E -dM -

或(感谢 @ mymedia 的建议):

gcc -E -dM -include sys/socket.h - < /dev/null

特别是,我想了解 SOMAXCONN 在我的系统上的定义。我知道我可以打开标准头文件,但有时我必须四处搜索才能找到头文件位置。相反,我可以只使用这一行:

$ gcc -E -dM -include sys/socket.h - < /dev/null | grep SOMAXCONN
#define SOMAXCONN 128
$ 

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?

echo "#include <sys/socket.h>" | gcc -E -dM -

or (thanks to @mymedia for the suggestion):

gcc -E -dM -include sys/socket.h - < /dev/null

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 -E -dM -include sys/socket.h - < /dev/null | grep SOMAXCONN
#define SOMAXCONN 128
$ 
执妄 2024-08-27 12:16:10

简单的方法 (gcc -dM -E - ) 对于 gcc 来说效果很好,但对于 g++ 却失败了。最近我需要测试 C++11/C++14 功能。相应宏名称的建议发布于 https://isocpp.org/std /stand-documents/sd-6-sg10-feature-test-recommendations。但是:

g++ -dM -E - < /dev/null | fgrep __cpp_alias_templates

总是失败,因为它静默调用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++ -dM -E -x c++ /dev/null | fgrep __cpp_alias_templates

不会有输出,因为 g++ (修订版 4.9.1,默认为 -std=gnu++98)默认情况下不启用 C++11 功能。为此,使用

g++ -dM -E -x c++ -std=c++11 /dev/null | fgrep __cpp_alias_templates

它最终会产生

#define __cpp_alias_templates 200704

注意到 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:

g++ -dM -E - < /dev/null | fgrep __cpp_alias_templates

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 message cc1: 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):

g++ -dM -E -x c++ /dev/null | fgrep __cpp_alias_templates

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

g++ -dM -E -x c++ -std=c++11 /dev/null | fgrep __cpp_alias_templates

which finally yields

#define __cpp_alias_templates 200704

noting that g++ 4.9.1 does support "Templates Aliases" when invoked with -std=c++11.

小姐丶请自重 2024-08-27 12:16:10

一种在 Linux 或 Windows 上同样有效的便携式方法(其中没有任何 /dev/null ):

echo | gcc -dM -E -

对于 C++,您可以使用(将 c++11 替换为您使用的任何版本):

echo | gcc -x c++ -std=c++11 -dM -E -

它的工作原理是告诉 GCC 预处理 标准输入(由 echo) 和 打印全部预处理器定义(搜索-dletters)。如果您想知道在包含头文件时添加哪些定义,可以使用-dD 选项,类似于 -dM,但它不包括预定义的宏:

echo "#include <stdlib.h>" | gcc -x c++ -std=c++11 -dD -E -

但是请注意,空输入仍然会产生大量带有 - 的定义dD 选项。

A portable approach that works equally well on Linux or Windows (where there isn't any /dev/null):

echo | gcc -dM -E -

For C++, you may use (replace c++11 with whatever version you use):

echo | gcc -x c++ -std=c++11 -dM -E -

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:

echo "#include <stdlib.h>" | gcc -x c++ -std=c++11 -dD -E -

Note, however, that empty input still produces lots of defines with the -dD option.

苏辞 2024-08-27 12:16:10

在处理具有复杂构建系统并且很难直接获取(或修改)gcc/g++ 命令的大型项目时,还有另一种方法可以查看宏扩展的结果。
只需重新定义宏,您将获得类似于以下内容的输出:

file.h: note: this is the location of the previous definition
#define MACRO current_value

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:

file.h: note: this is the location of the previous definition
#define MACRO current_value
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文