• 10
• 13
• 19
• 27
• 9

# /FORCE: MULTIPLE

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

## Recommended Posts

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?

##### Share on other sites
generally a multiply defined symbol means that you didn't wrap your headers in:

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

#endif

##### Share on other sites
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.

##### Share on other sites
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? :-/

##### Share on other sites
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).

##### Share on other sites
Quote:
 Original post by EvilKnuckles666i 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;}

##### Share on other sites
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

##### Share on other sites
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.

##### Share on other sites
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;