#### Archived

This topic is now archived and is closed to further replies.

This topic is 5399 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am including the following header file and I get a linking error LNK2005 that the structure is already defined in several other .obj files. Specifically, it is complaining about: "class std::list > * allocList" being defined in several .obj files. Thanks for the help. //File: Debug.h #ifndef __DEBUGUTIL__ #define __DEBUGUTIL__ #include <windows.h> #include <list> #include <string> //Used for tracking memory leaks typedef struct { DWORD address; DWORD size; std::string file; DWORD line; } ALLOC_INFO; typedef std::list AllocList; AllocList *allocList; #endif

##### Share on other sites
#include <string>
using namespace std;

string strString;

##### Share on other sites
Every time your header file is #included, your allocList pointer (near the very bottom of your source) will be defined again. When you link several .obj files that have that pointer defined, you''ll get your multiply defined linker errors.

To fix this, take it out of the header file and move it into one of your .cpp files (any one will do, really, but you usually put it in the file where you''re using it the most). Then, in every other .cpp file that needs it, you create an extern declaration:
extern AllocList* allocList;

Be sure you have #included the header file before declaring allocList.

Cheers, dorix

##### Share on other sites
Ahhh....I see what you are saying and can now fix this.
Thank you very much for the help.

What is still throwing me is that I put an #ifndef at the top of the header so shouldnt this keep it from being compiled into multiple .obj files?

Thanks
Todd

##### Share on other sites
Yes, you are right. The header will be included once, and only once. No need for the extern stuff.

##### Share on other sites
quote:
Original post by Anonymous Poster
Yes, you are right. The header will be included once, and only once. No need for the extern stuff.

Whaaaaa?? No. The header will be included once, and only once, PER .CPP FILE COMPILED. But each .cpp file that includes it *will get it*, thus causing multiple definitions of the symbol.

The #ifndef/#define/#endif inclusion guard is to prevent the header file from being included more than once by the same .cpp file. It does nothing to prevent it from being included in more than one .cpp file; in fact, that would completely defeat the purpose of the header file altogether.

Consider this example of one .cpp file and two .h files:

  // one.h#include <two.h>

  // two.hint foo;

  // three.cpp#include <one.h>#include <two.h>

Do you see that without an inclusion guard, the contents of two.h will be included twice? There would be a compiler error here as foo is redefined.

Now, with inclusion guards in place, the contents of two.h will only be included into three.cpp once (when one.h is included). The resulting three.obj will have only one copy of the foo symbol.

But when four.cpp comes along and ends up including two.h (either indirectly through one.h or by including two.h itself), the resulting four.obj will also have its own copy of the foo symbol. The linker will then complain that the symbol is multiply defined, which is exactly what tscott1213''s original problem was.

Cheers, dorix

##### Share on other sites
Dorix,

Thank you!
I probably should have posted this in the beginner section given that my problem was essentially with understanding the basics of compiling.

This site rocks.

Todd

• ### Forum Statistics

• Total Topics
628667
• Total Posts
2984143

• 13
• 10
• 10
• 9
• 9