ThatSnail
ThatSnail

Reputation: 67

error LNK2001: unresolved external symbol for static lib that is loaded

So I've been stuck on this for almost a month now. I have an MSVC++ project with a library in the proper directory, and Diagnostic build verbosity on. The .LIB is statically compiled, and running:

dumpbin /headers GazeApiLib.lib

returns, among other things:

     Code
     COMDAT; sym= "public: bool __cdecl gtl::GazeApi::connect(bool)" (?connect@GazeApi@gtl@@QEAA_N_N@Z)
     16 byte align
     Execute Read

But the verbose build fails with the following error:

1>  C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\Tracker.exe /a /d C:\Windows\Microsoft.NET\Framework\v4.0.30319\FileTracker.dll /i "C:\Users\Snail\Documents\Visual Studio 2010\Projects\EyeTracker2\EyeTracker2\Release" /r "C:\USERS\SNAIL\DOCUMENTS\VISUAL STUDIO 2010\PROJECTS\EYETRACKER2\EYETRACKER2\GAZEAPILIB.LIB|C:\USERS\SNAIL\DOCUMENTS\VISUAL STUDIO 2010\PROJECTS\EYETRACKER2\EYETRACKER2\RELEASE\MAIN.OBJ" /b MSBuildConsole_CancelEventc512b4a37e0c4c399b5a3a0500474f44  /c "c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\link.exe"  /ERRORREPORT:PROMPT /OUT:"C:\Users\Snail\Documents\Visual Studio 2010\Projects\EyeTracker2\Release\EyeTracker2.exe" /NOLOGO /LIBPATH:"C:\Users\Snail\Documents\Visual Studio 2010\Projects\EyeTracker2\EyeTracker2\libs" GazeApiLib.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /MANIFEST /ManifestFile:"Release\EyeTracker2.exe.intermediate.manifest" /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /DEBUG /PDB:"C:\Users\Snail\Documents\Visual Studio 2010\Projects\EyeTracker2\Release\EyeTracker2.pdb" /OPT:REF /OPT:ICF /LTCG /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"C:\Users\Snail\Documents\Visual Studio 2010\Projects\EyeTracker2\Release\EyeTracker2.lib" /MACHINE:X86 Release\main.obj
1>  GazeApiLib.lib (TaskId:27)
1>main.obj : error LNK2001: unresolved external symbol "public: bool __thiscall gtl::GazeApi::connect(bool)" (?connect@GazeApi@gtl@@QAE_N_N@Z)

Marking it as __cdecl manually in the header file gives this error instead:

1>main.obj : error LNK2001: unresolved external symbol "public: bool __cdecl gtl::GazeApi::connect(bool)" (?connect@GazeApi@gtl@@QAA_N_N@Z)

So on one hand it looks like it's successfully including the lib file but it can't seem to find the function even though the prototypes match exactly. What's going on?

EDIT: Some more information, I ran undname and found that the unmangled name in the LIB file was:

public: bool __cdecl gtl::GazeApi::connect(bool) __ptr64

which is slightly different than my code's signature, and which would explain why the mangled names don't match (?connect@GazeApi@gtl@@QAE_N_N@Z vs. ?connect@GazeApi@gtl@@QAA_N_N@Z)

Upvotes: 1

Views: 2973

Answers (1)

I'm pretty sure it's a wordsize difference between the 2 compilations. Your lib uses 64-bit-sized pointers, while your project compiles to use 32-bit-sized. Note the E in the mangled name of the function in the lib, which means __ptr64 (?connect@GazeApi@gtl@@QEAA_N_N@Z). This gives, that the declaration of the function corresponding the one in the lib is

BOOL __cdecl gtl::GazeApi::connect(BOOL) __ptr64

I hope this helps

EDIT: For similar problems in the future: http://demangler.com/

Upvotes: 1

Related Questions