未定义的引用与minGW gcc的功能。

时间:2023-01-27 19:40:15

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:

我正在尝试编译一个相对简单的OpenGL程序,在win7 x64系统上使用MinGW,并且我一直没有定义对几个GLEW函数的引用。我已经设置了库链接到程序,并且一直在寻找我列表中可能缺少的任何一个库,但是链接器的输出仍然是这样的:

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?

我尝试了几种不同配置的lglew32和-lglew32,认为在glew32中可能有不属于glew32的定义,这并没有帮助。对我可能遗漏的东西,或者我忽略的东西有什么指导吗?

1 个解决方案

#1


8  

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.

您需要在#include“glew”之前定义GLEW_STATIC。如果你使用的是静态链接库。我将继续向您的Makefile添加一条规则来定义这个预处理标记,而不是实际地将#define…在你的源代码。

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.

顺便说一下,这是在GLEW的安装文档中提到的。但是,从这个问题被问及的次数来看,它可能还不够清楚。


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.

定义这个标记的原因是,Microsoft Windows使用一个特殊的__declspec(…)用于DLL的导入和导出。通过定义GLEW_STATIC,您将告诉链接器使用标准行为来定位.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未定义时,它通知链接器,库的符号在运行时解析。但MSVC需要知道它是在创建导出,还是在寻找导入,因此还有另一个标记GLEW_BUILD来定义这种行为。由于您希望链接到(导入),而不是构建(导出)GLEW,请确保您不定义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.

还值得一提的是,您不能使用在官方的GLEW网站上提供的预构建的动态链接.lib和DLL文件。它们是用MSVC编译的;在MinGW中使用MSVC编译的DLL可以看到这个链接。更好的解决方案是避免使用动态链接库和使用静态库。

#1


8  

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.

您需要在#include“glew”之前定义GLEW_STATIC。如果你使用的是静态链接库。我将继续向您的Makefile添加一条规则来定义这个预处理标记,而不是实际地将#define…在你的源代码。

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.

顺便说一下,这是在GLEW的安装文档中提到的。但是,从这个问题被问及的次数来看,它可能还不够清楚。


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.

定义这个标记的原因是,Microsoft Windows使用一个特殊的__declspec(…)用于DLL的导入和导出。通过定义GLEW_STATIC,您将告诉链接器使用标准行为来定位.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未定义时,它通知链接器,库的符号在运行时解析。但MSVC需要知道它是在创建导出,还是在寻找导入,因此还有另一个标记GLEW_BUILD来定义这种行为。由于您希望链接到(导入),而不是构建(导出)GLEW,请确保您不定义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.

还值得一提的是,您不能使用在官方的GLEW网站上提供的预构建的动态链接.lib和DLL文件。它们是用MSVC编译的;在MinGW中使用MSVC编译的DLL可以看到这个链接。更好的解决方案是避免使用动态链接库和使用静态库。