我正在尝试在我的项目中链接GLFW。有我的项目结构的屏幕截图:
这是我的CMakeLists.txt:
cmake_minimum_required(VERSION 3.15)
project(testo)
set(CMAKE_CXX_STANDARD 17)
add_executable(testo main.cpp)
add_library(glfw3 STATIC main.cpp)
include_directories(lib/glfw/include/)
find_library(GLFW glfw3 lib/glfw/lib)
target_link_libraries(testo LINK_PUBLIC ${GLFW})
Howewer,当我尝试在clion中运行项目时,它给了我错误:
====================[ Build | testo | Debug ]===================================
"C:\Program Files\JetBrains\CLion 2019.2.5\bin\cmake\win\bin\cmake.exe" --build C:\Users\Student\testo\cmake-build-debug --target testo -- -j 2
[ 50%] Linking CXX executable testo.exe
CMakeFiles\testo.dir/objects.a(main.cpp.obj): In function `main':
C:/Users/Student/testo/main.cpp:5: undefined reference to `glfwInit'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [CMakeFiles\testo.dir\build.make:87: testo.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:77: CMakeFiles/testo.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:84: CMakeFiles/testo.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:117: testo] Error 2
答案 0 :(得分:1)
这里有一些github example。
这里是一些question you are duplicating。
基于此,它应如下所示:
cmake_minimum_required(VERSION 3.15)
project(testo)
set(CMAKE_CXX_STANDARD 17)
find_package(GLEW REQUIRED)
add_executable(testo main.cpp)
target_link_libraries(testo PUBLIC ${GLEW_LIBRARIES})
target_include_directories(testo PUBLIC ${GLEW_INCLUDE_DIRS})
答案 1 :(得分:0)
我认为这与linux在lib文件名中添加"lib***.a"
[pre / post]修复程序有关。
尝试像将lib/glfw/lib
一样将link_directories()
目录添加到include_dicrectories()
。
此外,add_library()
指定您正在尝试自己编译glfw3
,但是您尝试做的只是与之链接。我已为您删除了它。
cmake_minimum_required(VERSION 3.15)
project(testo)
set(CMAKE_CXX_STANDARD 17)
add_executable(testo main.cpp)
include_directories(lib/glfw/include/)
link_directories(lib/glfw/lib)
target_link_libraries(testo LINK_PUBLIC glfw3)