如何使用关键字签名在 CMake 上链接 opengl 库

Hah*_*pro 1 c++ opengl cmake vcpkg

我以前将 Visual Studio 与 NuGet 一起用于所有包。现在我改为 CMake。

现在我使用vcpkg来管理库。

但是,我需要 OpenGL

Cmake 链接 freeglut、glew、glm、libpng、zlib 的命令由 vcpkg 提供。但不是 OpenGL。

cmake_minimum_required(VERSION 3.0)
project(little_plane)

set(CMAKE_CXX_STANDARD 14)

add_executable(little_plane main.cpp)

# ./vcpkg install freeglut
find_package(GLUT REQUIRED)
target_link_libraries(little_plane PRIVATE GLUT::GLUT)


## ./vcpkg install glew
#find_package(GLEW REQUIRED)
#target_link_libraries(little_plane PRIVATE GLEW::GLEW)

#
# glm
find_package(glm CONFIG REQUIRED)
target_link_libraries(little_plane PRIVATE glm)

# ./vcpkg install libpng
find_package(PNG REQUIRED)
target_link_libraries(little_plane PRIVATE PNG::PNG)
##

find_package(ZLIB REQUIRED)
target_link_libraries(little_plane PRIVATE ZLIB::ZLIB)

find_package(OpenGL REQUIRED)

if (OPENGL_FOUND)
    message("opengl found")
    message("include dir: ${OPENGL_INCLUDE_DIR}")
    message("link libraries: ${OPENGL_gl_LIBRARY}")
else (OPENGL_FOUND)
    message("opengl not found")
endif()

target_link_libraries(little_plane ${OPENGL_gl_LIBRARY})


find_package(glfw3 CONFIG REQUIRED)
target_link_libraries(little_plane PRIVATE glfw)
Run Code Online (Sandbox Code Playgroud)

使用上面的 CMakeLists.txt,我运行 cmake .

 opengl found
include dir: /usr/include
link libraries: /usr/lib/x86_64-linux-gnu/libGL.so
CMake Error at CMakeLists.txt:40 (target_link_libraries):
  The keyword signature for target_link_libraries has already been used with
  the target "little_plane".  All uses of target_link_libraries with a target
  must be either all-keyword or all-plain.

  The uses of the keyword signature are here:

   * CMakeLists.txt:10 (target_link_libraries)
   * CMakeLists.txt:20 (target_link_libraries)
   * CMakeLists.txt:24 (target_link_libraries)
   * CMakeLists.txt:28 (target_link_libraries)



CMake Error at CMakeLists.txt:44 (target_link_libraries):
  The plain signature for target_link_libraries has already been used with
  the target "little_plane".  All uses of target_link_libraries with a target
  must be either all-keyword or all-plain.

  The uses of the plain signature are here:

   * CMakeLists.txt:40 (target_link_libraries)



-- Configuring incomplete, errors occurred!
Run Code Online (Sandbox Code Playgroud)

这意味着我的系统上安装了 opengl。我只是不知道如何使用 target_link_libraries 与我的项目链接。

如果可能,请提供可以复制并粘贴到 CMakeLists.txt 中的答案。

Rei*_*ica 8

您之前的所有内容target_link_libraries都包含一个传递关键字(PRIVATE在所有情况下),但是您在链接 OpenGL 时没有提供任何传递关键字。所以也只需添加:

target_link_libraries(little_plane PRIVATE ${OPENGL_gl_LIBRARY})
Run Code Online (Sandbox Code Playgroud)