简体   繁体   English

CMake链接库目标链接错误

[英]Cmake link library target link error

Hi I have problem with linkg Glfw and other libraries using cmake. 嗨,我对使用cmake的linkg Glfw和其他库有问题。 From command line i compile like this 从命令行我这样编译

g++ main.cpp -lGL -lGLU -lGLEW -lglfw

But I wanted to use cmake for compiling. 但是我想使用cmake进行编译。 I tried to use target_linkg_libraries but this produce error 我尝试使用target_linkg_libraries,但这会产生错误

CMake Error at CMakeLists.txt:18 (target_link_libraries): Cannot specify link libraries for target "GL" which is not built by this CMakeLists.txt:18(target_link_libraries)的CMake错误:无法为目标“ GL”指定链接库,该链接库不是由此库构建的
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)

The syntax for target_link_libraries is: target_link_libraries的语法为:

target_link_libraries(your_executable_name libraries_list)

And you don't have to add add_definition statements ( target_link_libraries adds this options) 而且你不必添加add_definition报表( target_link_libraries添加此选项)

There are also some useful variables provided by OpenGL and GLEW packages. OpenGL和GLEW软件包还提供了一些有用的变量。

Your CMakeLists.txt should be like: 您的CMakeLists.txt应该类似于:

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})

One important detail to keep in mind is to place the target_link_libraries after the add_executable (or add_library ) line. 记住一个重要的细节是把target_link_libraries add_executable (或add_library )线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM