Regenerate Build Files using CMake

Regenerate Build Files using CMake

When working on CMake-based projects, modifying the CMakeLists.txt files is a common task. Sometimes, these changes don't take effect because CMake caches values and settings from earlier runs. This tutorial demonstrates how to regenerate build files using CMake.

Consider a minimal CMake project defined in the CMakeLists.txt file:

cmake_minimum_required(VERSION 3.27)
project(myapp)

set(CMAKE_CXX_STANDARD 17)

add_executable(${PROJECT_NAME} main.cpp)

The standard way to generate build files is to use:

cmake -S . -B build
  • -S . - specifies the source directory (current directory).
  • -B build - tells CMake to place all build files in the build directory.

If you make changes to CMakeLists.txt or need to reset the build configuration (e.g., switching toolchain or fixing a bad cache), use the --fresh option:

cmake -S . -B build --fresh

This command initiates a clean configuration of the build directory by deleting the existing CMakeCache.txt file and the CMakeFiles directory, then regenerating them from scratch.

Leave a Comment

Cancel reply

Your email address will not be published.