[CMake] gnu-arm-embedded toolchain and CMAKE_FORCE_C_COMPILER

Wagner Martin Martin.Wagner at neuberger.net
Wed Mar 29 03:10:05 EDT 2017


Hi @all,

In recent CMake versions I get a deprecated message for using the cmake_force_xxx_compiler() function inside my toolchain file. After searching, I found that there are two threads on the CMake list, one
https://cmake.org/pipermail/cmake/2016-February/062773.html
just ending without a solution and referencing to
https://cmake.org/pipermail/cmake-developers/2016-February/027888.html
which also doesn’t really tell me how this is supposed to work in my case. 
From those threads, I was able to modify my toolchain file:
- replaced force_compiler function with regular set(CMAKE...
- added line set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) as linking needs a linker script.
However, it fails with the following output:

exit.c:(.text.exit+0x1a): undefined reference to `_exit'
collect2: error: ld returned 1 exit status

which to me indicates that CMake ignores the STATIC_LIBRARY flag and tries linking.

Has anyone successfully modified their toolchain file to correctly detect the gnu-arm-embedded toolchain?

Thanks for your help!

Regards,
Martin

Terminal output:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- The C compiler identification is GNU 6.2.1
-- The CXX compiler identification is GNU 6.2.1
-- Check for working C compiler: /opt/gcc-arm-freertos-eabi-6_2-2017q1/bin/arm-freertos-eabi-gcc
CMake Error at /home/user/workspace/project/buildsupport/toolchains/toolchain_stm32f303xB_freertos.cmake:37 (include):
  include could not find load file:

    /home/user/workspace/project/build/release/arm-cortex-m4f_stm32f303/CMakeFiles/CMakeTmp/buildsupport/toolchains/toolchain_arm-cortex-m4f.cmake
Call Stack (most recent call first):
  /home/user/workspace/project/build/release/arm-cortex-m4f_stm32f303/CMakeFiles/3.7.2/CMakeSystem.cmake:6 (include)
  /home/user/workspace/project/build/release/arm-cortex-m4f_stm32f303/CMakeFiles/CMakeTmp/CMakeLists.txt:2 (project)


CMake Error: Internal CMake error, TryCompile configure of cmake failed
-- Check for working C compiler: /opt/gcc-arm-freertos-eabi-6_2-2017q1/bin/arm-freertos-eabi-gcc -- broken
CMake Error at /usr/share/cmake-3.7/Modules/CMakeTestCCompiler.cmake:51 (message):
  The C compiler
  "/opt/gcc-arm-freertos-eabi-6_2-2017q1/bin/arm-freertos-eabi-gcc" is not
  able to compile a simple test program.

  It fails with the following output:

   

  
-- Configuring incomplete, errors occurred!
See also "/home/user/workspace/project/build/release/arm-cortex-m4f_stm32f303/CMakeFiles/CMakeOutput.log".
See also "/home/user/workspace/project/build/release/arm-cortex-m4f_stm32f303/CMakeFiles/CMakeError.log".

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

The target system is: Generic -  - arm-freertos-eabi
The host system is: Linux - 4.9.0-1-amd64 - x86_64
Compiling the C compiler identification source file "CMakeCCompilerId.c" succeeded.
Compiler: /opt/gcc-arm-freertos-eabi-6_2-2017q1/bin/arm-freertos-eabi-gcc 
Build flags: ;-DSTM32F303xC;-mcpu=cortex-m4;-mfpu=fpv4-sp-d16;-mfloat-abi=hard;-mthumb;--specs=nano.specs;-D_REENT_SMALL;
Id flags: -c

The output was:
0


Compilation of the C compiler identification source "CMakeCCompilerId.c" produced "CMakeCCompilerId.o"

The C compiler identification is GNU, found in "/home/user/workspace/project/build/release/arm-cortex-m4f_stm32f303/CMakeFiles/3.7.2/CompilerIdC/CMakeCCompilerId.o"

Compiling the CXX compiler identification source file "CMakeCXXCompilerId.cpp" succeeded.
Compiler: /opt/gcc-arm-freertos-eabi-6_2-2017q1/bin/arm-freertos-eabi-g++ 
Build flags: ;-DSTM32F303xC;-mcpu=cortex-m4;-mfpu=fpv4-sp-d16;-mfloat-abi=hard;-mthumb;--specs=nano.specs;-D_REENT_SMALL;
Id flags: -c

The output was:
0


Compilation of the CXX compiler identification source "CMakeCXXCompilerId.cpp" produced "CMakeCXXCompilerId.o"

The CXX compiler identification is GNU, found in "/home/user/workspace/project/build/release/arm-cortex-m4f_stm32f303/CMakeFiles/3.7.2/CompilerIdCXX/CMakeCXXCompilerId.o"

Compiling the C compiler identification source file "CMakeCCompilerId.c" succeeded.
Compiler: /opt/gcc-arm-freertos-eabi-6_2-2017q1/bin/arm-freertos-eabi-gcc 
Build flags: ;-DSTM32F303xC;-mcpu=cortex-m4;-mfpu=fpv4-sp-d16;-mfloat-abi=hard;-mthumb;--specs=nano.specs;-D_REENT_SMALL;
Id flags: -c

The output was:
0


Compilation of the C compiler identification source "CMakeCCompilerId.c" produced "CMakeCCompilerId.o"

The C compiler identification is GNU, found in "/home/user/workspace/project/build/release/arm-cortex-m4f_stm32f303/CMakeFiles/3.7.2/CompilerIdC/CMakeCCompilerId.o"

Compiling the CXX compiler identification source file "CMakeCXXCompilerId.cpp" succeeded.
Compiler: /opt/gcc-arm-freertos-eabi-6_2-2017q1/bin/arm-freertos-eabi-g++ 
Build flags: ;-DSTM32F303xC;-mcpu=cortex-m4;-mfpu=fpv4-sp-d16;-mfloat-abi=hard;-mthumb;--specs=nano.specs;-D_REENT_SMALL;
Id flags: -c

The output was:
0


Compilation of the CXX compiler identification source "CMakeCXXCompilerId.cpp" produced "CMakeCXXCompilerId.o"

The CXX compiler identification is GNU, found in "/home/user/workspace/project/build/release/arm-cortex-m4f_stm32f303/CMakeFiles/3.7.2/CompilerIdCXX/CMakeCXXCompilerId.o"

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CMakeError.log
Compiling the C compiler identification source file "CMakeCCompilerId.c" failed.
Compiler: /opt/gcc-arm-freertos-eabi-6_2-2017q1/bin/arm-freertos-eabi-gcc 
Build flags: ;-DSTM32F303xC;-mcpu=cortex-m4;-mfpu=fpv4-sp-d16;-mfloat-abi=hard;-mthumb;--specs=nano.specs;-D_REENT_SMALL;
Id flags: 

The output was:
1
/opt/gcc-arm-freertos-eabi-6_2-2017q1/bin/../lib/gcc/arm-freertos-eabi/6.2.1/../../../../arm-freertos-eabi/lib/thumb/v7e-m/fpv4-sp/hard/libc_nano.a(lib_a-exit.o): In function `exit':
exit.c:(.text.exit+0x1a): undefined reference to `_exit'
collect2: error: ld returned 1 exit status


Compiling the CXX compiler identification source file "CMakeCXXCompilerId.cpp" failed.
Compiler: /opt/gcc-arm-freertos-eabi-6_2-2017q1/bin/arm-freertos-eabi-g++ 
Build flags: ;-DSTM32F303xC;-mcpu=cortex-m4;-mfpu=fpv4-sp-d16;-mfloat-abi=hard;-mthumb;--specs=nano.specs;-D_REENT_SMALL;
Id flags: 

The output was:
1
/opt/gcc-arm-freertos-eabi-6_2-2017q1/bin/../lib/gcc/arm-freertos-eabi/6.2.1/../../../../arm-freertos-eabi/lib/thumb/v7e-m/fpv4-sp/hard/libc_nano.a(lib_a-exit.o): In function `exit':
exit.c:(.text.exit+0x1a): undefined reference to `_exit'
collect2: error: ld returned 1 exit status


Determining if the C compiler works failed with the following output:


Compiling the C compiler identification source file "CMakeCCompilerId.c" failed.
Compiler: /opt/gcc-arm-freertos-eabi-6_2-2017q1/bin/arm-freertos-eabi-gcc 
Build flags: ;-DSTM32F303xC;-mcpu=cortex-m4;-mfpu=fpv4-sp-d16;-mfloat-abi=hard;-mthumb;--specs=nano.specs;-D_REENT_SMALL;
Id flags: 

The output was:
1
/opt/gcc-arm-freertos-eabi-6_2-2017q1/bin/../lib/gcc/arm-freertos-eabi/6.2.1/../../../../arm-freertos-eabi/lib/thumb/v7e-m/fpv4-sp/hard/libc_nano.a(lib_a-exit.o): In function `exit':
exit.c:(.text.exit+0x1a): undefined reference to `_exit'
collect2: error: ld returned 1 exit status


Compiling the CXX compiler identification source file "CMakeCXXCompilerId.cpp" failed.
Compiler: /opt/gcc-arm-freertos-eabi-6_2-2017q1/bin/arm-freertos-eabi-g++ 
Build flags: ;-DSTM32F303xC;-mcpu=cortex-m4;-mfpu=fpv4-sp-d16;-mfloat-abi=hard;-mthumb;--specs=nano.specs;-D_REENT_SMALL;
Id flags: 

The output was:
1
/opt/gcc-arm-freertos-eabi-6_2-2017q1/bin/../lib/gcc/arm-freertos-eabi/6.2.1/../../../../arm-freertos-eabi/lib/thumb/v7e-m/fpv4-sp/hard/libc_nano.a(lib_a-exit.o): In function `exit':
exit.c:(.text.exit+0x1a): undefined reference to `_exit'
collect2: error: ld returned 1 exit status


Determining if the C compiler works failed with the following output:

~~~~eof, no output~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Toolchain file:

# FreeRTOS custom  toolchain
set(target_os freertos)
set(target_toolchain /opt/gcc-arm-${target_os}-eabi-6_2-2017q1/bin)

# Linker file
set(EXE_LINKER_FLAGS "${EXE_LINKER_FLAGS} -T path/to/linker/file/STM32F303CBTx_FLASH.ld")

set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) #can’t link without linker file

## specify the cross compiler
set(CROSSTOOLCHAIN_PATH ${target_toolchain})
set(CMAKE_SYSTEM_PROCESSOR arm-${target_os}-eabi)
set(CROSSTOOLCHAIN_PREFIX ${CMAKE_SYSTEM_PROCESSOR}-)
#cmake_force_c_compiler(${CROSSTOOLCHAIN_PATH}/${CROSSTOOLCHAIN_PREFIX}gcc GNU )
#cmake_force_cxx_compiler(${CROSSTOOLCHAIN_PATH}/${CROSSTOOLCHAIN_PREFIX}g++ GNU )
set(CMAKE_C_COMPILER   ${CROSSTOOLCHAIN_PATH}/${CROSSTOOLCHAIN_PREFIX}gcc)
set(CMAKE_CXX_COMPILER ${CROSSTOOLCHAIN_PATH}/${CROSSTOOLCHAIN_PREFIX}g++)

## where is the target environment 
set(CMAKE_FIND_ROOT_PATH  ${CROSSTOOLCHAIN_PATH})

## search for programs in the build host directories
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
## for libraries and headers in the target directories
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

# Cortex M4 Compilerflags
# gcc-arm-none-eabi-4_9-2015q3/share/doc/gcc-arm-none-eabi/readme.txt
set(C_FLAGS "${C_FLAGS} -mcpu=cortex-m4")
set(C_FLAGS "${C_FLAGS} -mfpu=fpv4-sp-d16")
set(C_FLAGS "${C_FLAGS} -mfloat-abi=hard")
set(C_FLAGS "${C_FLAGS} -mthumb")

# newlib Nano. REENT_SMALL needed for FreeRTOS!
set(C_FLAGS "${C_FLAGS} --specs=nano.specs -D_REENT_SMALL ")
# Bare Metal Target
set(EXE_LINKER_FLAGS "${EXE_LINKER_FLAGS} --specs=rdimon.specs -lrdimon")

set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} ${C_FLAGS} CACHE STRING "Cortex M4 C Flags")
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} ${C_FLAGS} CACHE STRING "Cortex M4 C++ Flags")
set(CMAKE_ASM_FLAGS ${CMAKE_ASM_FLAGS} ${C_FLAGS} CACHE STRING "Cortex M4 asm Flags")
set(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} ${EXE_LINKER_FLAGS} CACHE STRING "Cortex M4 Linker Flags")


More information about the CMake mailing list