Linking error LNK2001: unresolved external symbol (Visual Studio 2008)

Lólindir picture Lólindir · Apr 3, 2014 · Viewed 12.2k times · Source

I have 3 linking errors where I cannot get rid off.

I have defined some static public class constants of an own user-defined type in the header file of the class. Next I have declared and initialized the class constant in the cpp file. I can use these constants in the class itself. When I however try to use the constants in another project, I get a linking error:

error LNK2001: unresolved external symbol "public: static class EFString const CEFMldEreignis::s_typeMelderSteuern"

I have no idea what's wrong here. Someone an idea?

Many thanks in advance!

Header file:

class LIB_EXPORT CEFMldEreignis : public CBusinessObjekt
{
    ...

public:
    static const EFString s_typeMelderSteuern;

    ...
};

cpp file:

#include "EFMldEreignis.h"

const EFString CEFMldEreignis::s_typeMelderSteuern = _T("S");

We are using Visual Studio 2008. The project where the constants are defined is added as a reference in the common properties of the other project, as well as an additional include directory in the C++ properties.

In another, third project, we are actually declaring some static class constants as well, and these ones give no linking errors when using them.

Linker command line (all options):

(Remark: EuroAllgemeineMasken is the "other" project where the constants are used; EuroMelder is the project where the constants are defined and declared.)

/OUT:"C:\Eldis3\trunk\ELDIS\Workspace\..\Debug\EuroAllgemeineMasken.dll" /INCREMENTAL     
/NOLOGO /LIBPATH:"C:\Eldis3\trunk\ELDIS\Workspace\..\Debug" 
/LIBPATH:"C:\Eldis3\trunk\ELDIS\Workspace\\..\..\Lib\External\SST" 
/LIBPATH:"C:\Eldis3\trunk\ELDIS\Workspace\\..\..\Lib\External\ELDIS" /DLL /MANIFEST 
/MANIFESTFILE:"Debug\EuroAllgemeineMasken.dll.intermediate.manifest" 
/MANIFESTUAC:"level='asInvoker' uiAccess='false'" /DEBUG /
PDB:"C:\Eldis3\trunk\ELDIS\Workspace\..\Debug\EuroAllgemeineMasken.pdb" 
/SUBSYSTEM:WINDOWS /DYNAMICBASE:NO     
/IMPLIB:"C:\Eldis3\trunk\ELDIS\Workspace\..\Debug\EuroAllgemeineMasken.lib" /MACHINE:X86 
/ERRORREPORT:PROMPT ..\..\ELDIS\Debug\CEDVToolsEldis.lib ..\..\ELDIS\Debug\EuroTetra.lib 
..\..\ELDIS\Debug\Services.lib ..\..\ELDIS\Debug\ServiceManager.lib 
..\..\ELDIS\Debug\CESocketUDP.lib ..\..\ELDIS\Debug\EuroLWZ.lib 
..\..\ELDIS\Debug\EuroRouting.lib ..\..\ELDIS\Debug\MassnahmeTools.lib 
..\..\ELDIS\Debug\EuroDispo.lib ..\..\ELDIS\Debug\EuroMass.lib 
..\..\ELDIS\Debug\BCGControlBar.lib ..\..\ELDIS\Debug\EuroFlug.lib 
..\..\ELDIS\Debug\EuroMFCTools.lib ..\..\ELDIS\Debug\EuroLog.lib 
..\..\ELDIS\Debug\EuroGis.lib ..\..\ELDIS\Debug\EuroDialoge.lib 
..\..\ELDIS\Debug\CECrystalReport.lib ..\..\ELDIS\Debug\EuroInfo.lib 
..\..\ELDIS\Debug\EuroIO.lib ..\..\ELDIS\Debug\EuroPers.lib 
..\..\ELDIS\Debug\EuroStatus.lib ..\..\ELDIS\Debug\CELTextEldis.lib 
..\..\ELDIS\Debug\EuroBers.lib ..\..\ELDIS\Debug\EuroMelder.lib 
..\..\ELDIS\Debug\EuroLeit.lib ..\..\ELDIS\Debug\LOVDialog.lib 
..\..\ELDIS\Debug\EuroFunk.lib ..\..\ELDIS\Debug\EuroEsta.lib 
..\..\ELDIS\Debug\EuroOrt.lib ..\..\ELDIS\Debug\EuroEinsatz.lib 
..\..\ELDIS\Debug\Gms.lib ..\..\ELDIS\Debug\GMTs.lib ..\..\ELDIS\Debug\EuroAdmin.lib 
..\..\ELDIS\Debug\EuroOber.lib ..\..\ELDIS\Debug\EFBusiness.lib 
..\..\ELDIS\Debug\EuroTools.lib ..\..\ELDIS\Debug\DBAccess.lib 
..\..\ELDIS\Debug\DynamicWindows.lib

Answer

Lólindir picture Lólindir · Apr 8, 2014

There was a problem with the dllexport and dllimport statements. Some projects share the same LIB_EXPORT directive. This is defined as:

#if defined(_USRDLL) || (defined(_AFXDLL) && defined(_AFXEXT))
    #define LIB_EXPORT __declspec(dllexport)
#else
    #define LIB_EXPORT __declspec(dllimport)
#endif

This however doesn't seem to work correctly. We are now defining a separate directive for every project, like the one below:

#ifdef BUILD_EUROMELDER_DLL
    #undef  EUROMELDER_EXPORT
    #define EUROMELDER_EXPORT __declspec (dllexport)
#else
    #undef  EUROMELDER_EXPORT
    #define EUROMELDER_EXPORT __declspec (dllimport)
#endif

This solves the problem.