Solved

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

Posted on 2003-11-27
7
244 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

708 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

10 Experts available now in Live!

Get 1:1 Help Now