Solved

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

Posted on 2003-11-27
7
251 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

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!

Question has a verified solution.

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

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…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

623 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