arm-none-eabi link error undefined reference to `_exit' undefined reference to `_close' undefined reference to `_isatty' undefined reference to `_kill' undefined reference to `_getpid'

Hi all,
I got this error when trying to compile my code

[main] Building folder: test_cmake1_nc - Copy (2)
[build] Starting build
[proc] Executing command: "C:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" --build "c:/Users/rsridx/Desktop/test_cmake1_nc - Copy (2)/build" --config Debug --target all --
[build] [1/1 100% :: 0.229] Linking C executable Test
[build] FAILED: Test
[build] cmd.exe /C "cd . && C:\ST\STM32CubeCLT\GNU-tools-for-STM32\bin\arm-none-eabi-gcc.exe -g CMakeFiles/Test.dir/src/s1/helloWorld.c.obj CMakeFiles/Test.dir/src/s2/increment.c.obj -o Test && cd ."
[build] C:/ST/STM32CubeCLT/GNU-tools-for-STM32/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/bin/ld.exe: C:/ST/STM32CubeCLT/GNU-tools-for-STM32/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/lib\libg.a(libc_a-exit.o): in function `exit':
[build] /build/gnu-tools-for-stm32_11.3.rel1.20230912-1600/src/newlib/newlib/libc/stdlib/exit.c:65: undefined reference to `_exit'
[build] C:/ST/STM32CubeCLT/GNU-tools-for-STM32/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/bin/ld.exe: C:/ST/STM32CubeCLT/GNU-tools-for-STM32/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/lib\libg.a(libc_a-closer.o): in function `_close_r':
[build] /build/gnu-tools-for-stm32_11.3.rel1.20230912-1600/src/newlib/newlib/libc/reent/closer.c:47: undefined reference to `_close'
[build] C:/ST/STM32CubeCLT/GNU-tools-for-STM32/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/bin/ld.exe: C:/ST/STM32CubeCLT/GNU-tools-for-STM32/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/lib\libg.a(libc_a-lseekr.o): in function `_lseek_r':
[build] /build/gnu-tools-for-stm32_11.3.rel1.20230912-1600/src/newlib/newlib/libc/reent/lseekr.c:49: undefined reference to `_lseek'
[build] C:/ST/STM32CubeCLT/GNU-tools-for-STM32/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/bin/ld.exe: C:/ST/STM32CubeCLT/GNU-tools-for-STM32/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/lib\libg.a(libc_a-readr.o): in function `_read_r':
[build] /build/gnu-tools-for-stm32_11.3.rel1.20230912-1600/src/newlib/newlib/libc/reent/readr.c:49: undefined reference to `_read'
[build] C:/ST/STM32CubeCLT/GNU-tools-for-STM32/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/bin/ld.exe: C:/ST/STM32CubeCLT/GNU-tools-for-STM32/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/lib\libg.a(libc_a-writer.o): in function `_write_r':
[build] /build/gnu-tools-for-stm32_11.3.rel1.20230912-1600/src/newlib/newlib/libc/reent/writer.c:49: undefined reference to `_write'
[build] C:/ST/STM32CubeCLT/GNU-tools-for-STM32/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/bin/ld.exe: C:/ST/STM32CubeCLT/GNU-tools-for-STM32/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/lib\libg.a(libc_a-sbrkr.o): in function `_sbrk_r':
[build] /build/gnu-tools-for-stm32_11.3.rel1.20230912-1600/src/newlib/newlib/libc/reent/sbrkr.c:51: undefined reference to `_sbrk'
[build] C:/ST/STM32CubeCLT/GNU-tools-for-STM32/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/bin/ld.exe: C:/ST/STM32CubeCLT/GNU-tools-for-STM32/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/lib\libg.a(libc_a-fstatr.o): in function `_fstat_r':
[build] /build/gnu-tools-for-stm32_11.3.rel1.20230912-1600/src/newlib/newlib/libc/reent/fstatr.c:55: undefined reference to `_fstat'
[build] C:/ST/STM32CubeCLT/GNU-tools-for-STM32/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/bin/ld.exe: C:/ST/STM32CubeCLT/GNU-tools-for-STM32/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/lib\libg.a(libc_a-isattyr.o): in function `_isatty_r':
[build] /build/gnu-tools-for-stm32_11.3.rel1.20230912-1600/src/newlib/newlib/libc/reent/isattyr.c:52: undefined reference to `_isatty'
[build] C:/ST/STM32CubeCLT/GNU-tools-for-STM32/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/bin/ld.exe: C:/ST/STM32CubeCLT/GNU-tools-for-STM32/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/lib\libg.a(libc_a-abort.o): in function `abort':
[build] /build/gnu-tools-for-stm32_11.3.rel1.20230912-1600/src/newlib/newlib/libc/stdlib/abort.c:59: undefined reference to `_exit'
[build] C:/ST/STM32CubeCLT/GNU-tools-for-STM32/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/bin/ld.exe: C:/ST/STM32CubeCLT/GNU-tools-for-STM32/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/lib\libg.a(libc_a-signalr.o): in function `_kill_r':
[build] /build/gnu-tools-for-stm32_11.3.rel1.20230912-1600/src/newlib/newlib/libc/reent/signalr.c:53: undefined reference to `_kill'
[build] C:/ST/STM32CubeCLT/GNU-tools-for-STM32/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/bin/ld.exe: C:/ST/STM32CubeCLT/GNU-tools-for-STM32/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/lib\libg.a(libc_a-signalr.o): in function `_getpid_r':
[build] /build/gnu-tools-for-stm32_11.3.rel1.20230912-1600/src/newlib/newlib/libc/reent/signalr.c:83: undefined reference to `_getpid'
[build] collect2.exe: error: ld returned 1 exit status
[build] ninja: build stopped: subcommand failed.
[proc] The command: "C:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" --build "c:/Users/rsridx/Desktop/test_cmake1_nc - Copy (2)/build" --config Debug --target all -- exited with code: 1
[driver] Build completed: 00:00:00.301
[build] Build finished with exit code 1

