Undefined References to _imp____glew* functions with minGW gcc

混江龙づ霸主 提交于 2019-12-19 04:16:21

问题


I am trying to compile a relatively simple OpenGL program using MinGW on a Win 7 x64 system, and I keep getting undefined references to several of the GLEW functions. I have set the libraries to link to the programs, and have been looking around for any library I may be missing from my list, yet the output from the linker still looks like this:

16:35:50 **** Incremental Build of configuration Debug for project test ****
Info: Internal Builder is used for build
gcc -LD:/DEV/openGL/lib/x86 -LD:/DEV/x86/lib -o test.exe test.o -lfreeglut -lglaux -lglew32s -lglu32 -lglfw3 -lopengl32 -lgdi32 
test.o: In function `init':
E:\Development\C\test\Debug/../test.c:32: undefined reference to `_imp____glewGenVertexArrays'
E:\Development\C\test\Debug/../test.c:33: undefined reference to `_imp____glewBindVertexArray'
E:\Development\C\test\Debug/../test.c:35: undefined reference to `_imp____glewGenBuffers'
E:\Development\C\test\Debug/../test.c:36: undefined reference to `_imp____glewBindBuffer'
E:\Development\C\test\Debug/../test.c:37: undefined reference to `_imp____glewBufferData'
test.o: In function `display':
E:\Development\C\test\Debug/../test.c:45: undefined reference to  `_imp____glewBindVertexArray'
test.o: In function `main':
E:\Development\C\test\Debug/../test.c:61: undefined reference to `_imp__glewInit@0'
collect2: ld returned 1 exit status

16:35:50 Build Finished (took 675ms)

I have tried with both -lglew32 and -lglew32s in several different configurations, thinking that perhaps there were definitions in glew32s that were not in glew32, and this did not help. Any guidance as to what I may be missing, or something I have overlooked?


回答1:


You need to #define GLEW_STATIC before #include "glew.h" if you are using the static linking library. I would go ahead and add a rule to your Makefile to define this pre-processor token, rather than actually putting the #define ... in your source code.

This is mentioned in the installation documentation for GLEW, by the way. But judging by the number of times this question is asked, it may not be stated clearly enough.


UPDATE:

The reason for defining this token is that Microsoft Windows uses a special __declspec (...) for DLL imports and exports. By defining GLEW_STATIC, you are telling the linker to use standard behavior to locate the symbols in your .lib.

When GLEW_STATIC is undefined, it informs the linker that the library's symbols are resolved at runtime. But MSVC needs to know whether it is creating exports or looking for imports and so there is another token GLEW_BUILD that defines this behavior. Since you want to link to (import), and not build (export) GLEW, make sure you do not define GLEW_BUILD.

/*
 * GLEW_STATIC is defined for static library.
 * GLEW_BUILD  is defined for building the DLL library.
 */

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


It is also worth mentioning that you cannot use the pre-built dynamic linking .lib and DLL files that are supplied on the official GLEW website. They are compiled using MSVC; to use a DLL compiled with MSVC in MinGW see this link. The better solution is just to avoid using the dynamic link library and use the static library.



来源:https://stackoverflow.com/questions/18475234/undefined-references-to-imp-glew-functions-with-mingw-gcc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!