# CMake Automatically Links Each New C File with Previous File - Have to Delete Cache for Every New File (CLion)

**URL:** https://discourse.cmake.org/t/cmake-automatically-links-each-new-c-file-with-previous-file-have-to-delete-cache-for-every-new-file-clion/15293
**Category:** Usage
**Tags:** os:macos
**Created:** [November 9, 2025, 8:41am UTC](https://discourse.cmake.org/t/cmake-automatically-links-each-new-c-file-with-previous-file-have-to-delete-cache-for-every-new-file-clion/15293 "2025-11-09T08:41:44Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Kunal](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kunal/32/5888_2.png) [@Kunal](https://discourse.cmake.org/u/Kunal)
#### Post date: [November 9, 2025, 8:41am UTC](https://discourse.cmake.org/t/cmake-automatically-links-each-new-c-file-with-previous-file-have-to-delete-cache-for-every-new-file-clion/15293/1 "2025-11-09T08:41:44Z")

</div>

**Note: I’m new to coding and CMake, so I might be missing something obvious. Please be patient with me!**

I’m working on a C programming practice project in CLion where I have multiple independent `.c` files (q1.c, q2.c, q3.c, etc.) in a `PRACTICE/` folder. Each file contains its own `main()` function and should compile into a separate, independent executable.

**Problem:** Every time I create a new `.c` file, CMake automatically links it with the previous file I was working on, causing “duplicate symbol ‘\_main’” linker errors. For example:

- When building `q12`, it links both `q12.c.o` and `q13.c.o`

- When building `q11`, it linked both `q11.c.o` and `q10.c.o`

**Error Message:**

```auto
FAILED: bin/q12 
: && /usr/bin/cc -g -arch arm64 -Wl,-search_paths_first -Wl,-headerpad_max_install_names  
CMakeFiles/q12.dir/PRACTICE/q12.c.o CMakeFiles/q12.dir/PRACTICE/q13.c.o -o bin/q12 && :
duplicate symbol '_main' in:
    CMakeFiles/q12.dir/PRACTICE/q13.c.o
    CMakeFiles/q12.dir/PRACTICE/q12.c.o
ld: 1 duplicate symbols
clang: error: linker command failed with exit code 1 (use -v to see invocation)

```

**My CMakeLists.txt:**

cmake

```auto
cmake_minimum_required(VERSION 3.16)
project(SEM_1 C)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

file(GLOB PRACTICE_SOURCES "${CMAKE_SOURCE_DIR}/PRACTICE/*.c")

set(EXCLUDE_EXECUTABLES "common" "shared_helpers")

foreach(src IN LISTS PRACTICE_SOURCES)
    get_filename_component(name ${src} NAME_WE)
    list(FIND EXCLUDE_EXECUTABLES ${name} _idx)
    if(_idx EQUAL -1)
        add_executable(${name} ${src})
    else()
        message(STATUS "Skipping ${name} (excluded)")
    endif()
endforeach()

```

**What I’ve tried:**

1. Deleted `cmake-build-debug` folder and reloaded CMake

2. Used `Tools → CMake → Reset Cache and Reload Project`

3. Verified that `add_executable(${name} ${src})` only references `${src}` with no hardcoded files

4. Cleaned and rebuilt the project multiple times

5. Invalidated CLion caches and restarted

**The frustrating part:** These steps DO work and fix the issue temporarily, but I have to repeat this entire process (deleting cache, reloading CMake, cleaning build) **for every single new file I create**. When I create q14.c, it will link with q13.c again, and I’ll have to go through the whole cleanup process once more. This is extremely frustrating and time-consuming when I’m just trying to practice coding problems.

**Environment:**

- IDE: CLion (macOS, Apple Silicon)

- CMake version: 3.16+

- Compiler: Apple Clang

- Build system: Ninja

**Expected behavior:** Each executable should be built from only its own source file automatically. `q12` should only link `q12.c.o`, not `q13.c.o`. And when I create a new file, it should just work without requiring manual cache deletion.

**Question:** Is there a way to configure CMake or CLion to automatically handle this properly so I don’t have to manually delete caches and reload for every single new file? Why does CMake keep persisting these incorrect file associations, and how can I make the build system truly independent for each source file without manual intervention each time?

I’m learning and would really appreciate any guidance on what I might be doing wrong or what steps I should take to automate this properly. Thank you!

---

<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: [November 9, 2025, 11:25am UTC](https://discourse.cmake.org/t/cmake-automatically-links-each-new-c-file-with-previous-file-have-to-delete-cache-for-every-new-file-clion/15293/2 "2025-11-09T11:25:05Z")

</div>

This is either a user issue or a clion issue.

---

<div class="post-metadata">

### Author: ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)
#### Post date: [November 10, 2025, 10:04am UTC](https://discourse.cmake.org/t/cmake-automatically-links-each-new-c-file-with-previous-file-have-to-delete-cache-for-every-new-file-clion/15293/3 "2025-11-10T10:04:13Z")

</div>

First of all - globbing for sources is not recommended. I kinda understand why you do it here, but just so you know. It has problems when the files are added or removed without re-running cmake.

Anyway. I tried it with a command line only, and it works as expected. I does not seem possible (and I tried to break it, even with an in-source build), but I’m on Linux.

My advice would be to try with a command line only to elliminate at least one factor (CLion)

---

<div class="post-metadata">

### Author: ![Kunal](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kunal/32/5888_2.png) [@Kunal](https://discourse.cmake.org/u/Kunal)
#### Post date: [November 19, 2025, 4:07pm UTC](https://discourse.cmake.org/t/cmake-automatically-links-each-new-c-file-with-previous-file-have-to-delete-cache-for-every-new-file-clion/15293/4 "2025-11-19T16:07:40Z")

</div>

I don’t know anything about it. I set it up with help from ChatGPT. If I stop using glob, I will have to do it manually. Do you know any alternative to do it automatically?

---

<div class="post-metadata">

### Author: ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)
#### Post date: [November 20, 2025, 9:31am UTC](https://discourse.cmake.org/t/cmake-automatically-links-each-new-c-file-with-previous-file-have-to-delete-cache-for-every-new-file-clion/15293/5 "2025-11-20T09:31:56Z")

</div>

TBH, I always list all the files explicitly. It’s the most predictable and clear way. Especially for you, a beginner, I’d recommend using a dead-simple approach.  
Or, at least something half-automatic, like:

```cmake
cmake_minimum_required(VERSION 3.16)
project(SEM_1 C)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

# targets will be build from C-files with the same name
set(TARGETS_TO_BUILD q11 q12 q13)

foreach(name IN LISTS TARGETS_TO_BUILD)
        add_executable(${name} PRACTICE/${name}.c)
endforeach()

```

Here you just add stuff to the `TARGETS_TO_BUILD` list.
