Building C/C++ Projects with CMake
CMake is a meta-build system: project metadata in CMakeLists.txt describes targets, while CMake generates native build files (Makefiles, Ninja, Visual Studio solutions). Use out-of-source builds to keep the source tree clean.
A minimal project
Section titled “A minimal project”CMakeLists.txt:
cmake_minimum_required(VERSION 3.16)project(hello C)add_executable(hello src/main.c)target_compile_features(hello PRIVATE c_std_11)Build steps:
mkdir -p build && cd buildcmake -DCMAKE_BUILD_TYPE=Debug ..cmake --build .Common options
Section titled “Common options”CMAKE_BUILD_TYPE=Debug|Releasecontrols optimization and debug info-DCMAKE_C_FLAGS="-Wall -Wextra"to surface warnings
- Prefer
Ninjaas the underlying backend for fast parallel builds:cmake -G Ninja ... - Use
find_packageto locate dependencies (e.g.,find_package(PkgConfig REQUIRED)then usepkg_check_modules). - Keep tests in a
tests/directory and useenable_testing()+add_test().
Why this matters
Section titled “Why this matters”This topic is an important part of building a reliable Linux development workflow. Understanding it clearly will make later tasks easier, because it reduces guesswork and helps you recognize when a step is missing or misapplied.
Practical next steps
Section titled “Practical next steps”- Try the commands or configuration shown here in a safe test environment.
- Compare how the concepts apply across different distributions or tools.
- Keep a short note of what worked and what failed so you can diagnose future problems faster.
- Revisit the related article in the series to deepen the connections between topics.
Common pitfalls
Section titled “Common pitfalls”- Skipping verification steps and assuming the system is configured correctly.
- Copying commands without adapting paths, package names, or tool versions for your environment.
- Treating this topic as an isolated tip rather than part of a larger workflow.