CMake头文件依赖项丢失

时间:2019-04-11 15:16:01

标签: c++ cmake

我有以下小项目。

.
├── lib
|   ├── foo.cpp
|   └── foo.h
├── tests
|   └── test_main.cpp
└── CMakeLists.txt

CMakeLists.txt:

cmake_minimum_required(VERSION 3.13)
project(FOO)
set(CMAKE_CXX_STANDARD 17)

add_library(foo STATIC lib/foo.cpp lib/foo.h)
target_include_directories(foo PUBLIC ${CMAKE_CURRENT_LIST_DIR})

add_executable(cpp_test tests/test_main.cpp lib/foo.h)
target_link_libraries(cpp_test foo)

tests / test_main.cpp:

#include "lib/foo.h"

int main() {
    ...
}

显然,cpp_test目标取决于lib/foo.h头文件,但是在更新cpp_test时不会重建lib/foo.h

确实,CMakeFiles/cpp_test.dir/depend.make缺少对lib/foo.h的依赖性,如下所示。

# CMAKE generated file: DO NOT EDIT!
# Generated by "Unix Makefiles" Generator, CMake Version 3.14
CMakeFiles/cpp_test.dir/tests/test_main.cpp.o: ../tests/test_main.cpp

每次cpp_test更改时,我们如何强制cmake重建lib/foo.h

编辑

该问题用Handling header files dependencies with cmake标记为重复,但是该问题说明了简单项目的更基本问题,我仍然无法从该问题中找到任何解决方案。我也尝试添加tests/CMakeLists.txt

file(RELATIVE_PATH FOO_SOURCE_REL_DIR
        ${CMAKE_CURRENT_SOURCE_DIR}
        ${FOO_SOURCE_DIR}/.)
include_directories(${FOO_SOURCE_REL_DIR}/lib)

add_executable(cpp_test test_main.cpp ${FOO_SOURCE_REL_DIR}/lib/foo.cpp 
${FOO_SOURCE_REL_DIR}/lib/foo.h )
target_link_libraries(cpp_test foo)

和更新的根CMakeLists.txt如下,

cmake_minimum_required(VERSION 3.13)
project(FOO)

set(CMAKE_CXX_STANDARD 17)

add_library(foo STATIC lib/foo.cpp lib/foo.h)

add_subdirectory(tests)

但仍然无法正常工作。我希望在不使用include_directories的情况下能够解决此问题,因为在现代cmake中不建议使用include_directories

0 个答案:

没有答案