Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you have a common cmake minimum included from another file?

Tags:

cmake

I am quite new to cmake with a makefile background.

I like to use things like include(cmake_utils/header.cmake) to include common snippets of cmake files so that I can include them in my projects but only change them in one once in one place. Where cmake_utils is a git repo.

This is working nicely, but every single CMakeLists.txt I write has to have a cmake_minimum_required.

That is fine, but I may want to change this one day - lets say when one of my common files uses a feature from a newer version of cmake. In that case I don't want to go around changing all the CMakeLists.txt - I just want to change it in one place (ideally).

Here is my current 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)

I really want to move the line cmake_minimum_required(VERSION 3.10.2) into my cmake_utils/header.cmake file.

But when I do this I get the following error right at the end of calling 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".

Is this just a limitation of cmake that I have to live with, or is there a way to archive this?

It's also possible that I am still thinking like a gnu make writer and I have this all horribly wrong :o

like image 243
code_fodder Avatar asked Aug 31 '25 22:08

code_fodder


1 Answers

Per the documentation cmake_minimum_required: Call 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.

There is no way of getting around this.

like image 192
fdk1342 Avatar answered Sep 04 '25 20:09

fdk1342