简体   繁体   中英

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?

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.

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. 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. Other characters are interpreted by the compiler proper, or reserved for future versions of GCC, and so are silently ignored. 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. 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 .

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. Both kinds of output go to the standard output file.

N
Like D, but emit only the macro names, not their expansions.

I
Output #include directives in addition to the result of preprocessing.

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

Why not consult the section on 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 :

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:

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 ...)的预定义宏

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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