gpt4 book ai didi

c - 使用 minGW gcc 未定义对 _imp____glew* 函数的引用

转载 作者:可可西里 更新时间:2023-11-01 09:43:18 24 4
gpt4 key购买 nike

我正在尝试在 Win 7 x64 系统上使用 MinGW 编译一个相对简单的 OpenGL 程序,但我不断收到对几个 GLEW 函数的 undefined reference 。我已将库设置为链接到程序,并一直在四处寻找列表中可能缺少的任何库,但链接器的输出仍然如下所示:

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)

我尝试过在几种不同的配置中同时使用 -lglew32 和 -lglew32s,认为 glew32s 中的定义可能不在 glew32 中,但这没有帮助。关于我可能遗漏了什么或我忽略了什么的任何指导?

最佳答案

如果您使用静态链接库,您需要在#include "glew.h" 之前#define GLEW_STATIC。我会继续向您的 Makefile 添加一个规则来定义这个预处理器 token ,而不是实际将 #define ... 放在您的源代码中。

installation documentation 中提到了这一点顺便说一下,对于 GLEW。但是从这个问题被问到的次数来看,可能说的不够清楚。


更新:

定义此标记的原因是 Microsoft Windows 使用特殊的 __declspec (...) 进行 DLL 导入和导出。通过定义 GLEW_STATIC,您告诉链接器使用标准行为来定位 .lib 中的符号。

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


还值得一提的是,您不能使用 GLEW 官方网站上提供的预建动态链接 .lib 和 DLL 文件。它们是使用 MSVC 编译的;要在 MinGW 中使用通过 MSVC 编译的 DLL,请参阅 this link .更好的解决方案就是避免使用动态链接库而使用静态库。

关于c - 使用 minGW gcc 未定义对 _imp____glew* 函数的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18475234/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com