/FORCE: MULTIPLE

Started by
7 comments, last by moeron 18 years, 7 months ago
I have a namespace, in the namespace, i have about 7 global variable of my own classes. they're all in separate files and i include only the ones that need another class in the header of that one. but for some reason, it tells me about a symbol that's been defined alreayd and that i should use /FORCE: MULTIPLE to corret it. but now i get like 20 warrings that woulda been errors. is there anyway to get rid of that without having to use FORCE on it?
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
Advertisement
generally a multiply defined symbol means that you didn't wrap your headers in:

#ifndef <some unique string>
#define <some unique string>

//the body of your header here

#endif
If you define a variable in a header file, and that header is included in two separated cpp files, then the variable is defined twice. What you need in header file are variable declarations: learn to use the extern keyword.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
i am wrapping my headers like that. and i don't know.. i don't really trust externs... i remember i tried to use them last time and things got really messed up..

any other ideas? :-/
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
externs are the way to go.

you declare a variable in a header file, declared as having 'external' storage, the real variable is then defined in a source file (cpp).

Raymond Jacobs, Owner - Ethereal Darkness Interactive
www.EDIGames.com - EDIGamesCompany - @EDIGames

Quote:Original post by EvilKnuckles666
i am wrapping my headers like that. and i don't know.. i don't really trust externs... i remember i tried to use them last time and things got really messed up..


You don't "trust" externs? What the hell are you talking about? It's like saying "I don't really trust variables, any other ideas?"

In C++, you may not define variables in header files and expect your linker to be happy with it. It's not a question of you trusting externs or not, it's a question of you not understanding the C++ compilation model. Until you do, you will be plagued by this kind of problems. There is no "other" solution. Just learn how to use the language properly.

If you want to share a variable across cpp files, you must define that variable in one of the cpp files, and put an "extern" declaration in the header file that will enable other cpp files to use it.

foo.cppint i = 42;foo.hextern int i;bar.cpp#include <iostream>#include "foo.h"int main(){  std::cout << i << std::endl;}
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
thanks guys. oh and Fruny, thanks for your help, but no need to be a dick about it, seriously. just a tip for the future
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
Fruny's not a dick, he's French [grin]

Oh crap, did I say that out loud? Anyway, he's not being a dick, it's really very simple, 'not trusting externs' is jut a remarkably stupid thing to say, because externs are one of the features of the language. Whether or not you know how to use them, they are always going to work the same way.
Free speech for the living, dead men tell no tales,Your laughing finger will never point again...Omerta!Sing for me now!
if you don't trust externs, you could also declare the variable as static and that would solve your multiple definition problem. You might want to check a c++ reference book so you fully understand the difference between static and extern variables though, I'm not sure how you are using these global variables, so I cannot say for sure which one is the best for your situation.

// static example// this variable is ONLY defined in this header filestatic int foo;


// extern example// "foo.h"extern int foo;// "foo.cpp"int foo;
moe.ron

This topic is closed to new replies.

Advertisement