13

I am having some troubles setting up cross compiling with CMAKE. The toolchain I am using is created in yocto which works perfectly outside of cmake.

I have followed a tutorial to setup the following toolchain file:

SET(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_SYSTEM_VERSION 1)
SET(CMAKE_SYSTEM_PROCESSOR arm)

# specify the cross compiler
SET(tools /opt/poky/1.7.1/sysroots/x86_64-pokysdk-linux/usr)
SET(CMAKE_C_COMPILER ${tools}/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gcc)
SET(CMAKE_CXX_COMPILER ${tools}/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gcc)

# set sysroot
SET(CMAKE_SYSROOT /home/sifu/test-yocto/qemuarmdfs)
#SET(CMAKE_FIND_ROOT_PATH /home/sifu/test-yocto/qemuarm)

# 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)
SET(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

And get the following error when running cmake

The C compiler
"/opt/poky/1.7.1/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gcc"
is not able to compile a simple test program.

It fails with the following output:

Change Dir: /home/sifu/Projects/mv/doublepump-single-pump-sw.ss016m21_swapp/cc/CMakeFiles/CMakeTmp

Run Build Command:/usr/bin/make "cmTryCompileExec4012536451/fast"

/usr/bin/make -f CMakeFiles/cmTryCompileExec4012536451.dir/build.make
CMakeFiles/cmTryCompileExec4012536451.dir/build

make[1]: Entering directory
`/home/sifu/Projects/mv/doublepump-single-pump-sw.ss016m21_swapp/cc/CMakeFiles/CMakeTmp'

/usr/bin/cmake -E cmake_progress_report
/home/sifu/Projects/mv/doublepump-single-pump-sw.ss016m21_swapp/cc/CMakeFiles/CMakeTmp/CMakeFiles
1

Building C object
CMakeFiles/cmTryCompileExec4012536451.dir/testCCompiler.c.o


/opt/poky/1.7.1/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gcc
-O2 -pipe -g -feliminate-unused-debug-types -o
CMakeFiles/cmTryCompileExec4012536451.dir/testCCompiler.c.o -c
/home/sifu/Projects/mv/doublepump-single-pump-sw.ss016m21_swapp/cc/CMakeFiles/CMakeTmp/testCCompiler.c

Linking C executable cmTryCompileExec4012536451

/usr/bin/cmake -E cmake_link_script
CMakeFiles/cmTryCompileExec4012536451.dir/link.txt --verbose=1

/opt/poky/1.7.1/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gcc
-O2 -pipe -g -feliminate-unused-debug-types -Wl,-O1 -Wl,--hash-style=gnu
-Wl,--as-needed CMakeFiles/cmTryCompileExec4012536451.dir/testCCompiler.c.o
-o cmTryCompileExec4012536451 -rdynamic

... (A lot of ld errors similar to the one below)

/opt/poky/1.7.1/sysroots/x86_64-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/4.9.1/ld:
cannot find crtn.o: No such file or directory

collect2: error: ld returned 1 exit status

make[1]: Leaving directory
`/home/sifu/Projects/mv/doublepump-single-pump-sw.ss016m21_swapp/cc/CMakeFiles/CMakeTmp'

If I manually run the gcc command described in the log above with --sysroot=/home/sifu/test-yocto/qemuarmdfs it works for me. Why isn't cmake using the sysroot flag when the path to sysroot is added in the toolchain file.

7
  • Looks like CMake fails to detect that compiler supports --sysroot option. Is line The C compiler identification is GNU output by CMake when configure? BTW, you show toolchain with errorneous space before "gnueabi-gcc" in CMAKE_C_COMPILER variable definition. This space is absent in your original toolchain file, isn't it?
    – Tsyvarev
    Mar 24, 2016 at 8:52
  • When configuring it detects the correct C compiler The C compiler identification is GNU 4.9.1.. The space in CMAKE_C_COMPILER was just a cut and paste issue, i have edited the original question. Mar 24, 2016 at 9:00
  • I am unsure, but probably the CMake project you tried to build modifies CMAKE_C_FLAGS somehow, so --sysroot option disappears. Could you try to build simple "Hello world" program with your toolchain?
    – Tsyvarev
    Mar 24, 2016 at 9:53
  • The toolchain works perfectly with hello world as long as I use --sysroot=/home/sifu/test-yocto/qemuarmdfs. Mar 24, 2016 at 10:26
  • Oops, sorry, I meant to test CMake toolchain file with simple CMake project (e.g., which builds "Hellow world" program via add_executable). For such project CMake should add --sysroot option automatically, and you can see it when build with make V=1.
    – Tsyvarev
    Mar 24, 2016 at 10:44

3 Answers 3

9

I just hit this myself. The problem is that CMake only passes CMAKE_SYSROOT to compilers which specify what their command-line flag for receiving a sysroot is, and during the first probing in CMakeDetermineCompilerId doesn't yet know what compiler it's calling. Once it detects that it's GNU, Modules/Compiler/GNU.cmake will do

#!cmake
set(CMAKE_${lang}_COMPILE_OPTIONS_SYSROOT "--sysroot=")

After this is set, CMake will start passing "--sysroot=${CMAKE_SYSROOT}"

This is why running multiple times; despite the fact that the "check for working C compiler" failed the first time through, CMake still detected that the failing compiler was a flavor of GNU and cached that. So on the second attempt, it loads GNU.cmake, does pass --sysroot, and things work (then it has the same failure, but for CXX). On the third try CXX works too, and things actually pass.

You can fix this in a couple of ways, but I'm not sure what's really best

  • Skip linking, so that CMake doesn't actually need the sysroot to succeed, using CMAKE_TRY_COMPILE_TARGET_TYPE

    if((NOT CMAKE_C_COMPILER_ID) OR (NOT CMAKE_CXX_COMPILER_ID)) set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) endif()

  • Force the compiler ID detection early, so it will already know to load GNU.cmake (seemingly has to go in the cache to work within try_compile, not sure why)

    set(CMAKE_C_COMPILER_ID GNU CACHE STRING "") set(CMAKE_CXX_COMPILER_ID GNU CACHE STRING "")

  • Tell CMake about --sysroot, even before it knows it's GNU

    set(CMAKE_C_COMPILE_OPTIONS_SYSROOT "--sysroot=") set(CMAKE_CXX_COMPILE_OPTIONS_SYSROOT "--sysroot=")

