开发者

How can I get the compiler tell me what file #define a value?

My code is linking against several other libraries that are also developed at my company, one of these libraries is redefining several values from errno.h, I would like to be able to fix this, however I am having trouble finding the exact file that is redefining these values, I am want to know if there is a way to make the compiler tell me when 开发者_Python百科a file has defined a particular value.


You can probably do it by adding -include errno.h to the command line that builds the library in question. Here's a quick example. I have a C program called "file.c":

#define ESRCH 8

That's it - then I compile with:

cc -c -include errno.h file.c

And presto, a compiler warning:

file.c:1:1: warning: "ESRCH" redefined
In file included from /usr/include/errno.h:23,
                 from <command-line>:0:
/usr/include/sys/errno.h:84:1: warning: this is the location of the previous definition

That will tell you where your bad definitions are.


Have you tried searching with grep?


If you don't want to search through all your headers for the particular #define, you could use

#undef YOUR_MANIFEST_CONSTANT

after each #include in your source module and then start removing them from the bottom up and see where your definitions come from.

Also, your compiler may tell you that a #define has been redefined. Turn all your warnings on.


With GCC I did something similar with:

g++ input.cc -dD -E > cpp.out

-dD tells cpp to print all defines where they were defined. And in the cpp output there are also markers for the include file names and the line numbers.


It is possible that some environments, I'm thinking IDE's here, have configuration options tied into the "project settings" rather than using a configuration header. If you work with a lot of other developers in a place where this behavior is NOT frowned on then you might also check your tool settings.

Most compilers will tell you where the problem is, you have to look and think about what the diagnostic notification is telling you. Short of that, grep/findstr on *nix/Windows is your friend. If that yields nothing then check for tool settings in your build system.


Some IDE's will jump to the correct location if you right click on the usage and select 'go to definition'.

Another option if you're really stuck is a command line option on the compiler. Most compilers have an option to output the assembler they generate when compiling C++ code. You can view this assembler (which has comments letting you know the relative line number in the C++ source file). You don't have to understand the assembler but you can see what value was used and what files and definitions were included when the compiler ran. Check your compiler's documentation for the exact option to use

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