简体   繁体   English

清单C常量/宏

[英]Listing C Constants/Macros

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? 有没有办法让GNU C预处理器 ,cpp( 或其他工具 )列出C文件中给定点的所有可用宏及其值?

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. 我正在寻找系统特定的宏,同时移植已经unix精通的程序并加载一堆稀疏的unix系统文件。

Just wondering if there's an easier way than going hunting for definitions. 只是想知道是否有一种比寻找定义更简单的方法。

I don't know about a certain spot in a file, but using: 我不知道文件中的某个位置,但使用:

$ touch emptyfile
$ cpp -dM emptyfile

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. 对包含一些#include#define行的C文件执行相同操作也包括所有这些。 I guess you could truncate your file to the spot you care about and then do the same? 我想你可以截断你的文件到你关心的地方然后做同样的事情?

From the man page : 手册页

-dCHARS
CHARS is a sequence of one or more of the following characters, and must not be preceded by a space. CHARS是由以下一个或多个字符组成的序列,不得以空格开头。 Other characters are interpreted by the compiler proper, or reserved for future versions of GCC, and so are silently ignored. 其他字符由编译器正确解释,或保留用于GCC的未来版本,因此会被忽略。 If you specify characters whose behavior conflicts, the result is undefined. 如果指定行为冲突的字符,则结果未定义。

M
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. 而不是正常输出,为执行预处理器期间定义的所有宏生成#define指令列表,包括预定义宏。 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 假设你没有文件foo.h,那么命令

  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 . 如果在没有-E选项的情况下使用-dM ,则-dM被解释为-fdump-rtl-mach的同义词。

D
Like M except in two respects: it does not include the predefined macros, and it outputs both the #define directives and the result of preprocessing. M一样,除了两个方面:它不包括预定义的宏,它输出#define指令和预处理的结果。 Both kinds of output go to the standard output file. 两种输出都转到标准输出文件。

N
Like D, but emit only the macro names, not their expansions. 像D一样,但只发出宏名称,而不是它们的扩展。

I
Output #include directives in addition to the result of preprocessing. 除了预处理的结果外,还输出#include指令。

使用gcc,您可以使用“-dD”选项将所有宏定义转储到stdout。

Why not consult the section on Predefined-macros ? 为什么不查阅Predefined-macros部分 Do you need this for building a project or some such thing? 你是否需要这个来建立一个项目或一些这样的东西?

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 : 要使用宏列出“它们在C文件中给定点的值”,有两个可以演示C文件中的给定点,特别是在编译时,并且可以被认为对跟踪故障点有用...考虑这个示例代码名为foo.c

if (!(ptr = malloc(20))){
    fprintf(stderr, "Whoops! Malloc Failed in %s at line %d\n", __FILE__, __LINE__);
}

If that code logic was used several times in this file, and the call to malloc was failing, you would get this output: 如果在该文件中多次使用该代码逻辑,并且对malloc的调用失败,您将获得此输出:

Whoops! Malloc Failed in foo.c at line 25

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... 此示例用于显示可以使用该宏的位置...

这是一个页面的链接 ,其中包含命令行选项的概述, 以列出大多数编译器( gccclang ...)的预定义宏

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM