cmake_minimum_required & project()
Every top-level CMakeLists.txt starts with the same two commands, in this order. They look like boilerplate, but both change how the rest of the file behaves.
cmake_minimum_required()
cmake_minimum_required(VERSION 3.25)This line does two things:
- It fails early if the CMake running the project is older than 3.25, instead of failing later with a confusing "unknown command" error.
- It sets the policy version. CMake keeps old behaviour available through policies (numbered
CMP0000…CMP0200+). Every policy introduced up to the version you name is set to its NEW behaviour; newer ones keep their old behaviour (and may warn). So the same file behaves the same on CMake 3.25 and CMake 4.4.
You can also give a range: cmake_minimum_required(VERSION 3.25...4.4) means "works with 3.25, and I have tested up to 4.4, so use 4.4's policies when available". This is the recommended form for libraries that many people build with different CMake versions.
Note: CMake 4.0 removed compatibility with versions older than 3.5:
cmake_minimum_required(VERSION 3.1)is now an error. The Policies & CMake 4 lesson shows the exact messages and the escape hatch.
project()
project(Hello
VERSION 1.2.0
DESCRIPTION "A friendly greeter"
HOMEPAGE_URL "https://example.com/hello"
LANGUAGES CXX)project() names the project, records its version and enables languages. Enabling a language is what runs the compiler detection, so list only what you use. The default is C CXX; a pure C++ project should say LANGUAGES CXX and skip the C compiler checks.
It also defines variables that the rest of the build (and later install() and CPack) reads:
mkdir -p hello && cd hello
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Hello
VERSION 1.2.0
DESCRIPTION "A friendly greeter"
HOMEPAGE_URL "https://example.com/hello"
LANGUAGES CXX)
message("PROJECT_NAME = ${PROJECT_NAME}")
message("PROJECT_VERSION = ${PROJECT_VERSION}")
message(" MAJOR/MINOR/PATCH = ${PROJECT_VERSION_MAJOR} / ${PROJECT_VERSION_MINOR} / ${PROJECT_VERSION_PATCH}")
message("Hello_VERSION = ${Hello_VERSION}")
message("PROJECT_DESCRIPTION = ${PROJECT_DESCRIPTION}")
message("PROJECT_SOURCE_DIR = ${PROJECT_SOURCE_DIR}")
message("PROJECT_BINARY_DIR = ${PROJECT_BINARY_DIR}")
message("PROJECT_IS_TOP_LEVEL = ${PROJECT_IS_TOP_LEVEL}")
message("CMAKE_CXX_COMPILER_ID = ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}")
CM
cmake -S . -B build -G Ninja --log-level=NOTICEPROJECT_NAME = Hello
PROJECT_VERSION = 1.2.0
MAJOR/MINOR/PATCH = 1 / 2 / 0
Hello_VERSION = 1.2.0
PROJECT_DESCRIPTION = A friendly greeter
PROJECT_SOURCE_DIR = /home/learner/hello
PROJECT_BINARY_DIR = /home/learner/hello/build
PROJECT_IS_TOP_LEVEL = ON
CMAKE_CXX_COMPILER_ID = GNU 13.3.0
-- Configuring done (0.2s)
-- Generating done (0.0s)
-- Build files have been written to: /home/learner/hello/build
The same values are also available under the project's own name (Hello_VERSION, Hello_SOURCE_DIR), which is handy when one project includes another: PROJECT_* always means "the closest project() call", while Hello_* always means this one. PROJECT_IS_TOP_LEVEL tells you whether your project is being built on its own or as part of a bigger build, and is the usual guard for things only the top-level build should do (tests, packaging); see Structuring Real Projects.
What happens when they are missing
CMake is forgiving here, which hides mistakes: leave both commands out and it still builds, but it warns twice. It pretends there was a project(Project) line (so C and C++ get enabled), and it runs without a policy version:
mkdir -p sloppy && cd sloppy
echo 'add_executable(app main.cpp)' > CMakeLists.txt
echo 'int main() {}' > main.cpp
cmake -S . -B build -G Ninja --log-level=NOTICECMake Warning (author) in CMakeLists.txt:
No project() command is present. The top-level CMakeLists.txt file must
contain a literal, direct call to the project() command. Add a line of
code such as
project(ProjectName)
near the top of the file, but after cmake_minimum_required().
CMake is pretending there is a "project(Project)" command on the first
line.
This warning is for project developers. Use -Wno-author to suppress it.
CMake Warning (author) in CMakeLists.txt:
cmake_minimum_required() should be called prior to this top-level project()
call. Please see the cmake-commands(7) manual for usage documentation of
both commands.
This warning is for project developers. Use -Wno-author to suppress it.
-- Configuring done (0.4s)
-- Generating done (0.0s)
-- Build files have been written to: /home/learner/sloppy/build
Treat these author warnings as errors in your own projects: put cmake_minimum_required() on line 1 and a literal project() call right after it, in the top-level CMakeLists.txt (not hidden in an included file or a function).
Choosing the minimum version
Pick the oldest CMake you actually test with, not the newest you have. Some useful milestones:
| Version | Notable for this course |
|---|---|
| 3.5 | the oldest version CMake 4 still accepts |
| 3.14 | FetchContent_MakeAvailable, install(TARGETS) default destinations |
| 3.19 | CMakePresets.json |
| 3.21 | PROJECT_IS_TOP_LEVEL, --toolchain option |
| 3.23 | FILE_SET HEADERS for installing headers |
| 3.24 | FIND_PACKAGE_ARGS in FetchContent, --fresh |
| 3.25 | workflow presets, block() |
| 3.28 | C++20 modules support |
| 3.30 | FetchContent without a sub-build (policy CMP0168) |
| 4.0 | compatibility with < 3.5 removed |
Ubuntu 24.04 ships CMake 3.28, and the official binaries and pip install cmake give the latest release on any distribution, so 3.25 is a reasonable floor for new projects in 2026. This course uses 3.25 unless a lesson needs something newer.
Test yourself
What does cmake_minimum_required(VERSION 3.25) change besides checking the CMake version?
- Nothing else
- It sets every policy introduced up to 3.25 to NEW behaviour
- It downloads CMake 3.25
- It sets the C++ standard
A C++-only project calls project(App) without LANGUAGES. What happens?
- Only C++ is enabled
- C and C++ are both enabled, so CMake also looks for a C compiler
- No language is enabled
- It is an error
Which variable tells you whether your project is the top-level build or was added by another project?
CMAKE_PROJECT_NAMEPROJECT_IS_TOP_LEVELPROJECT_SOURCE_DIRCMAKE_SOURCE_DIR