I'm not sure which is the best workaround, and I think the whole thing is arguable a CMake bug since it did detect the compiler id fine (using -c, and thus not needing to link), it just didn't reload GNU.cmake based on the results of CMAKE_DETERMINE_COMPILER_ID before checking whether the compiler works.

7

I have two solutions of this problem:

  1. I use the following code in the toolchain file:

    # compiler
    set(CMAKE_C_COMPILER /path/to/arm-none-linux-gnueabi-gcc)
    set(CMAKE_CXX_COMPILER /path/to/arm-none-linux-gnueabi-g++)
    # sysroot location
    set(MYSYSROOT /path/to/sysroots/cortexa7-vfp-neon-telechips-linux-gnueabi)
    # compiler/linker flags
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --sysroot=${MYSYSROOT}" CACHE INTERNAL "" FORCE)
    set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} --sysroot=${MYSYSROOT}" CACHE INTERNAL "" FORCE)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --sysroot=${MYSYSROOT}" CACHE INTERNAL "" FORCE)
    set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} --sysroot=${MYSYSROOT}" CACHE INTERNAL "" FORCE)
    # cmake built-in settings to use find_xxx() functions
    set(CMAKE_FIND_ROOT_PATH "${MYSYSROOT}")
    set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
    set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
    set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
    
  2. Second solution is to use CMakeForceCompiler package in the toolchain file. It looks differently but I have the same result:

    # compiler
    include(CMakeForceCompiler)
    cmake_force_c_compiler(/path/to/arm-none-linux-gnueabi-gcc GNU)
    cmake_force_cxx_compiler(/path/to/arm-none-linux-gnueabi-g++ GNU)
    # sysroot location
    set(MYSYSROOT /path/to/sysroots/cortexa7-vfp-neon-telechips-linux-gnueabi)
    # compiler/linker flags
    add_definitions("--sysroot=${MYSYSROOT}")
    set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} --sysroot=${MYSYSROOT}" CACHE INTERNAL "" FORCE)
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --sysroot=${MYSYSROOT}" CACHE INTERNAL "" FORCE)
    # cmake built-in settings to use find_xxx() functions
    set(CMAKE_FIND_ROOT_PATH ${MYSYSROOT})
    set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
    set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
    set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
    

BTW, I can't use CMAKE_SYSROOT variable, since it available since CMake v3.0 only.

1
  • Both solution 1 and CMAKE_SYSROOT work for me (NXP QorIQ SDK 2.0 PPC64). Thank you very much!
    – Matt
    Oct 11, 2016 at 14:06
-1

I think you need to set CMAKE_C_FLAGS and CMAKE_CXX_FLAGS

Also I think you need

include(CMakeForceCompiler)
1
  • It does not solve my problem, there is still no sysroot added to the compiler test command. However if i run cmake three times in a row it manage to skip the test and I get I somewhat working make system. Mar 29, 2016 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.