views:

111

answers:

6

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 a file has defined a particular value.

Thank you.

+3  A: 

Have you tried searching with grep?

Ben S
Trouble is that their might be logic that defines it if something else is defined somwehere else.
Martin Beckett
+5  A: 

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.

Carl Norum
This was close to what I did for a solution, I in the end just #defined the value that was in contention at the top of my .cpp file before I included any files that way the first file to define it was redef'ing it and the IDE would then complain about that one.
NSA
+2  A: 

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.

Bruce
A: 

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.

C Johnson
+2  A: 

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.

Dummy00001
A: 

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

Jay