如何检查是否在C中的目标文件所在的宏?(How to check if a macro exists

2019-06-23 12:49发布

For example, I define a macro:

#ifdef VERSION
 //.... do something
#endif

How can I check if VERSION exist in my object file or not? I tried to disassemble it with objdump, but found no actual value of my macro VERSION. VERSION is defined in Makefile.

Answer 1:

尝试使用编译-g3在选项gcc 。 它存储宏太的信息在生成的ELF文件。

在此之后,如果你定义一个宏MACRO_NAME只是grep它在输出可执行文件或目标文件。 例如,

$ grep MACRO_NAME a.out # any object file will do instead of a.out
Binary file a.out matches

或者你甚至可以尝试,

$ strings -a -n 1 a.out | grep MACRO_NAME

 -a Do not scan only the initialized and loaded sections of object files; 
    scan the whole files.

 -n min-len Print sequences of characters that are at least min-len characters long,
    instead of the default 4.


Answer 2:

的以下命令显示内容.debug_macro矮部分:

$ readelf --debug-dump=macro path/to/binary

要么

$ objdump --dwarf=macro path/to/binary

您还可以使用dwarfdump path/to/binary ,但它并不容易 ,只留下.debug_macro节中的输出。



文章来源: How to check if a macro exists in an object file in C?