Then I have added a command in my cmakelists.txt file as target_link_libraries(Test -Wl,--start-group -lgcc -lc -lm -lrdimon -Wl,--end-group) and this has resolved my linker issue!

[main] Configuring project: test_cmake1_nc - Copy (2)
[proc] Executing command: "C:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" --no-warn-unused-cli -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_C_COMPILER:FILEPATH=C:\ST\STM32CubeCLT\GNU-tools-for-STM32\bin\arm-none-eabi-gcc.exe -DCMAKE_CXX_COMPILER:FILEPATH=C:\ST\STM32CubeCLT\GNU-tools-for-STM32\bin\arm-none-eabi-g++.exe "-SC:/Users/rsridx/Desktop/test_cmake1_nc - Copy (2)" "-Bc:/Users/rsridx/Desktop/test_cmake1_nc - Copy (2)/build" -G Ninja
[cmake] Not searching for unused variables given on the command line.
[build] Starting build
[build] Build finished with exit code -1
[cmake] -- Configuring done (0.0s)
[cmake] -- Generating done (0.0s)
[cmake] -- Build files have been written to: C:/Users/rsridx/Desktop/test_cmake1_nc - Copy (2)/build

It looks like you have -nostdinc or -freestanding (or a similar flag) enabled somewhere. These flags turn off things like libc and other things; you’ll need to manage platform libraries manually in such a situation. Though if that is the full link line…maybe the toolchain is compiled to have that as the default behavior?

I have modified the CmakeLists.txt file as shown below
And this has resolved my error

cmake_minimum_required (VERSION 3.0.0)
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(CMAKE_C_COMPILER arm-none-eabi-gcc)
set(CMAKE_CXX_COMPILER arm-none-eabi-g++)
set(CMAKE_ASM_COMPILER arm-none-eabi-as)
set(CMAKE_LINKER arm-none-eabi-ld)
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
set(CMAKE_C_FLAGS “${CMAKE_C_FLAGS}”)
set(CMAKE_CXX_FLAGS “${CMAKE_CXX_FLAGS}”)
set(CMAKE_EXE_LINKER_FLAGS “${CMAKE_EXE_LINKER_FLAGS}”)
project (Test)
file(GLOB SRC_FILES #add source file paths
${CMAKE_CURRENT_SOURCE_DIR}/src/s1/.c
${CMAKE_CURRENT_SOURCE_DIR}/src/s2/
.c
)
file(GLOB INC_FILES #add include file paths
${CMAKE_CURRENT_SOURCE_DIR}/include/h1
${CMAKE_CURRENT_SOURCE_DIR}/include/h2
)
add_executable (Test ${SRC_FILES})
target_include_directories(Test PUBLIC ${INC_FILES})
target_link_libraries(Test -Wl,–start-group -lgcc -lc -lm -lrdimon -Wl,–end-group) #to resolve undefined reference to _exit etc errors
set_target_properties(Test PROPERTIES SUFFIX “.elf”) #to make elf output executable files
add_custom_command(TARGET Test POST_BUILD #post build is required for producing other output executable formats
COMMAND ${CMAKE_OBJCOPY} -O ihex $<TARGET_FILE:Test> ${CMAKE_CURRENT_BINARY_DIR}/Test.hex #to make hex output executable files

COMMAND ${CMAKE_OBJCOPY} -O binary $<TARGET_FILE:Test> ${CMAKE_CURRENT_BINARY_DIR}/Test.bin   #to make bin output executable files

)

FYI, a lot of that is best placed into a toolchain file.