Solved

Combo C-C++ code with a linking error...

Posted on 2003-11-27
7
247 Views
Last Modified: 2010-04-01
I have a mixture of C and C++ in a very complex program... I know this is very little detail, but is anyone able to give me advice on how to remedy this linking error?

PlayerClient.obj : error LNK2001: unresolved external symbol "struct Player * Players" (?Players@@3PAUPlayer@@A)

Player is a structure I defined myself, and Players is the variable of type Player.  PlayerClient.cpp is what is attempting to use this extern function from another file.

Thanks,
Chris
0
Comment
Question by:cmreichl
7 Comments
 
LVL 13

Expert Comment

by:SteH
ID: 9832322
Where is the function defined? Is that library or obj file part of the project? Change options to view command to invoke linker. There you can check for the files.
0
 
LVL 86

Expert Comment

by:jkr
ID: 9832383
>>Player is a structure I defined myself, and Players is the variable of type Player

You are another victim of C++ name mangling. Declare the structure as

#ifdef __cplusplus
extern "C" {
#endif
struct Players {

//...
};
#ifdef __cplusplus
}
#endif
0
 
LVL 86

Expert Comment

by:jkr
ID: 9832390
Ooops, sorry

"Declare the structure as"

should read

"Declare the pointer as"
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 17

Expert Comment

by:rstaveley
ID: 9832601
Put this in your common header file:
--------8<--------
extern
#ifdef __cplusplus
        "C"
#endif
        struct Player * Players;
--------8<--------

Put this in your .c file:
--------8<--------
struct Player * Players;
--------8<--------

Take some time to look at: http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html
0
 
LVL 2

Author Comment

by:cmreichl
ID: 9834749
now it says...

PlayerClient.obj : error LNK2001: unresolved external symbol "struct Player * Players" (?Players@@3PAUPlayer@@A)
0
 
LVL 86

Expert Comment

by:jkr
ID: 9834795
That is pretty much the same error message. OK, so:

#ifdef __cplusplus
extern "C" {
#endif
#ifdef ALLOCATE_VARS
struct Player* Players = NULL;
#else
struct Player* Players = NULL;
#endif
#ifdef __cplusplus
}
#endif

And #define ALLOCATE_VARS in just ONE of your modules before including that header file.
0
 
LVL 17

Accepted Solution

by:
rstaveley earned 125 total points
ID: 9835518
jkr's suggestion needs to be either in a .cpp file or a .c file. It ensures that the variable has C linkage without the name mangling (i.e. has the public symbol _Players rather than a symbol mangled with all the weird and wonderful @ signs in it), regardless of which file it is put in.

You also need to ensure that the .cpp code attempting to access it does so assuming C linkage. That's where you need the common header file extern declaration, which doesn't have the = NULL bit. It tells both .c and .cpp code that in some module there is a symbol called _Players, without instantiating it. If your .cpp uses the extern declaration....

     extern Player* Players;

... it will expect to link to the mangled version. If, however, it uses...

     extern "C" Player* Players;

... which is equivalent to ...

extern
#ifdef __cplusplus
        "C"
#endif
        struct Player * Players;

... because the keyword struct is ignored in C++, it will expect to link to the version with C-linkage (i.e. _Players).
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now