Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2003-11-27
7
Medium Priority
?
254 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 500 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

824 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