你能从另一个文件中包含一个通用的 cmake 最小值吗?

cod*_*der 3 cmake

我对具有 makefile 背景的 cmake 很陌生。

我喜欢使用诸如include(cmake_utils/header.cmake)包含 cmake 文件的常见片段之类的方法,这样我就可以将它们包含在我的项目中,但只能在一处更改一次。cmake_utilsgit 仓库在哪里。

这工作得很好,但我写的每一个 CMakeLists.txt 都必须有一个cmake_minimum_required.

这很好,但我可能希望有一天改变这一点 - 假设我的一个常见文件使用了较新版本的 cmake 中的一项功能。在这种情况下,我不想更改所有 CMakeLists.txt - 我只想在一个地方更改它(理想情况下)。

这是我当前的 CMakeFile.txt:

cmake_minimum_required(VERSION 3.10.2)

# Include common elements
include(cmake_utils/header.cmake)
include(cmake_utils/cpp_flags.cmake)

# Include path
include_directories(
    inc
    inc/log4cpp
)

# Include source files by wild card
file(GLOB SOURCES "src/log4cpp/*.cpp")

# Setup output and libs
include(cmake_utils/output_lib_shared.cmake)
include(cmake_utils/common_libs.cmake)
Run Code Online (Sandbox Code Playgroud)

我真的想将该行cmake_minimum_required(VERSION 3.10.2)移到我的cmake_utils/header.cmake文件中。

但是当我这样做时,我在调用结束时收到以下错误cmake

CMake Error in CMakeLists.txt:
  No cmake_minimum_required command is present.  A line of code such as

    cmake_minimum_required(VERSION 3.10)

  should be added at the top of the file.  The version specified may be lower
  if you wish to support older CMake versions for this project.  For more
  information run "cmake --help-policy CMP0000".
Run Code Online (Sandbox Code Playgroud)

这只是我必须忍受的 cmake 的限制,还是有办法将其存档?

也有可能我仍然像gnu make作家一样思考,而这一切都错了:o

Fre*_*red 5

根据文档cmake_minimum_requiredCall the cmake_minimum_required() command at the beginning of the top-level CMakeLists.txt file even before calling the project() command. It is important to establish version and policy settings before invoking other commands whose behavior they may affect.

没有办法解决这个问题。