Cmake link library target link error(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.
这篇关于Cmake 链接库目标链接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Cmake 链接库目标链接错误


- 从python回调到c++的选项 2022-11-16
- C++ 协变模板 2021-01-01
- 近似搜索的工作原理 2021-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- 静态初始化顺序失败 2022-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07