# A method of writing that can handle file name changes.

**URL:** https://discourse.cmake.org/t/a-method-of-writing-that-can-handle-file-name-changes/15825
**Category:** Code
**Tags:** os:linux, os:windows, gen:ninja
**Created:** [September 9, 2026, 11:39am UTC](https://discourse.cmake.org/t/a-method-of-writing-that-can-handle-file-name-changes/15825 "2026-09-09T11:39:23Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![amagi](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/9d8465/32.png) [@amagi](https://discourse.cmake.org/u/amagi)
#### Post date: [September 9, 2026, 11:39am UTC](https://discourse.cmake.org/t/a-method-of-writing-that-can-handle-file-name-changes/15825/1 "2026-09-09T11:39:23Z")

</div>

The following code requires a `cmake --build build` command to build if the filename is changed. However, emptying and regenerating the build directory is cumbersome, so I’ve assigned a single command to a shortcut key for automation.

## Problem: Each run takes over 5 seconds to build, slowing down development.

## What I want to know: How can I automate the build process with a single, perfect command, even when changing filenames or other modifications?

```auto
"cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Debug && cmake --build build && ./build/app"

```

## CMakeLists

```auto
cmake_minimum_required(VERSION 3.15)

# compiler
set(CMAKE_CXX_COMPILER "clang++")
set(CMAKE_C_COMPILER "clang")

# project
set(PROJECT_NAME "app")

set(CMAKE_CXX_STANDARD 26)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

set(PROJECT_VERSION "1.0.0")
project(${PROJECT_NAME} VERSION ${PROJECT_VERSION})

#####################################################################
# test project
#####################################################################

set(EXE_NAME "app")
set(EXE_SOURCE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/src")
set(EXE_INCLUDE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/include")

file(GLOB_RECURSE EXE_SOURCE_LIST CONFIGURE_DEPENDS "${EXE_SOURCE_PATH}/*")

add_executable(${EXE_NAME} ${EXE_SOURCE_LIST})
target_include_directories(${EXE_NAME} PRIVATE "${EXE_INCLUDE_PATH}")

target_link_libraries(${EXE_NAME} PRIVATE "raylib")
target_link_libraries(${EXE_NAME} PRIVATE "box2d")
target_link_libraries(${EXE_NAME} PRIVATE "LDtkLoader")
target_link_libraries(${EXE_NAME} PRIVATE "GL")
target_link_libraries(${EXE_NAME} PRIVATE "m")
target_link_libraries(${EXE_NAME} PRIVATE "pthread")
target_link_libraries(${EXE_NAME} PRIVATE "dl")
target_link_libraries(${EXE_NAME} PRIVATE "rt")
target_link_libraries(${EXE_NAME} PRIVATE "X11")

```

---

<div class="post-metadata">

### Author: ![hsattler](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@hsattler](https://discourse.cmake.org/u/hsattler)
#### Post date: [September 9, 2026, 12:44pm UTC](https://discourse.cmake.org/t/a-method-of-writing-that-can-handle-file-name-changes/15825/2 "2026-09-09T12:44:55Z")

</div>

Don’t glob the list of source files.

---

<div class="post-metadata">

### Author: ![amagi](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/9d8465/32.png) [@amagi](https://discourse.cmake.org/u/amagi)
#### Post date: [September 9, 2026, 12:54pm UTC](https://discourse.cmake.org/t/a-method-of-writing-that-can-handle-file-name-changes/15825/3 "2026-09-09T12:54:53Z")

</div>

So what should we do?

---

<div class="post-metadata">

### Author: ![hsattler](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@hsattler](https://discourse.cmake.org/u/hsattler)
#### Post date: [September 9, 2026, 4:50pm UTC](https://discourse.cmake.org/t/a-method-of-writing-that-can-handle-file-name-changes/15825/4 "2026-09-09T16:50:31Z")

</div>

You could use an IDE that supports CMake. Usually, renaming files and updating the target source file list in cmake files is also supported. OTOH, how often do you rename source code files? Strange work flow with enough overhead (other code files, vcs, etc.) that even also updating the file name in cmake files should not be much to require from developers.

Unless you are in a hostile environment or actually another build system is used instead? Sounds like a non-technical problem, then. But that’s just guessing from my side. Maybe you could clarify the problem that leads to this problem?

Globbing (at cmake configure time or once beforehand) should at best only be used for e.g. code generation with unpredictable file names. Or you have to live it’s downsides.

---

<div class="post-metadata">

### Author: ![amagi](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/9d8465/32.png) [@amagi](https://discourse.cmake.org/u/amagi)
#### Post date: [September 10, 2026, 5:37am UTC](https://discourse.cmake.org/t/a-method-of-writing-that-can-handle-file-name-changes/15825/5 "2026-09-10T05:37:37Z")

</div>

I’m a Neovim user and I’ve set up keybindings to automate commands.  
I created two new commands, one for generating and building, and another for building only, and I checked the execution time for both. They both take the same amount of time. Is this a problem other than CMake?

## Current Situation

cmake --build build  
cmake -S . -B build  
both take the same amount of time, approximately 8 seconds.

 ![Screenshot From 2026-09-10 14-32-10](https://discourse.cmake.org/uploads/default/original/2X/2/2850e2c18fb44a46092fb82c19599b87103b15df.png)

---

<div class="post-metadata">

### Author: ![amagi](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/9d8465/32.png) [@amagi](https://discourse.cmake.org/u/amagi)
#### Post date: [September 12, 2026, 2:54am UTC](https://discourse.cmake.org/t/a-method-of-writing-that-can-handle-file-name-changes/15825/6 "2026-09-12T02:54:38Z")

</div>

# Self-resolved

## Cause

Compilation time was long because everything was written only in the header file.

## Solution

Separate the header and source files.
