如何知道(在 GCC 中)何时声明给定的宏/预处理器符号?

发布于 2024-07-09 20:55:08 字数 368 浏览 6 评论 0原文

假设我在各种头文件中有#define foo。 它可能会扩展到一些不同的事物。 我想知道(编译 .cc 文件时)何时遇到 #define、它将扩展什么、它是哪个文件以及它是从哪里包含的。

是否可以? 如果没有,是否有任何可能有帮助的部分解决方案?

请随意添加带有澄清请求的评论。

编辑:当前的答案似乎集中在有一个 #define 的情况,我只想跳转到定义或知道定义是什么。 这就是简单的情况,是的,您的解决方案有效。 但是,当我在不同的文件中有相同的#define,并且想知道哪个文件首先启动时,这些技术都没有用。 好吧,我实际上仔细地使用了#warning 来找到正确的地方。 但这需要做很多工作。

Suppose I have #define foo in various header files. It may expand to some different things. I would like to know (when compiling a .cc file) when a #define is encountered, to what it will expand, it which file it is and where it got included from.

Is it possible? If not, are there any partial solutions that may help?

Feel free to add comments with clarification requests.

Edit: current answers seem to concentrate on the case when there is one #define and I just want to jump to definition or know what the definition is. That's the simple case and yes, your solutions work. But when I have the same #define in different files, and want to know which one kicks in first, none of these techniques is useful. Okay, I actually used #warning carefully to find the right place. But this requires much work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

属性 2024-07-16 20:55:08

使用 -E :

# shows preprocessed source with cpp internals removed
g++ -E -P file.cc
# shows preprocessed source kept with macros and include directives 
g++ -E -dD -dI -P file.cc  

上面的内部结构是 gcc 的行标记,当您阅读输出时,它们有点令人困惑。 -P 删除它们

 -E  Stop after the preprocessing stage; do not run the compiler proper.  
     The output is in the form of preprocessed source code, which is sent to the 
     standard output.

     Input files which don't require preprocessing are ignored.

注意:评论正确地抱怨这只是部分解决方案。 它不会告诉您宏何时被替换。 它会向您显示经过预处理的源代码,无论如何这都会有所帮助。

Use -E :

# shows preprocessed source with cpp internals removed
g++ -E -P file.cc
# shows preprocessed source kept with macros and include directives 
g++ -E -dD -dI -P file.cc  

The internals above are line-markers for gcc which are kinda confusing when you read the output. -P strips them

 -E  Stop after the preprocessing stage; do not run the compiler proper.  
     The output is in the form of preprocessed source code, which is sent to the 
     standard output.

     Input files which don't require preprocessing are ignored.

Note: comments correctly complain this is only a partial solution. It won't tell you when a macro will be replaced. It shows you the preprocessed source, which can be helpful anyway.

吾家有女初长成 2024-07-16 20:55:08

我想知道(编译 .cc 文件时)何时遇到 #define,

我知道一个解决方案。 使用已定义为非法 C++ 代码的符号编译文件(链接到的文章使用“@”)。 因此,对于 GCC,您可以编写

gcc my_file.c -Dfoo=@

When that Expanded ,它肯定会导致语法错误,并且编译器应该告诉您该语法错误位于哪个文件中,这将非常有帮助。

如果您使用 Raymond Chen 建议的技巧,编译器可能会告诉您“冲突”定义的来源,并且可能会为您提供包含它的方式的列表。 但没有任何保证。 由于我不使用宏(我更喜欢 const 和 enum),我不能说 GCC 是否是这方面更智能的编译器之一。 我不相信 C 或 C++ 标准对此有任何说明,除非预处理器运行后您会丢失各种有用的信息。

I would like to know (when compiling a .cc file) when a #define is encountered,

I know a solution to that. Compile the file with the symbol already defined as illegal C++ code (the article linked to uses '@'). So, for GCC you would write

gcc my_file.c -Dfoo=@

When that expands it's guaranteed to cause a syntax error, and the compiler should tell you which file that syntax error is in, which will be very helpful.

If you use the trick Raymond Chen suggests, the compiler may tell you where the "conflicting" definition came from, and may give you a list of how it got included. But there's no guarantee. Since I don't use macros (I prefer const and enum) I can't say if GCC is one of the smarter compilers in this regard. I don't believe the C or C++ standards say anything about this, other than once the preprocessor runs you lose all sorts of useful information.

可是我不能没有你 2024-07-16 20:55:08

它不会帮助您找到它的定义位置,但您可以使用 -E -dM 标志查看特定文件的定义

g++ -E -dM file.cpp | grep MACRO

It wont help you find where it was defined but you can see the definition for a particular file by using the -E -dM flags

g++ -E -dM file.cpp | grep MACRO
还如梦归 2024-07-16 20:55:08

对于“它将扩展的内容”,我使用 gcc 中的 -E 开关,它给出了预处理的输出。 但是没有回溯哪个宏来自哪里(或者是否有宏)。

您可能使用的另一个选项是 -g3,这会添加有关宏的调试信息,即您稍后可以在调试器中看到每个宏的定义。

for the "to what it will expand" I use the -E switch in gcc which gives the preprocessed output. But there is no backtrace which macro came where from (or if there was a macro at all).

Another option you might use is -g3, this adds debug information regarding the macros, i.e. you can later see in your debugger the definition of each macro.

︶葆Ⅱㄣ 2024-07-16 20:55:08

一个好的 IDE 可以通过某种形式的跳转到定义来按需为您完成此操作。

A Good IDE can do this for you on demand via some form of jump to definition.

柏拉图鍀咏恒 2024-07-16 20:55:08

使用#警告。 此处对此进行了描述。

Use #warning. It's described here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文