Unresolved external symbol error (LNK2019), after including headers

I know this has been asked probably a thousand times, but I've been biting my nails on this for days. I am new to C ++ and this is my first experience with Visual Studio.

I am trying to modify TORCS Racing Simulator for a competition at my university. Most of them are already done, so only parts of what I post is actually my code. But I added some new features to it and I'm having problems with them.

Everything worked fine on Linux with g ++, but when I try to compile it in Visual Studio 2005 I get an unresolved external symbols error.

I added a new project to a solution that uses features from other projects.

In this new project, I call the function:

getisDerbyDuel()

      

which is declared in raceinit.h

, which I included in a new project.

#ifndef _RACEINIT_H_
#define _RACEINIT_H_

#define RACE_ENG_CFG    "config/raceengine.xml"

extern void ReInit(void);
extern void ReShutdown(void);
extern void ReStartNewRace(void * /* dummy */);
extern void ReAddRacemanListButton(void *menuHandle);
extern int  ReInitCars(void);
extern int  ReInitTrack(void);
extern void ReRaceCleanup(void);
extern void ReRaceCleanDrivers(void);
extern char *ReGetCurrentRaceName(void);
extern char *ReGetPrevRaceName(void);
extern bool getisDerbyDuel(void);
extern void setisDerbyDuel(bool isDerbyDuel);

extern tModList *ReRaceModList;

#endif /* _RACEINIT_H_ */

      

and is defined in raceinit.cpp

:

 bool _isDerbyDuel = true;

void setisDerbyDuel(bool isDerbyDuel) {
    _isDerbyDuel = isDerbyDuel;
}
bool getisDerbyDuel(void) {
    return _isDerbyDuel;
}

      

raceinit.h

and raceinit.cpp

are in different projects of my client solution that compiles without errors. I also added client.lib

to dependencies in my project.

When compiling, I get the following output:

1>Generating Code... 
1>Compiling resources...
1>Linking...
1>   Creating library .\Release/championship2010server.lib and object      .\Release/championship2010server.exp
1>championship2010server.obj : error LNK2019: unresolved external symbol "bool __cdecl     getisDerbyDuel(void)" (?getisDerbyDuel@@YA_NXZ) referenced in function "void     __cdecl drive(int,struct CarElt *,struct Situation *)"     (?drive@@YAXHPAUCarElt@@PAUSituation@@@Z)
1>.\Release/championship2010server.dll : fatal error LNK1120: 1 unresolved externals
1>Build log was saved at "file://c:\Users\Administrator\Desktop\torcs-verylasttry    \src\drivers\championship2012server\Release\BuildLog.htm"
1>championship2010server - 2 error(s), 9 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========    

      

Any ideas appreciated, I don't know what to try more.


EDIT:

Thank you for your responses. I tried to change the external thing, so the flag itself is declared external in the header file and the getter is not (and a few combinations), but nothing will change the error output.

I'm not really sure what is meant by exporting functions. A quick Google search brought me to this:

[..]
extern void ReRaceCleanDrivers(void);
extern char *ReGetCurrentRaceName(void);
extern char *ReGetPrevRaceName(void);
#ifdef __cplusplus
extern "C" {  // only need to export C interface if
              // used by C++ source code
#endif
extern bool __declspec( dllimport ) getisDerbyDuel(void);
extern void __declspec( dllimport ) setisDerbyDuel(bool isDerbyDuel);

#ifdef __cplusplus
}
#endif
extern bool _isDerbyDuel;
[...]

      

and

extern "C" {
#include <raceinit.h>
}

      

What changed the error Output to this:

1>championship2010server.obj : error LNK2019: unresolved external symbol     __imp__getisDerbyDuel referenced in function "void __cdecl drive(int,struct CarElt *,struct     Situation *)" (?drive@@YAXHPAUCarElt@@PAUSituation@@@Z)
1>.\Release/championship2010server.dll : fatal error LNK1120: 1 unresolved externals

      

I just don't know what's wrong here.


EDIT TWO:

So, after reading on import / export of a function, I adjusted my code.
The header file where the unresolved function is declared looks like this:

#ifdef __cplusplus
extern "C" {  // only need to export C interface if
              // used by C++ source code
#endif
 __declspec( dllexport ) bool getisDerbyDuel(void);
 __declspec( dllexport ) void setisDerbyDuel(bool isDerbyDuel);

#ifdef __cplusplus
}
#endif
extern bool _isDerbyDuel;

      

The file where I call the function does not have a header file, but I tried to declare the imported function according to the other functions in the file:

[...]
static tTrack *curTrack;
static int RESTARTING[NBBOTS];

static void initTrack(int index, tTrack* track, void *carHandle,
        void **carParmHandle, tSituation *s);
static void newrace(int index, tCarElt* car, tSituation *s);
extern "C" void drive(int index, tCarElt* car, tSituation *s);
static void endrace(int index, tCarElt *car, tSituation *s);
static void shutdown(int index);
static int InitFuncPt(int index, void *pt);

__declspec( dllimport ) bool getisDerbyDuel(void);
[...]

      

The header is included like any other, and the function is called like this:

if (getisDerbyDuel()) {
[...]

      

Now the error output has changed to:

error LNK2019: unresolved external symbol _getisDerbyDuel referenced in function _drive

      

So, it seems to me that if I just solved the C / C ++ compatibility issue (or made it worse, I'm not even sure), but the linker issue remains the same.

Any ideas? I'm putting it off for now, but I need to get this to work or my boss will be mad :)

+3


source to share


2 answers


As I said, I'm new to Visual Studio, but this is still a little confusing. I found my mistake and it wasn't actually in my code.
In addition to linker input etc. I had to add a project containing raceinit.cpp to links in my new project. This solved my problem.



+6


source


Visual Studio uses a different method to include the path and link the lib.

Check your project settings:



  • right click your project and select "settings"
  • in the Configuration Properties section under the C / C ++ section that you have. added the paths to the headers you need.
  • Now check under "Linker" that you have added the path to your lib file under "Advanced" Library Directories'
  • Also check that you have added lib in 'Linker> Input> Additional Dependencies'

Hope it helps.

+3


source







All Articles