# cmake with arm-none-eabi-gcc doesn't include/link default headers and libraries

**URL:** https://discourse.cmake.org/t/cmake-with-arm-none-eabi-gcc-doesnt-include-link-default-headers-and-libraries/13254
**Category:** Code
**Tags:** os:windows, comp:gcc, gen:ninja
**Created:** [December 23, 2024, 9:50am UTC](https://discourse.cmake.org/t/cmake-with-arm-none-eabi-gcc-doesnt-include-link-default-headers-and-libraries/13254 "2024-12-23T09:50:34Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![b.e.nazarenko](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/8dc957/32.png) [@b.e.nazarenko](https://discourse.cmake.org/u/b.e.nazarenko)
#### Post date: [December 23, 2024, 9:50am UTC](https://discourse.cmake.org/t/cmake-with-arm-none-eabi-gcc-doesnt-include-link-default-headers-and-libraries/13254/1 "2024-12-23T09:50:34Z")

</div>

Moving my project from eclipse to cmake. Made a simple arm-none-eabi toolchain file (got it from STM32 CubeMX), and it works fine. But when I tried to use anything from root includes (math.h for example), it didn’t work. I get errors like this:

```auto
[build] c:/toolchain/sysgcc/arm-eabi/bin/../lib/gcc/arm-none-eabi/12.2.1/../../../../arm-none-eabi/bin/ld.exe: CMakeFiles/cmake_move.dir/src/utils.c.obj: in function `dBmToWatt':
[build] C:/Users/ben/Projects/cmake_move/src/utils.c:58: undefined reference to `pow'

```

I have no idea what’s wrong. The toolchain file looks like this:

```cmake
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR arm)

set(CMAKE_C_COMPILER_FORCED TRUE)
set(CMAKE_CXX_COMPILER_FORCED TRUE)
set(CMAKE_C_COMPILER_ID GNU)
set(CMAKE_CXX_COMPILER_ID GNU)

# Some default GCC settings
# arm-none-eabi- must be part of path environment
set(TOOLCHAIN_PREFIX arm-none-eabi-)

set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}gcc)
set(CMAKE_ASM_COMPILER ${CMAKE_C_COMPILER})
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}g++)
set(CMAKE_LINKER ${TOOLCHAIN_PREFIX}g++)
set(CMAKE_OBJCOPY ${TOOLCHAIN_PREFIX}objcopy)
set(CMAKE_SIZE ${TOOLCHAIN_PREFIX}size)

set(CMAKE_EXECUTABLE_SUFFIX_ASM ".elf")
set(CMAKE_EXECUTABLE_SUFFIX_C ".elf")
set(CMAKE_EXECUTABLE_SUFFIX_CXX ".elf")

set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

set(TARGET_FLAGS "-mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard ")

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${TARGET_FLAGS}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fmessage-length=0 -ffunction-sections -fdata-sections -ffreestanding -fno-move-loop-invariants")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wpedantic")
if(CMAKE_BUILD_TYPE MATCHES Debug)
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -g3")
endif()
if(CMAKE_BUILD_TYPE MATCHES Release)
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Os -g0")
endif()

set(CMAKE_ASM_FLAGS "${CMAKE_C_FLAGS} -x assembler-with-cpp -MMD -MP")
set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -fno-rtti -fno-exceptions -fno-threadsafe-statics")

set(CMAKE_C_LINK_FLAGS "${TARGET_FLAGS}")
set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -T \"${CMAKE_SOURCE_DIR}/ldscripts/gd32f407_linkerScript.ld\"")
set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} --specs=nano.specs")
set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -Wl,-Map=${CMAKE_PROJECT_NAME}.map -Wl,--gc-sections")
set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -Wl,--start-group -lc -lm -Wl,--end-group")
set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -Wl,--print-memory-usage")

set(CMAKE_CXX_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -Wl,--start-group -lstdc++ -lsupc++ -Wl,--end-group")

```

---

<div class="post-metadata">

### Author: ![Trampas\_Stern](https://discourse.cmake.org/user_avatar/discourse.cmake.org/trampas_stern/32/5212_2.png) [@Trampas\_Stern](https://discourse.cmake.org/u/Trampas_Stern)
#### Post date: [December 23, 2024, 4:29pm UTC](https://discourse.cmake.org/t/cmake-with-arm-none-eabi-gcc-doesnt-include-link-default-headers-and-libraries/13254/2 "2024-12-23T16:29:09Z")

</div>

> [@b.e.nazarenko](#):
>
> `arm-none-eabi/bin/ld.exe:`

It looks like your cmake file is not being used. That is the command line you sent used ld.exe not arm-none-eabi-g++ for the linker. Try deleting your build directory and have cmake regenerate the the makefiles (build files).

---

<div class="post-metadata">

### Author: ![b.e.nazarenko](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/8dc957/32.png) [@b.e.nazarenko](https://discourse.cmake.org/u/b.e.nazarenko)
#### Post date: [December 24, 2024, 6:38am UTC](https://discourse.cmake.org/t/cmake-with-arm-none-eabi-gcc-doesnt-include-link-default-headers-and-libraries/13254/3 "2024-12-24T06:38:45Z")

</div>

Interesting, thanks.  
But the file is being used. I tried changing c compiler to clang and it changed everywhere. Removing the CMAKE\_LINKER option adds this to CMakeCache.txt:

```auto
//Path to a program.
CMAKE_LINKER:FILEPATH=C:/Toolchain/SysGCC/arm-eabi/bin/arm-none-eabi-ld.exe

```

But during build it still uses bin/ld.exe. Not sure what’s causing this (I start to think that I have to add specific compiler options).  
**UPD:**  
Printing CMAKE\_C\_LINK\_EXECUTABLE and CMAKE\_LINKER gives me this:

```auto
[cmake] <CMAKE_C_COMPILER> <FLAGS> <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>
[cmake] C:/Toolchain/SysGCC/arm-eabi/bin/arm-none-eabi-g++.exe

```

So according to cmake it should use compiler as linker. Setting this in my toolchain solved the problem:

```auto
set(CMAKE_C_LINK_EXECUTABLE "<CMAKE_LINKER> <FLAGS> <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")

```
