gpt4 book ai didi

c++ - 动态链接 GLEW 与 CMake

转载 作者:行者123 更新时间:2023-11-28 01:40:33 29 4
gpt4 key购买 nike

我查看了 Stack Overflow 和 Google 以试图解决这个问题,但我所看到的关于动态链接的所有内容要么不起作用,要么最终变成静态链接。

E:/CLion/GLEW-Test/main.cpp:8: undefined reference to `_imp__glewInit@0'
E:/CLion/GLEW-Test/main.cpp:12: undefined reference to `_imp__glewGetErrorString@4'
E:/CLion/GLEW-Test/main.cpp:14: undefined reference to `_imp__glewGetString@4'

我正在尝试使用 CLion 动态链接 GLEW。问题是,我是伴随着 Visual Studio 长大的,所以我从来没有真正需要考虑太多。我正在尝试自学 CMake 和 OpenGL,所以这将是一次完美的练习。

我下载了 GLEW 的源代码并使用 CMake 和 MinGW 构建了它。我放置了结果 glew32.dllC:/Windows/System32/ ,然后我放置了 libglew32.aC:/MinGW/lib ,然后我放置了 glew.h , glxew.h , 和 wglew.h进入C:/MinGW/include/GL .

目前,我还有库 glew32.dll , libglu32.a , 和 libopengl32.a在我的构建目录中。

这是我的 CMakeLists.txt:

cmake_minimum_required(VERSION 3.8)
project(GLEW_Test)

set(CMAKE_CXX_STANDARD 11)
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
set( CMAKE_VERBOSE_MAKEFILE on )

set(SOURCE_FILES main.cpp)
add_executable(GLEW_Test ${SOURCE_FILES})
target_link_libraries(GLEW_Test glew32 glu32 opengl32)

这是我的 main.cpp:

#include <iostream>
#include <GL/glew.h>

int main()
{
std::cout << "Hello, World!" << std::endl;

GLenum err = glewInit();
if (GLEW_OK != err)
{
/* Problem: glewInit failed, something is seriously wrong. */
fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
}
fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));

return 0;
}

现在,如果我 #define GLEW_STATIC在我之前#include <GL/glew.h> ,程序编译并输出预期的输出(没有 GLContext 的错误)。但是,据我所知,那是静态链接,我想动态链接 GLEW。这是我在没有 #define GLEW_STATIC 的情况下编译时看到的:

"C:\Program Files\JetBrains\CLion 2017.2.2\bin\cmake\bin\cmake.exe" --build E:\CLion\GLEW-Test\cmake-build-debug --target GLEW_Test -- -j 2
"C:\Program Files\JetBrains\CLion 2017.2.2\bin\cmake\bin\cmake.exe" -HE:\CLion\GLEW-Test -BE:\CLion\GLEW-Test\cmake-build-debug --check-build-system CMakeFiles\Makefile.cmake 0
C:/MinGW/bin/mingw32-make.exe -f CMakeFiles\Makefile2 GLEW_Test
mingw32-make.exe[1]: Entering directory 'E:/CLion/GLEW-Test/cmake-build-debug'
"C:\Program Files\JetBrains\CLion 2017.2.2\bin\cmake\bin\cmake.exe" -HE:\CLion\GLEW-Test -BE:\CLion\GLEW-Test\cmake-build-debug --check-build-system CMakeFiles\Makefile.cmake 0
"C:\Program Files\JetBrains\CLion 2017.2.2\bin\cmake\bin\cmake.exe" -E cmake_progress_start E:\CLion\GLEW-Test\cmake-build-debug\CMakeFiles 2
C:/MinGW/bin/mingw32-make.exe -f CMakeFiles\Makefile2 CMakeFiles/GLEW_Test.dir/all
mingw32-make.exe[2]: Entering directory 'E:/CLion/GLEW-Test/cmake-build-debug'
C:/MinGW/bin/mingw32-make.exe -f CMakeFiles\GLEW_Test.dir\build.make CMakeFiles/GLEW_Test.dir/depend
mingw32-make.exe[3]: Entering directory 'E:/CLion/GLEW-Test/cmake-build-debug'
"C:\Program Files\JetBrains\CLion 2017.2.2\bin\cmake\bin\cmake.exe" -E cmake_depends "MinGW Makefiles" E:\CLion\GLEW-Test E:\CLion\GLEW-Test E:\CLion\GLEW-Test\cmake-build-debug E:\CLion\GLEW-Test\cmake-build-debug E:\CLion\GLEW-Test\cmake-build-debug\CMakeFiles\GLEW_Test.dir\DependInfo.cmake --color=
mingw32-make.exe[3]: Leaving directory 'E:/CLion/GLEW-Test/cmake-build-debug'
C:/MinGW/bin/mingw32-make.exe -f CMakeFiles\GLEW_Test.dir\build.make CMakeFiles/GLEW_Test.dir/build
mingw32-make.exe[3]: Entering directory 'E:/CLion/GLEW-Test/cmake-build-debug'
[ 50%] Linking CXX executable GLEW_Test.exe
"C:\Program Files\JetBrains\CLion 2017.2.2\bin\cmake\bin\cmake.exe" -E cmake_link_script CMakeFiles\GLEW_Test.dir\link.txt --verbose=1
"C:\Program Files\JetBrains\CLion 2017.2.2\bin\cmake\bin\cmake.exe" -E remove -f CMakeFiles\GLEW_Test.dir/objects.a
C:\MinGW\bin\ar.exe cr CMakeFiles\GLEW_Test.dir/objects.a @CMakeFiles\GLEW_Test.dir\objects1.rsp
C:\MinGW\bin\g++.exe -Wall -Wextra -g -Wl,--whole-archive CMakeFiles\GLEW_Test.dir/objects.a -Wl,--no-whole-archive -o GLEW_Test.exe -Wl,--out-implib,libGLEW_Test.dll.a -Wl,--major-image-version,0,--minor-image-version,0 @CMakeFiles\GLEW_Test.dir\linklibs.rsp
CMakeFiles\GLEW_Test.dir/objects.a(main.cpp.obj): In function `main':
E:/CLion/GLEW-Test/main.cpp:8: undefined reference to `_imp__glewInit@0'
E:/CLion/GLEW-Test/main.cpp:12: undefined reference to `_imp__glewGetErrorString@4'
E:/CLion/GLEW-Test/main.cpp:14: undefined reference to `_imp__glewGetString@4'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [GLEW_Test.exe] Error 1
CMakeFiles\GLEW_Test.dir\build.make:98: recipe for target 'GLEW_Test.exe' failed
mingw32-make.exe[2]: *** [CMakeFiles/GLEW_Test.dir/all] Error 2
mingw32-make.exe[3]: Leaving directory 'E:/CLion/GLEW-Test/cmake-build-debug'
mingw32-make.exe[1]: *** [CMakeFiles/GLEW_Test.dir/rule] Error 2
CMakeFiles\Makefile2:69: recipe for target 'CMakeFiles/GLEW_Test.dir/all' failed
mingw32-make.exe[2]: Leaving directory 'E:/CLion/GLEW-Test/cmake-build-debug'
CMakeFiles\Makefile2:81: recipe for target 'CMakeFiles/GLEW_Test.dir/rule' failed
mingw32-make.exe[1]: Leaving directory 'E:/CLion/GLEW-Test/cmake-build-debug'
Makefile:120: recipe for target 'GLEW_Test' failed
mingw32-make.exe: *** [GLEW_Test] Error 2

我做错了什么?我遵循了 glew's sourceforge 的指南,并将 .dll 放在我的 System32 文件夹中。因为我是用 CMake 和 MinGW 构建的,所以我有 .a 文件而不是 .lib 文件,我把它们放在我的 MinGW/lib 目录中。我唯一没有做的事情(因为我不确定)就是不对我的 libglew32.dll.a 做任何事情这是罪魁祸首吗?我不确定如何处理它,谷歌搜索有关 .dll.a 文件的信息一无所获。

最佳答案

target_link_libraries(GLEW_Test libglew32.a libglu32.a libopengl32.a)

你不应该在你的 target_link_libraries 中包含“lib”前缀和库的文件扩展名。相反,你应该这样做

target_link_libraries(GLEW_Test glew32 glu32 opengl32)

因为 cmake 已经会搜索例如glew32.dll,libglew32.a 本身。这样,您还可以保持平台/编译器的独立性。

出于测试/bughunt 的目的,我建议您首先将所需的库放在可执行文件的目录中,然后检查错误是否仍然存在,因为 windows 链接器 searches the executable's directory first用于图书馆。

如果它不再发生但当库位于其他位置时会发生,请确保这些位置是 PATH 环境变量的一部分。

关于c++ - 动态链接 GLEW 与 CMake,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47321186/

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