2

I am previously using Visual Studio with NuGet for all package. Now I change to CMake.

Now I am using vcpkg to manage library.

However, I need OpenGl

The command of Cmake to link freeglut, glew, glm, libpng, zlib was provide by vcpkg. But not 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)

With the CMakeLists.txt above, I run 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!

Which mean opengl is installed on my system. I just don't know how to use target_link_libraries to link with my project.

Provide answer that can copy and paste into CMakeLists.txt if possible.

genpfault
  • 51,148
  • 11
  • 85
  • 139
Haha TTpro
  • 5,137
  • 6
  • 45
  • 71

1 Answers1

9

All your previous target_link_libraries contain a transitivity keyword (PRIVATE in all cases), but you have not provided any when linking OpenGL. So just add that too:

target_link_libraries(little_plane PRIVATE ${OPENGL_gl_LIBRARY})
Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • 1
    I got the same issue and this worked, but where do you find this magic `${OPENGL_gl_LIBRARY}` phrase, this works, but if i face a similar issue with another library, how do I figure what to exactly put in there? When i install opengl from vcpkg, I don't get any instruction on how to link it. Is there like some place in vcpkg directory with all of these stuff? – Bmax May 09 '21 at 17:09
  • @Bmax It's in the documentation of the CMake support for the package. If the package provides a package config file (e.g. `FooConfig.cmake`), look into its documentation. If you're finding the pakcage using a CMake-shipped find module for that package (https://cmake.org/cmake/help/latest/manual/cmake-modules.7.html#find-modules), see that find module's documentation. If you're using a 3rd-party-provided find module, look into its docs. – Angew is no longer proud of SO May 10 '21 at 07:29