使用cmake处理头文件依赖项 [英] Handling header files dependencies with cmake
问题描述
我在一个小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.
我的项目非常简单:
- 所有公开标题所在的include目录(在各个子目录中)
- src目录中每个子目录的CMakeLists.txt
一个src目录,其中包含源文件的所有子目录,src CMakeLists.txt位于
- a top directory where all resources sit, the main CMakeLists.txt sits there
- a "include" directory where all public headers lies (in various subdirectories)
- a "src" directory where all the subdirectories for sources files are, the src CMakeLists.txt sits there
- a CMakeLists.txt per subdirectory in the "src" directory
主目录包含:
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)
EDIT :
感谢Bill的建议,我已经检查了 / code>文件生成的CMake,它确实缺乏(严重)。下面是一个例子:
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, non 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.
这篇关于使用cmake处理头文件依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!