# How do you prevent CMake from adding C flags to linker flags when linking with GCC?

**URL:** <https://discourse.cmake.org/t/how-do-you-prevent-cmake-from-adding-c-flags-to-linker-flags-when-linking-with-gcc/11324>\
**Category:** Code\
**Created:** [July 25, 2024, 1:32pm UTC](https://discourse.cmake.org/t/how-do-you-prevent-cmake-from-adding-c-flags-to-linker-flags-when-linking-with-gcc/11324 "2024-07-25T13:32:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jwrwseu](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@jwrwseu](https://discourse.cmake.org/u/jwrwseu)\
**Post date:** [July 25, 2024, 1:32pm UTC](https://discourse.cmake.org/t/how-do-you-prevent-cmake-from-adding-c-flags-to-linker-flags-when-linking-with-gcc/11324/1 "2024-07-25T13:32:02Z")

</div>

My toolchain:

```auto
set(CMAKE_C_FLAGS_INIT -D __ATmega64M1__ )
list(APPEND CMAKE_C_FLAGS_INIT -mmcu=atmega64m1)
list(APPEND CMAKE_C_FLAGS_INIT -x c)
list(APPEND CMAKE_C_FLAGS_INIT -funsigned-char)
list(APPEND CMAKE_C_FLAGS_INIT -funsigned-bitfields)
list(APPEND CMAKE_C_FLAGS_INIT -ffunction-sections)
list(APPEND CMAKE_C_FLAGS_INIT -fdata-sections)
list(APPEND CMAKE_C_FLAGS_INIT -fpack-struct)
list(APPEND CMAKE_C_FLAGS_INIT -fshort-enums)
list(APPEND CMAKE_C_FLAGS_INIT -Wall)
list(APPEND CMAKE_C_FLAGS_INIT -Wextra)
list(APPEND CMAKE_C_FLAGS_INIT -std=c11)
list(JOIN CMAKE_C_FLAGS_INIT " " CMAKE_C_FLAGS_INIT)

set(CMAKE_EXE_LINKER_FLAGS "-Wl,--gc-sections -static -Wl,--start-group -lc -lm -Wl,--end-group -Wl,--print-memory-usage")

```

When the linker is run on my executable target, the compiler binary `avr-gcc` is used and no matter what I try, it is using both the C flags and linker flags I set in the toolchain. I want the linker flags to be completely separate. How do you do this?

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [July 18, 2025, 8:33pm UTC](https://discourse.cmake.org/t/how-do-you-prevent-cmake-from-adding-c-flags-to-linker-flags-when-linking-with-gcc/11324/2 "2025-07-18T20:33:12Z")

</div>

I would make an `INTERFACE` target which `target_compile_options` the flags you want and then link that from everything.

```cmake
add_library(avrflags INTERFACE)
target_compile_definitions(avrflags INTERFACE __ATmega64M1__ )
target_compile_options(avrflags INTERFACE -mmcu=atmega64m1 …)

```

I would skip `-x c` and `-std=c11` and instead set the `LANGUAGE` and `C_STANDARD` properties on source files and targets as needed.
