views:

132

answers:

7

while programming in c++, Most of the times i get 'some symbol' has already been defined see previous definition of 'some symbol'

I think this happens because improper order of headers file included.

How can i find out all the definitions of 'some symbol'

Thanks in advance, Uday

EDIT:

I am using visual studio I remember some command some thing like dumpbin /exports (I dont remember exactly) to get all the definition

+4  A: 

Full-text search is the most cross-platform cross-environment and efficient technique for that.

sharptooth
+1: Simple but true :-) Searching the preprocessor output of the .cpp that's failing, for example.
James Hopkin
A: 

Generally I just grep the header files.

John Dibling
+2  A: 

If you're using Visual Studio with Visual Assist installed, alt-G will pop up a list of all places where something is defined.

Jim Buck
+6  A: 

That usually happens due to a couple of common problems:

  • you are redefining the same symbol
  • you are including a header file many times and it does not have include guards
  • you are incorrectly forward declaring with an incompatible type and then including

I would start trying to fix the second. Make sure that all headers have include guards so that if you include them from different places it won't try to redefine the symbols:

#ifndef HEADER_FILE_NAME_GUARD // or something alike
#define HEADER_FILE_NAME_GUARD
// rest of file
#endif

If you need to order your includes then you will run into troubles later, make sure that you can and do include all your dependencies at all places. (Or forward declare if you can)

David Rodríguez - dribeas
BTW, if he is using Visual Studio Env. the it automatically creates header guards whenever you create a new header file with it's wizard. For him, this seems to be a problem of bad design.
Aamir
That relies on the fairly big assumption that he actually uses a wizard to generate his header files.
jalf
+1  A: 

If you need to order includes, it is an indication of 'Code Smell' in the first place. Most probably, your code is not designed well enough. All your common code should be part of a separate package/dll/lib(whatever) and should be included/linked from there.

If you are in C++/VC++, this normally happens when you include cpp & h files directly from other projects instead of linking to a utility library of some sort.

As somebody already pointed out, if you have to live with it, text search is the best option but try re-designing the code in a better way (if you can).

Aamir
+2  A: 

If you're using Visual Studio with Visual Assist installed, you'd better press Alt+X then D to see all the references to the symbol

+2  A: 

Translating C++ into an executbale has two steps. In step one, the compiler works linearly through the inputs (typically .cpp files and the headers they include). In step 2, the linker combines the results from step 1.

From your description of "symbol defined before", I conclude the problem must occur within a .cpp file, as those are processed linearly. dumpbin /exports works on the output of step 1, which you probably won't have. Header inclusion could be the culprit, as that's an early phase of compilation. You want the preprocessed input. IIRC, you can get that with the /EP switch.

MSalters