0

I am trying to compile a C++ project on linux which utilizes freeglut. I can not build the project because the libraries are not linked correctly in the CMake files. I researched and tried to apply what was mentioned in a similar answer here: How to compile GLUT + OpenGL project with CMake and Kdevelop in linux?

However build process still fails with the following exception:

/opt/JetBrains/apps/CLion/ch-0/181.4668.70/bin/cmake/bin/cmake --build /home/user/Documents/Projects/GdvProject/cmake-build-debug --target testas -- -j 2
CMake Error at CMakeLists.txt:9 (target_link_libraries):
  Cannot specify link libraries for target "GdvProject" which is not built by
  this project.


-- Configuring incomplete, errors occurred!
See also "/home/user/Documents/Projects/GdvProject/cmake-build-debug/CMakeFiles/CMakeOutput.log".
make: *** [Makefile:176: cmake_check_build_system] Error 1

My CMakeLists file looks like this:

cmake_minimum_required(VERSION 2.8)

project(GdvProject)
add_executable(testas main.cpp)
find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)
include_directories( ${OPENGL_INCLUDE_DIRS}  ${GLUT_INCLUDE_DIRS} )

target_link_libraries(GdvProject ${OPENGL_LIBRARIES} ${GLUT_LIBRARY})

How can I fix this issue?

Kyu96
  • 1,159
  • 2
  • 17
  • 35
  • Possible duplicate of [Getting a CMake Error: Cannot specify link libraries for target which is not built by the project](https://stackoverflow.com/questions/25909943/getting-a-cmake-error-cannot-specify-link-libraries-for-target-which-is-not-bui) – Tsyvarev May 14 '18 at 13:57

1 Answers1

1

target_link_libraries wants a target name. Targets are specified by (among others) add_executable, add_library and add_custom_target.

In other words, target_link_libraries(testas ...) should work. While you're at it, you should consider switching your include_directories(...) to target_include_directories(testas ...) as well.

Botje
  • 26,269
  • 3
  • 31
  • 41
  • Whats the advantage of switching `include_directories(...)` to `arget_include_directories(testas ...)`? – Kyu96 May 14 '18 at 12:14
  • 1
    Consistency, mostly. It also means that other targets you create in this project do not need to depend on whatever `testas` depends. – Botje May 14 '18 at 12:28