我尝试使用 CLion 的 CMake 编译 GLFW 站点的示例代码(我以前根本没有使用过)。编译时出现此错误
cmd.exe /C "cd . && C:\MinGW\bin\g++.exe -g CMakeFiles/ImGUI.dir/main.cpp.obj -o ImGUI.exe -Wl,--out-implib,libImGUI.dll.a -Wl,--major-image-version,0,--minor-image-version,0 C:/LIB/GLFW/lib/libglfw3.a -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
CMakeFiles/ImGUI.dir/main.cpp.obj: In function `main':
C:/Users/Reilb/Desktop/ImGUI/main.cpp:8: undefined reference to `glfwInit'
C:/Users/Reilb/Desktop/ImGUI/main.cpp:12: undefined reference to `glfwCreateWindow'
C:/Users/Reilb/Desktop/ImGUI/main.cpp:15: undefined reference to `glfwTerminate'
C:/Users/Reilb/Desktop/ImGUI/main.cpp:20: undefined reference to `glfwMakeContextCurrent'
C:/Users/Reilb/Desktop/ImGUI/main.cpp:23: undefined reference to `glfwWindowShouldClose'
C:/Users/Reilb/Desktop/ImGUI/main.cpp:26: undefined reference to `_imp__glClear@4'
C:/Users/Reilb/Desktop/ImGUI/main.cpp:29: undefined reference to `glfwSwapBuffers'
C:/Users/Reilb/Desktop/ImGUI/main.cpp:32: undefined reference to `glfwPollEvents'
C:/Users/Reilb/Desktop/ImGUI/main.cpp:35: undefined reference to `glfwTerminate'
我的 glfw lib 位于 LIB 文件夹的根目录中:
C:/LIB
└───GLFW
├───include
│ ├───glfw3.h
│ └───glfw3native.h
├───lib-mingw
└───lib-mingw-w64
我目前正在使用这个文件“CMakeLists.txt”
cmake_minimum_required(VERSION 3.25)
project(ImGUI)
set(CMAKE_CXX_STANDARD 17)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
set(SOURCE_FILES main.cpp)
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
include(FindPkgConfig)
find_package(GLFW REQUIRED)
include_directories(${GLFW_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME} PUBLIC ${GLFW_LIBRARY})
它在位于同一级别的 cmake 文件夹中获取文件“FindGLFW.cmake”
set(FIND_GLFW_PATHS
C:/LIB/GLFW)
find_path(GLFW_INCLUDE_DIR glfw3.h
PATH_SUFFIXES include
PATHS ${FIND_GLFW_PATHS})
find_library(GLFW_LIBRARY
NAMES libglfw3.a
PATH_SUFFIXES lib-mingw-w64
PATHS ${FIND_GLFW_PATHS})
现在我尝试使用“lib-mingw”而不是“lib-mingw-w64”(在 FindGLFW.cmake 文件中),但它并没有改变问题。
我也试过在main.cpp开头加上“#define GLFW_DLL”,错误变成:
cmd.exe /C "cd . && C:\MinGW\bin\g++.exe -g CMakeFiles/Test.dir/main.cpp.obj -o Test.exe -Wl,--out-implib,libTest.dll.a -Wl,--major-image-version,0,--minor-image-version,0 C:/LIB/GLFW/lib-mingw-w64/libglfw3.a -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
CMakeFiles/Test.dir/main.cpp.obj: In function `main':
C:/Users/Reilb/Desktop/ImGUI/main.cpp:9: undefined reference to `_imp__glfwInit'
C:/Users/Reilb/Desktop/ImGUI/main.cpp:13: undefined reference to `_imp__glfwCreateWindow'
C:/Users/Reilb/Desktop/ImGUI/main.cpp:16: undefined reference to `_imp__glfwTerminate'
C:/Users/Reilb/Desktop/ImGUI/main.cpp:21: undefined reference to `_imp__glfwMakeContextCurrent'
C:/Users/Reilb/Desktop/ImGUI/main.cpp:24: undefined reference to `_imp__glfwWindowShouldClose'
C:/Users/Reilb/Desktop/ImGUI/main.cpp:27: undefined reference to `_imp__glClear@4'
C:/Users/Reilb/Desktop/ImGUI/main.cpp:30: undefined reference to `_imp__glfwSwapBuffers'
C:/Users/Reilb/Desktop/ImGUI/main.cpp:33: undefined reference to `_imp__glfwPollEvents'
C:/Users/Reilb/Desktop/ImGUI/main.cpp:36: undefined reference to `_imp__glfwTerminate'
正如您在上面的文件树中看到的那样,包含文件未放置在“GLFW”文件夹中,因此在 main.cpp 中我将
#include <GLFW/glfw3.h>
替换为 #include <glfw3.h>
.