c++ - 为什么 CMake 无法识别库,尽管它已由 'include_directories' 添加?

标签 c++ cmake dependency-management

我的 CMakeList 文件定义了应该包含在构建路径中的目录列表:

set(CMAKE_CXX_STANDARD 11)

include_directories(lib)
include_directories(lib/freeglut)
include_directories(lib/freeglut/include)
include_directories(lib/freeglut/include/GL)
include_directories(lib/matrix)
include_directories(lib/mavlink)
include_directories(lib/mavlink/common)
include_directories(src)
include_directories(src/Drawing)
include_directories(src/Math)
include_directories(src/MavlinkNode)
include_directories(src/Simulation)
include_directories(src/Utility)
...

所有依赖文件都在/lib 下。当我运行 CMake CMakeList.txt & make 时,出现以下错误:

[100%] Linking CXX executable FCND_Controls_CPP
CMakeFiles/FCND_Controls_CPP.dir/src/main.cpp.o: In function `main':
/home/peng/git-drone/__Udacity__/FCND-Controls-CPP/src/main.cpp:68: undefined reference to `glutTimerFunc'
/home/peng/git-drone/__Udacity__/FCND-Controls-CPP/src/main.cpp:70: undefined reference to `glutMainLoop'
...

虽然函数 glutTimerFunc 和 glutMainLoop 都在 'lib/freeglut/include/GL/freeglut.h' 下。然而,CMake 和 make 都忽略了它们。为什么会发生这种情况,我应该如何解决?

我使用的是 cmake 3.9.1,我尝试了 2 个后端:gcc/g++ 和 clang/clang++,但都给出了同样的错误。

最佳答案

您正在使用 include_directories,这是供编译器检查包含的目录。您应该使用的是 target_link_libraries

但是,由于您使用的是已知库,因此您需要执行类似的操作(您可以在 cmake-modules< 下的 FindGLUT.cmake 文件中找到此信息 目录):

find_package(GLUT REQUIRED)
target_include_directories(your_exe PUBLIC ${GLUT_INCLUDE_DIR}) # could also be PRIVATE, depending on usage
target_link_libraries(your_exe ${GLUT_LIBRARY} )

请注意,您还需要对具有缓存变量 ${OPENGL_INCLUDE_DIR}${OPENGL_LIBRARIES}OpenGL 库执行相同的操作>.

Note: Try to avoid using include_directories (without target) as that is considered the older style and has the drawback of pollutating the include directories of all targets.

关于c++ - 为什么 CMake 无法识别库,尽管它已由 'include_directories' 添加?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49703455/

相关文章:

c++ - 将 cmake 与 Code::Blocks 一起使用

c++ - 如何在 CMake 项目中包含 IntelliSense 的外部库源?

grails - 如何将软管鸟客户端库集成到Grails项目?

eclipse - 在运行应用程序上将依赖项目添加到 eclipse grails 项目失败

c++ - 在 Eigen 中创建 reshape 函数

cmake - Doxygen 使用过时版本的 bison OSX Big Sur

c++ - 使用反向传播神经网络进行人脸识别?

Python 包依赖树

c++ - 在异常规范中仅使用 std::exception

c++ - 选择适当的FIFO数据结构