Cmake 链接库目标链接错误
我在使用 cmake 连接 Glfw 和其他库时遇到问题.从命令行我像这样编译
Hi I have problem with linkg Glfw and other libraries using cmake. From command line i compile like this
g++ main.cpp -lGL -lGLU -lGLEW -lglfw
但我想使用 cmake 进行编译.我尝试使用 target_linkg_libraries 但这会产生错误
But I wanted to use cmake for compiling. I tried to use target_linkg_libraries but this produce error
CMakeLists.txt 中的 CMake 错误:18 (target_link_libraries):不能为不是由这个构建的目标GL"指定链接库
项目.
CMake Error at CMakeLists.txt:18 (target_link_libraries): Cannot specify link libraries for target "GL" which is not built by this
project.
我尝试使用添加定义来做到这一点.我没有看到错误,但这不会链接库.
I tried do this using add definitions. I dont see error but this don't link libraries.
cmake_minimum_required (VERSION 2.6)
project (test)
find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
ADD_DEFINITIONS(
-lGL
-lGLU
-lGLEW
-lglfw
)
add_executable(test.out
main.cpp
)
target_link_libraries(GL GLU GLEW glfw)
推荐答案
target_link_libraries
的语法是:
target_link_libraries(your_executable_name libraries_list)
而且您不必添加add_definition
语句(target_link_libraries
添加此选项)
And you don't have to add add_definition
statements (target_link_libraries
adds this options)
OpenGL 和 GLEW 包也提供了一些有用的变量.
There are also some useful variables provided by OpenGL and GLEW packages.
你的 CMakeLists.txt 应该是这样的:
Your CMakeLists.txt should be like:
cmake_minimum_required (VERSION 2.6)
project (test)
find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
include_directories(${OPENGL_INCLUDE_DIR} ${GLEW_INCLUDE_DIRS})
add_executable(test
main.cpp
)
target_link_libraries(test ${OPENGL_LIBRARIES} ${GLEW_LIBRARIES})
要记住的一个重要细节是将target_link_libraries
放在 add_executable
(或add_library
)之后线.
One important detail to keep in mind is to place the target_link_libraries
after the add_executable
(or add_library
) line.
相关文章