gpt4 book ai didi

c++ - CMake:如何在包含的 header 更改时自动重建 .obj 文件

转载 作者:行者123 更新时间:2023-11-30 04:43:30 25 4
gpt4 key购买 nike

根据我自己的搜索,我不确定“正常”行为是什么:当相关源文件中包含的 header 更改时,CMake 通常会重建 .obj 文件吗?因为它在我的项目中根本不这样做。这是我的顶级 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。这些仅包含使用适当源文件的 addLibraryaddExecutabletargetLinkLibraries 命令。除了在“测试”中,还有 addTestaddCustomCommand 使单元测试可执行文件在构建后运行。

问题是:我是不是漏掉了什么,它应该在 header 更改时重建 (1)?或者这是正常行为 (2)?

可能相关的问题:

Rebuilding object files when a header changes

Make doesn't rebuild headers when changed

GCC included header (using -include) changes not detected by CMake

最佳答案

当 header 更改时,CMake 确实会重建目标文件,但是 CMake 3.15 有一个错误,它不能正确地用于 make 目标。我也遇到了这个问题,发现已经报了:https://gitlab.kitware.com/cmake/cmake/issues/19507

它在 3.15.1 中已修复,因此解决方案是升级(并可能将 cmake_minimum_required 更改为 3.15.1)。

关于c++ - CMake:如何在包含的 header 更改时自动重建 .obj 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58246264/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com