Cmake 链接库目标链接错误

Cmake link library target link error(Cmake 链接库目标链接错误)
本文介绍了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 链接库目标链接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Prevent class inheritance in C++(防止 C++ 中的类继承)
Why should I declare a virtual destructor for an abstract class in C++?(为什么要在 C++ 中为抽象类声明虚拟析构函数?)
Why is Default constructor called in virtual inheritance?(为什么在虚拟继承中调用默认构造函数?)
C++ cast to derived class(C++ 转换为派生类)
C++ virtual function return type(C++虚函数返回类型)
Is there any real risk to deriving from the C++ STL containers?(从 C++ STL 容器派生是否有任何真正的风险?)