C++ linker error help

puffpio

Golden Member
Dec 21, 1999
1,664
0
0
I'm using this code someone else made

in sample.h there is a global variable boolean foobar

in sample.cpp there is code which accesses this global boolean

When I compile it is fine and dandy

when I build..in the link step I get:

error LNK2020: unresolved token (0A00036D) "bool foobar" (?foobar@@3_NA) sample.obj
error LNK2001: unresolved external symbol "bool foobar" ("foobar@@3_NA) sample.obj


Any ideas?
 

itachi

Senior member
Aug 17, 2004
390
0
0
post the code.. also, what arguments are you passing to the compiler?

edit: you are including sample.h in sample.cpp, right?
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
the variable also needs to appear in one of your cpp code files, not just the header. Look up "extern" in the index of your textbook.
 

puffpio

Golden Member
Dec 21, 1999
1,664
0
0
I think I found the error

Apparently .net managed c++ DLL's do not allow for global variables other than a basic int type. ssomething about them getting instantiated in dllmain before the CLR goodness kicks in

The possible solutions I've found pertain only to older versions of visual studio...

so i'm stuck..

I can't post the code cuz that would be proprietary to my company..
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Can you have a global/static class and access the variable when it is contained in the class.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Originally posted by: DaveSimmons
the variable also needs to appear in one of your cpp code files, not just the header. Look up "extern" in the index of your textbook.

I had this thing typed up, and then realized from the next message that he's using managed C++, which I don't have a lot of experience with (I do C# these days). But I'll post this anyway. Might help someone. In C/C++ (compiled, unmanaged) a variable declared as

bool fooBar;

Is both a declaration and a definition. The presence of that declaration in sample.h, which is included in sample.cpp, means that when sample.cpp is compiled the declaration is present in the translation unit, and doesn't require any additional definition to be accessible in that unit.

Since this variable isn't marked static, it has external linkage. If the project contains another module sample2.cpp that also includes sample.h, then a duplicate symbol error will occur at link time. This is why headers are, by convention, typically limited to declarations only (classes, structs, enums, typedefs). You usually don't want any definitions in them, because they are included in multiple places.

What is sometimes done is to declare a variable at module scope, say in sample.cpp, and then in another header have an extern declaration that simply introduces a declaration into scope within the current translation unit, without creating a definition (i.e. no storage). The linker will fix up the reference at link time, as long as it is defined non-statically in some other module that is being linked.

sample.cpp
bool fooBar;

anotherh.h
extern bool fooBar;

Needless to say, not a great practice :).

As an aside, the syntactic distinction between declarations and definitions is one of the hardest things for a new programmer to get right in C or C++, and is one of the biggest sources of link-time frustration. Their absence is also one of the things I like the most about C#.