使用 cmake 处理头文件依赖项

2021-12-26 00:00:00 dependencies cmake c++ header-files

我在一个小型 C++ 项目中使用 CMake,到目前为止它工作得很好......只需一点点:x

I am using CMake on a small C++ project and so far it works great... with one twist :x

当我更改头文件时,它通常需要重新编译许多源文件(直接或间接包含它的那些),但是似乎 cmake 只检测一些源文件以被重新编译,导致损坏状态.我可以通过清除项目并从头开始重建来解决这个问题,但这绕过了使用 make 实用程序的目标:只重新编译需要的内容.

When I change a header file, it typically requires recompiling a number of sources files (those which include it, directly or indirectly), however it seems that cmake only detects some of the source files to be recompiled, leading to a corrupted state. I can work around this by wiping out the project and rebuilding from scratch, but this circumvents the goal of using a make utility: only recompiling what is needed.

因此,我想我做错了什么.

Therefore, I suppose I am doing something wrong.

我的项目非常简单:

  • 所有资源所在的顶级目录,主要的 CMakeLists.txt 位于那里
  • 一个包含"所有公共头文件所在的目录(在各个子目录中)
  • 一个src"源文件的所有子目录所在的目录,src CMakeLists.txt 就在那里
  • src"中每个子目录的 CMakeLists.txt目录

主目录有:

cmake_minimum_required(VERSION 2.8)

project(FOO)

set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)

# Compiler Options
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -std=c++0x -Wall -Wextra -Werror")

include_directories($(FOO_SOURCE_DIR)/include)

add_subdirectory(src)

src"目录:

add_subdirectory(sub1)
add_subdirectory(sub2)
add_subdirectory(sub3)
add_subdirectory(sub4)

add_executable(foo main.cpp)

target_link_libraries(foo sub1 sub2 sub3 sub4)

其中 sub4 依赖于 sub3 依赖于 sub2 依赖于 sub1

Where sub4 depends on sub3 which depends on sub2 which depends on sub1

以及一个子目录(sub3)的例子:

And an example of a subdirectory (sub3):

set(SUB3_SRCS
    File1.cpp
    File2.cpp
    File3.cpp
    File4.cpp
    File5.cpp
    File6.cpp
    )

add_library(sub3 ${SUB3_SRCS})

target_link_libraries(sub3 sub1 sub2)

如果有人能指出我的错误,我会很高兴,在这里或在 CMake 上搜索没有产生任何结果,所以我想这很容易或者应该开箱即用...

I'd be glad if anyone could point my mistake to me, searching here or on CMake didn't yield anything so I guess it's very easy or should work out of the box...

(作为参考,我在 MSYS 上使用的是 cmake 2.8.2 版)

(for reference, I am using cmake version 2.8.2 on MSYS)

编辑:

感谢 Bill 的建议,我检查了 CMake 生成的 depend.make 文件,确实缺少(严重).下面是一个例子:

Thanks to Bill's suggestion I have checked the depend.make file generated by CMake, and it is indeed lacking (severely). Here is an example:

src/sub3/CMakeFiles/sub3.dir/File1.cpp.obj: ../src/sub3/File1.cpp

是的,就是这样,根本没有引用任何包含:x

Yep, that's all, none of the includes were referenced at all :x

推荐答案

您应该查看二叉树中的 depend.make 文件.它将在 CMakeFiles/target.dir/depend.make 中.尝试找到其中一个缺少您认为应该具有的 .h 文件的文件.然后为 cmake 创建错误报告或通过电子邮件发送 cmake 邮件列表.

You should look at the depend.make files in your binary tree. It will be in CMakeFiles/target.dir/depend.make. Try to find one of those that is missing a .h file that you think it should have. Then create a bug report for cmake or email the cmake mailing list.

相关文章