0

我在源文件 (*.c) 中使用了一些宏。

在编译期间或从库中是否有任何方法可以识别出解析此特定宏的确切头文件?

问题是我们在某些头文件中使用了宏 #defined to 10 ,但在代码中接收到的值是 4 。因此,我们不想去检查所有的 dep 文件,而是想知道是否有一些直接的方法来识别宏被解析的来源。

4

3 回答 3

7

如果您只是在文件上运行 cpp(C 预处理器),输出将包含以下形式的 #line 指令

#line 45 "silly-file-with-macros.h"

让编译器说出一切的来源。所以一种方法是使用

 cpp my-file.c | more

并寻找#line指令。

根据您的编译器,您可以使用的另一个技巧是将宏重新定义为其他内容,编译器会发出警告,例如

test-eof.c:5:1: warning: "FRED" redefined
test-eof.c:3:1: warning: this is the location of the previous definition

(这是来自 gcc)它应该告诉你宏之前定义的位置。但是想一想,你怎么还没有收到那个警告呢?

另一个想法是用来makedepend获取所有包含文件的列表,然后用 grep 查找其中的#define行。

于 2009-10-21T14:06:05.300 回答
2

grep #define?

于 2009-10-21T14:06:25.030 回答
0
find / -name '*.h' | xargs -L 100 grep -H macroname

There are three commands there. The find command selects which files to be searched so you could change that to '.c' or '.cpp' or whatever you need. Then the xargs command, splits the list of files into 100 at a time so that you don't overflow some internal shell command buffer size. Then the grep command is run repeatedly with each list of 100 files and it prints any filenames containing macroname and the line of code that uses it.

From this you should be able to see where it is being redefined.

于 2009-10-21T14:08:12.490 回答