1

I am trying to use arm-none-eabi-gcc with CMake but am getting an error about an undefined reference to "_exit". The following is the terminal output after running cmake (the first line is the command):

cmake --no-warn-unused-cli -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_C_COMPILER:FILEPATH=F:\Resources\Dev\Tools\GNU-arm\10.3-2021.07\bin\arm-none-eabi-gcc.exe -DCMAKE_CXX_COMPILER:FILEPATH=F:\Resources\Dev\Tools\GNU-arm\10.3-2021.07\bin\arm-none-eabi-g++.exe -Hf:/Projects/Dev/VS-Code/CPP/CPP-Sandbox "-Bf:/Projects/Dev/VS-Code/CPP/CPP-Sandbox/build/GCC 10.3.1 arm-none-eabi/Debug" -G Ninja
Not searching for unused variables given on the command line.
-- The C compiler identification is GNU 10.3.1
-- Check for working C compiler: F:/Resources/Dev/Tools/GNU-arm/10.3-2021.07/bin/arm-none-eabi-gcc.exe
-- Check for working C compiler: F:/Resources/Dev/Tools/GNU-arm/10.3-2021.07/bin/arm-none-eabi-gcc.exe - broken
CMake Error at C:/Program Files/CMake/share/cmake-3.17/Modules/CMakeTestCCompiler.cmake:60 (message):
  The C compiler

    "F:/Resources/Dev/Tools/GNU-arm/10.3-2021.07/bin/arm-none-eabi-gcc.exe"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: F:/Projects/Dev/VS-Code/CPP/CPP-Sandbox/build/GCC 10.3.1 arm-none-eabi/Debug/CMakeFiles/CMakeTmp

    Run Build Command(s):F:/Resources/Dev/Tools/MinGW/MinGW-9.0.0-x64/bin/ninja.exe cmTC_1a12b && [1/2] Building C object CMakeFiles/cmTC_1a12b.dir/testCCompiler.c.obj
    [2/2] Linking C executable cmTC_1a12b
    FAILED: cmTC_1a12b
    cmd.exe /C "cd . && F:\Resources\Dev\Tools\GNU-arm\10.3-2021.07\bin\arm-none-eabi-gcc.exe    CMakeFiles/cmTC_1a12b.dir/testCCompiler.c.obj  -o cmTC_1a12b   && cd ."
    f:/resources/dev/tools/gnu-arm/10.3-2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: f:/resources/dev/tools/gnu-arm/10.3-2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/lib\libc.a(lib_a-exit.o): in function `exit':
    exit.c:(.text.exit+0x2c): undefined reference to `_exit'
    collect2.exe: error: ld returned 1 exit status
    ninja: build stopped: subcommand failed.





  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:41 (project)


-- Configuring incomplete, errors occurred!
See also "F:/Projects/Dev/VS-Code/CPP/CPP-Sandbox/build/GCC 10.3.1 arm-none-eabi/Debug/CMakeFiles/CMakeOutput.log".
See also "F:/Projects/Dev/VS-Code/CPP/CPP-Sandbox/build/GCC 10.3.1 arm-none-eabi/Debug/CMakeFiles/CMakeError.log".

I have tried the following solution (i.e. adding simply adding -specs=nosys.specs to my CMakeLists.txt but to no avail. Here is my CMakeLists.txt:


cmake_minimum_required(VERSION 3.12)
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_C_STANDARD 99)

# Project variables
set(LOCAL_PROJECT_NAME        "C-Sandbox")
set(LOCAL_PROJECT_VERSION     "0.0.1")
set(LOCAL_PROJECT_DESCRIPTION "Description")

# Project setup
project(${LOCAL_PROJECT_NAME}
        VERSION ${LOCAL_PROJECT_VERSION}
        DESCRIPTION ${LOCAL_PROJECT_DESCRIPTION}
        LANGUAGES C)

add_executable(${LOCAL_PROJECT_NAME})

target_sources(${LOCAL_PROJECT_NAME} PRIVATE src/main.c)
target_link_options(${LOCAL_PROJECT_NAME} PRIVATE "-specs=nosys.specs")
set_target_properties(${LOCAL_PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "bin")

Edit: When I cd into my source folder and run arm-none-eabi-gcc main.c -lc "-specs=nosys.specs" I get no error and an a.out file.

Gary Allen
  • 1,218
  • 1
  • 13
  • 28
  • 1
    Try building without CMake. Do you get the same error? – Thomas Matthews Sep 11 '21 at 18:43
  • When I cd into my source folder and run ```arm-none-eabi-gcc main.c -lc "-specs=nosys.specs"``` I get no error and an a.out file – Gary Allen Sep 11 '21 at 18:45
  • 1
    Adding `target_link_options(${LOCAL_PROJECT_NAME} PRIVATE "-specs=nosys.specs")` doesn't change what happens during the initial project creation and checking for a compiler. https://stackoverflow.com/questions/43781207/how-to-cross-compile-with-cmake-arm-none-eabi-on-windows – fdk1342 Sep 12 '21 at 14:37
  • Did you find a solution for this? – DavidA Dec 22 '21 at 11:43
  • 1
    An easy fix to this problem is to set `CMAKE_C_COMPILER_FORCED` and `CMAKE_CXX_COMPILER_FORCED` to true. There is no documentation about these two variables in CMAKE docs but I found them used in [Zephyr RTOS's CMAKE script](https://github.com/zephyrproject-rtos/zephyr/blob/zephyr-v2.7.1/cmake/app/boilerplate.cmake#L514-L516=). There is a comment that says they "Prevent CMake from testing the toolchain." – Rami Apr 18 '22 at 10:15
  • @Rami None of these work: ``` set(CMAKE_C_COMPILER_FORCED TRUE) set(CMAKE_CXX_COMPILER_FORCED TRUE) CMAKE_FORCE_C_COMPILER(arm-none-eabi-gcc GNU) CMAKE_FORCE_CXX_COMPILER(arm-none-eabi-gcc GNU) ``` – Paul Jurczak Jul 13 '22 at 02:16
  • @PaulJurczak [`CMAKE_FORCE_{LANG}_COMPILER`](https://cmake.org/cmake/help/latest/module/CMakeForceCompiler.html) is deprecated since 3.6. What you should use instead is [`set(CMAKE_{LANGE}_COMPILER ...)`](https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_COMPILER.html). This should definitely work on CMake 3.20 or higher. I haven't tested on older version but a quick look at the CMake sources show that the variables are available in versions as old as 3.0. – Rami Jul 14 '22 at 03:26
  • @PaulJurczak Since I can't answer the question here, if you are still running into issues, please post a question and tag me in the comment so I can answer it for you. – Rami Jul 14 '22 at 03:29

0 Answers0