Jump to content
  • Advertisement
Sign in to follow this  
NGEVA

LNK 2005 but already using extern....

This topic is 3410 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

rendering.h has the item in question leading_node.h includes rendering.h tester.h includes leading_node.h runmain.h includes rendering.h tester.h and leading_node.h are actual classes. i'm using VC++2005 it caused about 5-7 LNK2005 errors about the same item, then i read about extern and used it. now it is down to two errors, one with runmain.obj and another with tester.obj and is the same item in question.. runmain.cpp includes tester.h to utilize the functions in this test class because i haven't been able to get it working as a game engine should.. not sure how to make it link up through includes alone... but i may have to worry about that later

Share this post


Link to post
Share on other sites
Advertisement
If you declare something as extern, you have to be sure to give it a real definition in one (and only one) compilation unit (a.k.a. source file). For example:

// header.h
extern int myInteger;

// source.cpp, which may or may not #include header.h
int myInteger; // and here is the actual definition

// anotherSource.cpp, which does #include header.h
// since myInteger is defined in source.cpp, but we have an external declaration in the header file we included,
// we don't have to give the definition of myInteger in this source file; we can just use it
void doSomething()
{
    myInteger = 5;
}

Or was that not what you were asking...

Share this post


Link to post
Share on other sites
i knew i forgot something in my initial post.. ..

runmain doesn't use the variable but instead, the functions are called... actually i guess i should extern those in an attempt to resolve my variable/object redefinition error?...

tester also doesn't use the item but since it includes leading_node, leading_node object is utilized in the tester class... perhaps it is redefining but going into leading_node a second or more times and then to rendering where the original definition exists?..

i also have ifndef statements for all of my .h and i just added a set of ifndef statement to each of the corresponding .cpp

Share this post


Link to post
Share on other sites
Sometimes you'll see headers with something like this...

#ifdef SOMESYMBOL
#define EXTERN extern
#else
#define EXTERN
#endif

EXTERN the_variable;


Then, the "real" source file/compilation unit that belongs to that definition can define SOMESYMBOL before including the header.

EDIT: I somehow managed to screw this up in a major way... ignore this and see my next comment. :-)

[Edited by - smitty1276 on May 19, 2009 4:20:20 AM]

Share this post


Link to post
Share on other sites
what's the difference between the caps extern and the lower case extern? is it order/scope/level sort of thing?

actually.. i'm a bit confused in general with your example... what's being defined?...

Share this post


Link to post
Share on other sites
C/C++ is case sensitive; "extern" is extern--the C/C++ keyword--and "EXTERN" is just a defined symbol. As a rule, the thing after #define is being defined. [grin]

But I totally screwed up my example... sorry. I just put "the_variable" instead of "type name", which probably made things confusing in the first place. Plus, I accidentally flipped the conditions, so the whole thing was a mess. Let me try that again, and hopefully it will be clearer.
#ifdef SOMESYMBOL
#define EXTERN
#else
#define EXTERN extern
#endif

EXTERN int the_variable;
Depending on whether or not the conditional symbol SOMESYMBOL is defined, extern is either defined as "extern" or as nothing, giving you either...
extern int the_variable; // SOMESYMBOL not defined
or
int the_variable; //SOMESYMBOL is defined
At least one compilation unit that includes the header will need to define that conditional symbol. Like most things involving extern, it isn't pretty, but I've seen it quite a lot.

[Edited by - smitty1276 on May 19, 2009 4:47:59 AM]

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!