Uday
Uday

Reputation: 829

see previous definition of 'some symbol'

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

Upvotes: 1

Views: 2573

Answers (7)

MSalters
MSalters

Reputation: 179907

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.

Upvotes: 2

boguscoder
boguscoder

Reputation:

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

Upvotes: 2

Aamir
Aamir

Reputation: 15566

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).

Upvotes: 1

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)

Upvotes: 7

Jim Buck
Jim Buck

Reputation: 20726

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

Upvotes: 2

John Dibling
John Dibling

Reputation: 101456

Generally I just grep the header files.

Upvotes: 0

sharptooth
sharptooth

Reputation: 170489

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

Upvotes: 4

Related Questions