Solved

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

Posted on 2003-11-27
7
249 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 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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ get user from AD  (VS6) 7 93
GUI: DIalog Stacking and Popping in MS C++ 4 87
Arduino EDI - Programming language 3 114
Problem with SqlConnection 4 184
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

679 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