{}const=>[]async()letfn</>var
Development

🛠️ CMake without pain: set up the build once and forget

CMake scares many, but not you. Here is a simple and clear guide to setting up CMake for C++ without pain and magic. Mini-template, tips, life hacks — and all this accompanied by Kodik 🚀

К

Kodik

Author

2 min read

If you've written in C++, you've probably encountered CMake. And maybe you had a moment of despair when everything just is not going to. CMake is a powerful tool, but its syntax and structure often cause pain. Let's figure out how to configure CMake once and correctlyto avoid returning to chaos 💡

🔰 What is CMake?

CMake is a build generation system. It does not compile the code directly, but creates files for assembly for your system: Makefile, Ninja, Xcode, or Visual Studio.

  • ✅ Works cross-platform

  • ✅ Allows you to customize dependencies

  • ✅ Simplifies the assembly of large projects

  • ✅ Compatible with IDE and CI/CD

🔥 100,000+ students already with us

Tired of reading theory?
Time to code!

Kodik — an app where you learn to code through practice. AI mentor, interactive lessons, real projects.

🤖 AI 24/7
🎓 Certificates
💰 Free
🚀 Start learning
Joined today

🧱 Basic project structure with CMake

project-root/
├── CMakeLists.txt
├── src/
│   └── main.cpp
├── include/
│   └── mylib.hpp
├── CMakeLists.txt (внутри src/)

📌 Root CMakeLists.txt:

cmake_minimum_required(VERSION 3.15)
project(MyProject)

set(CMAKE_CXX_STANDARD 20)

add_subdirectory(src)

📌 src/CMakeLists.txt:

add_executable(my_app main.cpp)
target_include_directories(my_app PRIVATE ../include)

⚙️ Tips for setting up CMake without pain

1. Use target_* instead of global variables

❌ Bad:

include_directories(./include)

✅ Correct:

target_include_directories(my_app PRIVATE ./include)

2. Add compile_commands.json

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

Helps autocomplete and highlight errors in VS Code and clangd.

3. Always use out-of-source build

mkdir build && cd build
cmake ..
cmake --build .

4. Connect third-party libraries correctly

Example with FetchContent:

include(FetchContent)
FetchContent_Declare(
  json 
  URL https://github.com/nlohmann/json/releases/latest/download/json.hpp
)
FetchContent_MakeAvailable(json)

target_link_libraries(my_app PRIVATE nlohmann_json::nlohmann_json)

🧪 Checklist of a good CMake project

  • ✅ CMakeLists.txt is no longer than 30 lines

  • ✅ All dependencies through target_*

  • ✅ Debug/Release support

  • ✅ Out-of-source build

  • ✅ Works in terminal and IDE

📚 Want to delve into the topic?
In the attachment Code you will find detailed lessons on CMake and C++, step-by-step exercises, error analysis and convenient practice right on your phone or browser.
And if you want to be aware of the news, new features and useful materials - subscribe to our Telegram channel. It's cozy, businesslike and with love for code ❤️

🎯Stop procrastinating

Liked the article?
Time to practice!

In Kodik, you don't just read — you write code immediately. Theory + practice = real skills.

Instant practice
🧠AI explains code
🏆Certificate

No registration • No card