cmake

Cannot build CMake project because "Compatibility with CMake < 3.5 has been removed from CMake."


I want to clone and build the following example repository:

https://github.com/bewagner/fetchContent_example

This is what I did:

git clone https://github.com/bewagner/fetchContent_example.git
cd fetchContent_example
mkdir build
cd build
cmake ..

But CMake gives me the following error:

CMake Error at build/_deps/doctest-src/CMakeLists.txt:1 (cmake_minimum_required):
  Compatibility with CMake < 3.5 has been removed from CMake.

  Update the VERSION argument <min> value.  Or, use the <min>...<max> syntax
  to tell CMake that the project requires at least <min> but has been updated
  to work with policies introduced by <max> or earlier.

  Or, add -DCMAKE_POLICY_VERSION_MINIMUM=3.5 to try configuring anyway.

Here doctest is a dependency of this project. If I open build/_deps/doctest-src/CMakeLists.txt, the first line reads:

cmake_minimum_required(VERSION 3.0)

On the other hand, if I run

cmake --version

then I get

cmake version 4.0.0-rc4

I'm confused. How can I build this project?

Is the error saying that my CMake version is too new to build this project? Do I need to install an older version of CMake?

Suppose then I have two dependencies: one that requires CMake 3.2 and one that requires CMake 4.0. Is it impossible to build the project without changing the code in the dependencies?


Solution

  • Is the error saying that my CMake version is too new to build this project? Do I need to install an older version of CMake?

    Yes, this is the exact meaning of the error message: CMake 4.0.0 no longer supports features deprecated in CMake 3.5 or earlier.

    If a project actually uses such deprecated features, then you need to build the project with CMake 3.31 or older.

    If a project doesn't use such deprecated features, but is written with

    cmake_minimum_required(VERSION 3.4) # or lower CMake version
    

    then you could redefine a project's VERSION specification via setting CMAKE_POLICY_VERSION_MINIMUM parameter.

    Suppose then I have two dependencies: one that requires CMake 3.2 and one that requires CMake 4.0. Is it impossible to build the project without changing the code in the dependencies?

    If your project's dependencies can be expressed as pre-installed libraries, then you could just build (and install) the first one with older CMake, build (and install) the other one with newer CMake, and then build your project with CMake suitable for it.

    If your project's dependencies are added with add_subdirectory calls (or via FetchContent mechanism), then they will co-exist in a single CMake "environment". If these dependencies require incompatible CMake versions, then your project simply won't work.