提问人:Kirill 提问时间:7/9/2023 最后编辑:Kirill 更新时间:7/9/2023 访问量:176
GLFW库的链接错误(未定义引用)
linking error with GLFW library (undefined reference to)
问:
我正在尝试学习 OpenGL,但我有点坚持链接必要的库。注意:我使用 WSL,尝试使用 CMake 和 mingw-w64(编译器)为 Windows 构建可执行文件。i686-w64-mingw32-g++
这是我的文件夹结构:
build/
CMakeCache.txt
etc.
lib/
include/
GLFW/
glfw3.h
glfw3native.h
libglfw3.a
src/
main.cpp
.gitignore
CMakeLists.txt
我想编译该文件,并将其与 libglfw3.a
(来自目录的 64 位 Windows 预编译二进制文件)链接main.cpp
lib-mingw-w64
My 是 GLFW 网站的示例代码:https://www.glfw.org/documentation.htmlmain.cpp
#include <GLFW/glfw3.h>
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
CMakeLists.txt
:
cmake_minimum_required(VERSION 3.18.4)
project(opengl-triangle)
add_executable(triangle src/main.cpp)
target_include_directories(triangle PUBLIC lib/include)
find_library(
GLFW_LIB
NAMES glfw3
HINTS "${CMAKE_SOURCE_DIR}/lib"
NO_DEFAULT_PATH
)
if(NOT GLFW_LIB)
message(FATAL_ERROR "GLFW library not found")
endif()
target_link_libraries(
triangle
${GLFW_LIB}
)
当我尝试构建我的项目时,我得到这个:
$ make
-- Configuring done
-- Generating done
-- Build files have been written to: /mnt/d/Programming/Projects/opengl-test/build
Scanning dependencies of target triangle
[ 50%] Building CXX object CMakeFiles/triangle.dir/src/main.cpp.o
[100%] Linking CXX executable triangle
/usr/bin/i686-w64-mingw32-ld: CMakeFiles/triangle.dir/src/main.cpp.o:main.cpp:(.text+0x17): undefined reference to `glfwInit'
/usr/bin/i686-w64-mingw32-ld: CMakeFiles/triangle.dir/src/main.cpp.o:main.cpp:(.text+0x56): undefined reference to `glfwCreateWindow'
/usr/bin/i686-w64-mingw32-ld: CMakeFiles/triangle.dir/src/main.cpp.o:main.cpp:(.text+0x64): undefined reference to `glfwTerminate'
/usr/bin/i686-w64-mingw32-ld: CMakeFiles/triangle.dir/src/main.cpp.o:main.cpp:(.text+0x76): undefined reference to `glfwMakeContextCurrent'
/usr/bin/i686-w64-mingw32-ld: CMakeFiles/triangle.dir/src/main.cpp.o:main.cpp:(.text+0x81): undefined reference to `glfwWindowShouldClose'
/usr/bin/i686-w64-mingw32-ld: CMakeFiles/triangle.dir/src/main.cpp.o:main.cpp:(.text+0x96): undefined reference to `_imp__glClear@4'
/usr/bin/i686-w64-mingw32-ld: CMakeFiles/triangle.dir/src/main.cpp.o:main.cpp:(.text+0xa6): undefined reference to `glfwSwapBuffers'
/usr/bin/i686-w64-mingw32-ld: CMakeFiles/triangle.dir/src/main.cpp.o:main.cpp:(.text+0xab): undefined reference to `glfwPollEvents'
/usr/bin/i686-w64-mingw32-ld: CMakeFiles/triangle.dir/src/main.cpp.o:main.cpp:(.text+0xb2): undefined reference to `glfwTerminate'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/triangle.dir/build.make:104: triangle] Error 1
make[1]: *** [CMakeFiles/Makefile2:95: CMakeFiles/triangle.dir/all] Error 2
make: *** [Makefile:103: all] Error 2
如果我理解正确,这是一个链接错误。但是,我用 .为什么会有s?CMakeLists.txt
target_link_libraries
undefined reference
我尝试在没有 WSL 的情况下使用 MinGW 构建相同的内容,尝试将库与完整路径链接:。尝试了这个答案(包装在 .结果是一样的。target_link_libraries(triangle "/mnt/d/Programming/Projects/opengl-test/lib/libglfw3.a")
#include
extern "C"
我还尝试使用不同的编译器(),并更改了:cmake .. -D CMAKE_C_COMPILER=x86_64-w64-mingw32-gcc -D CMAKE_CXX_COMPILER=x86_64-w64-mingw32-g++
undefined reference
Scanning dependencies of target triangle
[ 50%] Building CXX object CMakeFiles/triangle.dir/src/main.cpp.o
[100%] Linking CXX executable triangle
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/triangle.dir/src/main.cpp.o:main.cpp:(.text+0x8a): undefined reference to `__imp_glClear'
/usr/bin/x86_64-w64-mingw32-ld: ../lib/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x10f): undefined reference to `__imp_CreateDCW'
/usr/bin/x86_64-w64-mingw32-ld: ../lib/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x14e): undefined reference to `__imp_GetDeviceCaps'
/usr/bin/x86_64-w64-mingw32-ld: ../lib/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x1a3): undefined reference to `__imp_DeleteDC'
/usr/bin/x86_64-w64-mingw32-ld: ../lib/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x2e3): undefined reference to `__imp_GetDeviceCaps'
/usr/bin/x86_64-w64-mingw32-ld: ../lib/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x8db): undefined reference to `__imp_GetDeviceCaps'
/usr/bin/x86_64-w64-mingw32-ld: ../lib/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0xd68): undefined reference to `__imp_CreateDCW'
/usr/bin/x86_64-w64-mingw32-ld: ../lib/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0xd77): undefined reference to `__imp_GetDeviceGammaRamp'
/usr/bin/x86_64-w64-mingw32-ld: ../lib/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0xd80): undefined reference to `__imp_DeleteDC'
/usr/bin/x86_64-w64-mingw32-ld: ../lib/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0xe78): undefined reference to `__imp_CreateDCW'
/usr/bin/x86_64-w64-mingw32-ld: ../lib/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0xe87): undefined reference to `__imp_SetDeviceGammaRamp'
/usr/bin/x86_64-w64-mingw32-ld: ../lib/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0xe90): undefined reference to `__imp_DeleteDC'
/usr/bin/x86_64-w64-mingw32-ld: ../lib/libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0x1e5): undefined reference to `__imp_CreateDIBSection'
/usr/bin/x86_64-w64-mingw32-ld: ../lib/libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0x21f): undefined reference to `__imp_CreateBitmap'
/usr/bin/x86_64-w64-mingw32-ld: ../lib/libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0x2c8): undefined reference to `__imp_DeleteObject'
/usr/bin/x86_64-w64-mingw32-ld: ../lib/libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0x350): undefined reference to `__imp_DeleteObject'
/usr/bin/x86_64-w64-mingw32-ld: ../lib/libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0x3e4): undefined reference to `__imp_CreateRectRgn'
/usr/bin/x86_64-w64-mingw32-ld: ../lib/libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0x41b): undefined reference to `__imp_DeleteObject'
/usr/bin/x86_64-w64-mingw32-ld: ../lib/libglfw3.a(wgl_context.c.obj):wgl_context.c:(.text+0x14a): undefined reference to `__imp_SwapBuffers'
/usr/bin/x86_64-w64-mingw32-ld: ../lib/libglfw3.a(wgl_context.c.obj):wgl_context.c:(.text+0x3f6): undefined reference to `__imp_ChoosePixelFormat'
/usr/bin/x86_64-w64-mingw32-ld: ../lib/libglfw3.a(wgl_context.c.obj):wgl_context.c:(.text+0x404): undefined reference to `__imp_SetPixelFormat'
/usr/bin/x86_64-w64-mingw32-ld: ../lib/libglfw3.a(wgl_context.c.obj):wgl_context.c:(.text+0xb2f): undefined reference to `__imp_DescribePixelFormat'
/usr/bin/x86_64-w64-mingw32-ld: ../lib/libglfw3.a(wgl_context.c.obj):wgl_context.c:(.text+0x108f): undefined reference to `__imp_DescribePixelFormat'
/usr/bin/x86_64-w64-mingw32-ld: ../lib/libglfw3.a(wgl_context.c.obj):wgl_context.c:(.text+0x10aa): undefined reference to `__imp_SetPixelFormat'
/usr/bin/x86_64-w64-mingw32-ld: ../lib/libglfw3.a(wgl_context.c.obj):wgl_context.c:(.text+0x1386): undefined reference to `__imp_DescribePixelFormat'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/triangle.dir/build.make:104: triangle] Error 1
make[1]: *** [CMakeFiles/Makefile2:95: CMakeFiles/triangle.dir/all] Error 2
make: *** [Makefile:103: all] Error 2
答:
那么,解决方案是链接另外两个库(opengl32 和
gdi32
)并使用编译器。x86_64-w64-mingw32-g++
工作 :CMakeLists.txt
cmake_minimum_required(VERSION 3.18.4)
project(opengl-triangle)
add_executable(triangle src/main.cpp)
target_include_directories(triangle PUBLIC lib/include)
find_library(
GLFW_LIB
NAMES glfw3
HINTS "${CMAKE_SOURCE_DIR}/lib"
NO_DEFAULT_PATH
)
if(NOT GLFW_LIB)
message(FATAL_ERROR "GLFW library not found")
endif()
target_link_libraries(
triangle
${GLFW_LIB}
opengl32
gdi32
)
评论
${GLFW_LIB}
message(STATUS "GLFW_LIB: ${GLFW_LIB}")
-- GLFW_LIB: /mnt/d/Programming/Projects/opengl-test/lib/libglfw3.a
make VERBOSE=1
[100%] Linking CXX executable triangle /usr/bin/cmake -E cmake_link_script CMakeFiles/triangle.dir/link.txt --verbose=1 /usr/bin/i686-w64-mingw32-g++ CMakeFiles/triangle.dir/src/main.cpp.o -o triangle ../lib/libglfw3.a /usr/bin/i686-w64-mingw32-ld: CMakeFiles/triangle.dir/src/main.cpp.o:main.cpp:(.text+0x17): undefined reference to glfwInit