Jump to content
  • Advertisement
Sign in to follow this  
BloodLust666

/FORCE: MULTIPLE

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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.cpp
int i = 42;

foo.h
extern int i;

bar.cpp
#include <iostream>
#include "foo.h"

int main()
{
std::cout << i << std::endl;
}

Share this post


Link to post
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 this post


Link to post
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 file

static int foo;



// extern example

// "foo.h"
extern int foo;


// "foo.cpp"
int foo;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!