CLion 编译过剩 linux

CLion compile glut linux

我正在尝试在 linux 上编译一个使用 freeglut 的 C++ 项目。 我无法构建项目,因为 CMake 文件中的库链接不正确。我研究并尝试应用此处类似答案中提到的内容:How to compile GLUT + OpenGL project with CMake and Kdevelop in linux?

但是构建过程仍然失败,出现以下异常:

/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

我的 CMakeLists 文件如下所示:

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

我该如何解决这个问题?

target_link_libraries 想要目标名称。目标由(除其他外)add_executableadd_libraryadd_custom_target.

指定

换句话说,target_link_libraries(testas ...) 应该有效。 当你这样做的时候,你也应该考虑把你的 include_directories(...) 换成 target_include_directories(testas ...)