Problem linking in C++ and visual studio 2005 [solved]

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

Recommended Posts

Share on other sites
I believe the answer is that you should not "define" things in header files. You only want to 'declare'.

"Defining" a variable means you make a physical instance of it. "Declaring" something means you are simply describing what it is named and how it is used.

For example, if you do this in a header file:

#ifndef _HEADER_H#define _HEADER_Hint i;#endif

You get a separate copy of the "int i;" in every C++ file that gets compiled (because #include really is just copy-pasting the header into the C++ file at compile time).

The header guards only prevent a SINGLE C++ file from including a header more than once. Separate C++ files do not prevent each other from #including.

Now, replace the "int i;" with your global AudioClass and SoundFile variables, and this is what is happening.

Then, when the linker tries to put all of it together, it sees one separate variable in each of the C++ files, but they're all named the same thing and it doesn't know how to glue them all together! (the linker is very dumb.)

The easiest solution is to use "extern" to convert the definition into a declaration.

#ifndef _HEADER_H#define _HEADER_Hextern AudioClass *audioClass; // or whatever.#endif

This turns the line from a "define" into a "declare" and means that you don't get multiple physical copies of the variable.

Important: Once you turn something into an "extern", you need to put a physical define for it in ONE C++ file, otherwise you will get "unresolved external reference" errors from the linker.

So in your AudioClass.cpp file, you would have:

#include "GlobalVariables.h"AudioClass *audioClass;// and a bunch of other code for your class.

This way, all of your code that includes your header will know that SOMEWHERE, there will be a definition of the audioClass, but it's not known until linking time.

Then, when the linker glues everything together, you only have one AudioClass and everything that was #including the "extern" declaration will be fixed by the linker to use the ONE AudioClass you end up with.

Also: You might enjoy GraphViz - it can help you render nice diagrams like those much more quickly. You make a text file that describes your graph, and GraphViz calculates the layout and then renders it to an image file.

[Edited by - Nypyren on January 21, 2009 2:27:17 AM]

Share on other sites
oh, thank you very much. this seems like an easy solution and it did not require any change to my structure.
however in my mind i thought that the "one cpp" file i would define (physically) the global variables will be globalVariables.cpp
i'll define them like this in globalVariables.cpp :

AudioClass audioclass;
std::list<PlayingSound> playingSoundList;
and etc.

when i will get home from college, i will report if it worked.

it worked!
thanks again.

Share on other sites
Quote:
 Original post by itay390however in my mind i thought that the "one cpp" file i would define (physically) the global variables will be globalVariables.cpp

Yeah, that's perfectly fine. The linker doesn't care which C++ file you use for the definition - you are free to put those definitions anywhere that makes the most sense to you.

1. 1
2. 2
Rutin
21
3. 3
4. 4
frob
17
5. 5

• 9
• 33
• 13
• 13
• 10
• Forum Statistics

• Total Topics
632582
• Total Posts
3007208

×