Cmake
CMake Mastery
Section titled “CMake Mastery”CMake is the most popular build system generator for C++. It generates platform-specific build files from simple configuration files.
Basic CMake Project
Section titled “Basic CMake Project”MyProject/├── CMakeLists.txt├── main.cpp└── include/ └── utils.hMinimal CMakeLists.txt
Section titled “Minimal CMakeLists.txt”cmake_minimum_required(VERSION 3.10)project(MyProject)
set(CMAKE_CXX_STANDARD 17)set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(myapp main.cpp)Building
Section titled “Building”mkdir buildcd buildcmake ..make./myappVariables
Section titled “Variables”# Set variableset(MY_VAR "value")
# Use variable${MY_VAR}
# Listset(SOURCES main.cpp util.cpp helper.cpp)
# Cache variablesset(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")Multiple Source Files
Section titled “Multiple Source Files”cmake_minimum_required(VERSION 3.10)project(MyProject)
set(CMAKE_CXX_STANDARD 17)
# Collect all .cpp files automaticallyfile(GLOB SOURCES "src/*.cpp")
add_executable(myapp ${SOURCES})Headers and Include Directories
Section titled “Headers and Include Directories”include_directories(${CMAKE_SOURCE_DIR}/include)
add_executable(myapp main.cpp src/util.cpp src/helper.cpp)Targets-Based Commands (Modern CMake)
Section titled “Targets-Based Commands (Modern CMake)”add_executable(myapp main.cpp)
target_include_directories(myapp PRIVATE ${CMAKE_SOURCE_DIR}/include)
target_compile_features(myapp PRIVATE cxx_std_17)Libraries
Section titled “Libraries”# Static libraryadd_library(mylib STATIC src/lib.cpp)
# Shared libraryadd_library(myshared SHARED src/lib.cpp)
# Link libraryadd_executable(myapp main.cpp)target_link_libraries(myapp mylib)Finding Packages
Section titled “Finding Packages”# Find a packagefind_package(Threads REQUIRED)
# Use packageadd_executable(myapp main.cpp)target_link_libraries(myapp Threads::Threads)Common Packages
Section titled “Common Packages”# Boostfind_package(Boost REQUIRED)target_link_libraries(myapp Boost::filesystem)
# OpenSSLfind_package(OpenSSL REQUIRED)target_link_libraries(myapp OpenSSL::SSL OpenSSL::Crypto)
# Threadsfind_package(Threads REQUIRED)target_link_libraries(myapp Threads::Threads)Options
Section titled “Options”option(ENABLE_TESTS "Enable unit tests" ON)option(USE_OPENGL "Use OpenGL rendering" OFF)
if(ENABLE_TESTS) enable_testing() add_subdirectory(tests)endif()Complete Example
Section titled “Complete Example”cmake_minimum_required(VERSION 3.16)project(MyApp VERSION 1.0.0)
# C++ Standardset(CMAKE_CXX_STANDARD 17)set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Output directoriesset(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
# Find packagesfind_package(Threads REQUIRED)
# Source filesfile(GLOB SOURCES ${CMAKE_SOURCE_DIR}/src/*.cpp)
# Create executableadd_executable(myapp ${SOURCES})
# Include directoriestarget_include_directories(myapp PRIVATE ${CMAKE_SOURCE_DIR}/include)
# Link librariestarget_link_libraries(myapp Threads::Threads)
# Install rulesinstall(TARGETS myapp RUNTIME DESTINATION bin)Best Practices
Section titled “Best Practices”- Use modern CMake (3.10+)
- Prefer target_ commands over global commands*
- Use VERSION in project()
- Set C++ standard explicitly
- Use out-of-source builds
Key Takeaways
Section titled “Key Takeaways”- CMake generates build files from CMakeLists.txt
- Use target_* commands for better organization
- find_package locates external libraries
- Always use out-of-source builds
Next Steps
Section titled “Next Steps”Now let’s learn about testing in C++.
Next Chapter: 11_testing/52_unit_testing.md - Unit Testing with Google Test