unresolved external symbol __imp__... when I want to link a static library

Today I build an OpenGL tutorial project with GLEW in visual studio 14 (2015). I don't want to deal with "where to put the dlls" things. So I build the static library to use : $(SolutionDir)..\dependency\glew-2.0.0\lib\Debug\Win32\glew32sd.lib

But the linker error happens: ... unresolved external symbol __imp__glewInit@0 ... and lots of other "__imp__..." unresolved symbols (which are omitted here).

Why?

Let's check the symbols in glew32sd.lib:

Open the Developer Command Prompt and type in:dumpbin /symbols glew32sd.lib > a.txt (> a.txt means redirect the console output to the file a.txt)

Search the string "__imp__glewInit@0" in “a.txt” (nodepad++ is my first choice on windows). What do you get? Well, nothing. But I think it's exactly the reason why the linker complains.

I skimmed a.txt for a while and decided to search "glewinit". In the finding results, I noticed a different but similar symbol named "_glewInit@0" which is marked as "External" ( "External" here corresponds to non-static global functions/variables I think). You may have noticed that they both have a sub-string "glewInit@0", one with prefix "__imp__" and one with prefix "_".


So, strangely enough, why are they not the same?why did the compiler expect "__imp__glewInit@0" while the static library we built with the same tool chain offers "_glewInit@0" ?

If you see the "glewinit" function declaration in the glew.h, it is:

GLEWAPI GLenum GLEWAPIENTRY glewInit (void);

And "GLEWAPI" is defined as:

#ifdef GLEW_STATIC
#  define GLEWAPI extern
#else
#  ifdef GLEW_BUILD
#    define GLEWAPI extern __declspec(dllexport)
#  else
#    define GLEWAPI extern __declspec(dllimport)
#  endif
#endif


I have never defined GLEW_STATIC. And GLEWAPI is replaced with extern __declspec(dllimport).

I guess that's the reason why our compiler expects "__imp__glewInit@0". (__imp__ probably means dllimport) And I'm right:

After I add GLEW_STATIC to C/C++ -> Preprocessor -> Preprocessor Definitions, it get solved.

The same argument applies to freeglut.



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章