列出 C 常量/宏
有没有办法让GNU C 预处理器、cpp(或其他一些工具)列出C<中给定点的所有可用宏及其值/strong> 文件?
我正在寻找特定于系统的宏,同时移植一个已经精通 UNIX 的程序并加载一堆稀疏的 UNIX 系统文件。
只是想知道是否有比寻找定义更简单的方法。
Is there a way to make the GNU C Preprocessor, cpp (or some other tool) list all available macros and their values at a given point in a C file?
I'm looking for system-specific macros while porting a program that's already unix savvy and loading a sparse bunch of unix system files.
Just wondering if there's an easier way than going hunting for definitions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不知道文件中的某个位置,但使用:
转储所有默认位置。对其中包含一些
#include
和#define
行的 C 文件执行相同的操作也包括所有这些内容。我想您可以将文件截断到您关心的位置,然后执行相同的操作?从 手册页:
I don't know about a certain spot in a file, but using:
Dumps all the default ones. Doing the same for a C file with some
#include
and#define
lines in it includes all those as well. I guess you could truncate your file to the spot you care about and then do the same?From the man page:
使用 gcc,您可以使用“-dD”选项将所有宏定义转储到标准输出。
With gcc, you can use the "-dD" option to dump all the macro definitions to stdout.
为什么不参考预定义宏部分?您需要这个来构建项目或类似的东西吗?
Why not consult the section on Predefined-macros? Do you need this for building a project or some such thing?
要使用宏列出“C 文件中给定点的值”,有两个宏可以演示 C 文件中的给定点,尤其是在编译时,并且被认为对于跟踪故障点很有用...考虑此示例代码位于名为 foo.c 的文件中:
如果在此文件中多次使用该代码逻辑,并且对
malloc
的调用失败,您将得到以下输出:行号会有所不同,具体取决于源中使用该逻辑的位置。该示例的目的是展示可以在哪里使用该宏......
To list "their values at a given point in a C file" using macros, there is two that can demonstrate a given point in a C file, especially when compiled, and would be deemed useful for tracing a point of failure...consider this sample code in a file called foo.c:
If that code logic was used several times in this file, and the call to
malloc
was failing, you would get this output:The line number would be different depending on where in the source, that logic is used. This sample serves the purpose in showing where that macro could be used...
这是链接,指向包含命令行概述的页面用于列出大多数编译器的预定义宏的选项(
gcc
、clang
...)。Here is a link to a page with an overview of command-line options to list predefined macros for most compilers (
gcc
,clang
...).