CMake:如何在包含 header 时自动重建 .obj-files 更改
CMake: How to automatically rebuild .obj-files when included header changed
根据我自己的搜索,我不确定 "normal" 行为是什么:
当关联源文件中包含的 header 更改时,CMake 通常会重建 .obj-files 吗?
因为它在我的项目中根本不这样做。这是我的 top-level CMakeLists.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()
然后我在 lib、src 和测试中还有另一个 CMakeLists.txt。这些仅包含使用适当源文件的 addLibrary
、addExecutable
和 targetLinkLibraries
命令。除了 "tests",其中还有 addTest
和 addCustomCommand
使单元测试在构建后可执行 运行。
问题来了:我是不是漏掉了什么,当 header 发生变化 (1) 时它应该正在重建?或者这是正常行为 (2)?
可能相关的问题:
Make doesn't rebuild headers when changed
CMake 会在 headers 更改时重建 object 文件,但是 CMake 3.15 有一个错误,它不能正确地用于 make 目标。我也遇到了这个问题,发现已经报了:https://gitlab.kitware.com/cmake/cmake/issues/19507
在 3.15.1 中已经修复,所以解决方案是升级(可能会将 cmake_minimum_required 更改为 3.15.1)。
根据我自己的搜索,我不确定 "normal" 行为是什么: 当关联源文件中包含的 header 更改时,CMake 通常会重建 .obj-files 吗? 因为它在我的项目中根本不这样做。这是我的 top-level CMakeLists.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()
然后我在 lib、src 和测试中还有另一个 CMakeLists.txt。这些仅包含使用适当源文件的 addLibrary
、addExecutable
和 targetLinkLibraries
命令。除了 "tests",其中还有 addTest
和 addCustomCommand
使单元测试在构建后可执行 运行。
问题来了:我是不是漏掉了什么,当 header 发生变化 (1) 时它应该正在重建?或者这是正常行为 (2)?
可能相关的问题:
Make doesn't rebuild headers when changed
CMake 会在 headers 更改时重建 object 文件,但是 CMake 3.15 有一个错误,它不能正确地用于 make 目标。我也遇到了这个问题,发现已经报了:https://gitlab.kitware.com/cmake/cmake/issues/19507
在 3.15.1 中已经修复,所以解决方案是升级(可能会将 cmake_minimum_required 更改为 3.15.1)。