CMake:如何在包含的标头更改时自动重建 .obj 文件
CMake: How to automatically rebuild .obj-files when included header changed
从我自己的搜索中,我不确定"正常"行为是什么: 当关联的源文件中包含的标头发生更改时,CMake 通常会重建 .obj 文件吗? 因为它在我的项目中根本没有这样做。这是我的顶级CMakeList.txt:
cmake_minimum_required(VERSION 3.15 FATAL_ERROR)
enable_language(CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# project and binary name
project("myProjectName")
# compiler specific warnings
# and warnings are treated as errors
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR
"${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
set(warnings "-Wall -Wextra -Werror")
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
set(warnings "/W4 /WX /EHsc")
endif()
if (NOT CONFIGURED_ONCE)
set(CMAKE_CXX_FLAGS "${warnings}"
CACHE STRING "Flags used by the compiler during all build types." FORCE)
set(CMAKE_C_FLAGS "${warnings}"
CACHE STRING "Flags used by the compiler during all build types." FORCE)
endif()
# default build-type is Debug:
if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Debug")
endif()
# =============================
# libraries
# =============================
# external
# eigen numerics library
find_package (Eigen3 3.3 REQUIRED NO_MODULE)
set( EIGEN Eigen3::Eigen )
# some internal libraries here
set( LIB_NAME_1 lib1 )
set( LIB_NAME_2 lib2 )
# =============================
# directory configuration
# =============================
# for finding libraries and such in the project's subdirectories
# all paths are prepended with the project's root directory
set(CMAKE_PREFIX_PATH ${CMAKE_SOURCE_DIR})
# specifiying output directories
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
#set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
# the last one is disabled to allow for tests in a different directory
# include paths
include_directories(
${CMAKE_SOURCE_DIR}/include
${CMAKE_SOURCE_DIR}/lib # I put header-only libraries there
${CMAKE_SOURCE_DIR}/src # for template definitions
)
# =============================
# unit test configuration
# =============================
# enable_testing() sets the internal flag "CMAKE_TESTING_ENABLED" to 1
# add_test() commands are only run when enable_testing() has executed.
# here, it is only executed in debug mode
if(CMAKE_BUILD_TYPE MATCHES Debug)
enable_testing()
endif()
# the unit test framework used here is "Catch2" (single header file "catch.hpp")
# (https://github.com/catchorg/Catch2)
if ( ${CMAKE_TESTING_ENABLED} )
message( "-- Tests are enabled" )
set( UNIT_TEST_LIB catch2 )
endif()
# =============================
# subdir calls
# =============================
# the second parameter specifies the output path for binaries.
# however, the respective CMAKE_XYZ_OUTPUT_DIRECTORY takes precedence
add_subdirectory(lib)
add_subdirectory(src bin)
if ( ${CMAKE_TESTING_ENABLED} )
add_subdirectory(tests tests)
endif()
然后我有另一个 CMakeLists.txt在 lib、src 和测试中。这些命令仅包含使用相应源文件的addLibrary
、addExecutable
和targetLinkLibraries
命令。除了在"测试"中,还有addTest
和addCustomCommand
使单元测试在构建后运行
。问题是:我是否错过了某些内容,并且应该在标题更改时重建(1(?还是这是正常行为(2(?
可能相关的问题:
在标头更改时重建对象文件
Make 在更改时不会重建标头
GCC 包含标头(使用-include
个(CMake 未检测到的更改
> CMake 确实会在标头更改时重建对象文件,但是 CMake 3.15 有一个错误,它无法正常工作。我也遇到了这个问题,发现已经报告了:https://gitlab.kitware.com/cmake/cmake/issues/19507
它在 3.15.1 中修复,因此解决方案是升级(并可能将cmake_minimum_required更改为 3.15.1(。
相关文章:
- Android 在编译二进制文件时重建静态库
- 自定义构建文件更改不会触发VS 2017中的项目重建
- 每次都会重建多个程序的制作文件,尽管配方"all"被标记为虚假
- CMake:如何在包含的标头更改时自动重建 .obj 文件
- 为什么这个生成文件每次都会重建
- 一次又一次地重建以查看导入的 QML 文件更改
- Cmake和忍者重建不必要的文件
- 如果头文件被更改然后恢复,如何停止/欺骗 cmake 不重建
- 每次将对象文件转储到其他目录中时重建 makefile
- 强制 MS VS2010 在每次构建时重建一个 cpp 文件
- 删除它们后,会自动重建使用-mmd生成的依赖性文件
- 生成文件 - 使用修改后的头文件重建
- 如何获取 Rakefile 以自动重建依赖项文件
- Qt 在编译时生成包含 Git 哈希的头文件,并使用文件强制重建
- 二叉树不会从文件重建
- C++重建删除依赖文件
- openCV-如何从windows 8.1中为github for python下载的.zip文件中重建
- 如何在SWIG文件更改后重建项目
- 将一个文件编码为1/0二进制文件,并在c++中重建原始文件
- 使用GCC依赖文件会导致Make每次都完全重建项目