Cross-compilation from x86_64 to armv7 using Clang

Hi! I need to setup cross-compilation in Github Actions from the x86_64 ubuntu runner, to armv7 using Clang. The goal is to build a few static libraries and serve them, so that they can be downloaded by a user and linked natively on an armv7 target (like a Raspberry PI).

Cross-compilation using GCC already works fine: I install the packages (like gcc-9-arm-linux-gnueabihf) and then provide a CMake toolchain file that sets the compiler (like arm-linux-gnueabihf-g++-9. That works fine.

But with Clang, it seems to be a lot more complicated. I found following toolchain file on the internet:

cmake_minimum_required(VERSION 3.13)

set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR ARM)

# Specify the cross compiler
# The target triple needs to match the prefix of the binutils exactly
# (e.g. CMake looks for arm-none-eabi-ar)
set(CLANG_TARGET_TRIPLE arm-linux-gnueabihf)
set(GCC_ARM_TOOLCHAIN_PREFIX ${CLANG_CLANG_TARGET_TRIPLE})
set(CMAKE_C_COMPILER clang)
set(CMAKE_C_COMPILER_TARGET ${CLANG_TARGET_TRIPLE})
set(CMAKE_CXX_COMPILER clang++)
set(CMAKE_CXX_COMPILER_TARGET ${CLANG_TARGET_TRIPLE})
set(CMAKE_ASM_COMPILER clang)
set(CMAKE_ASM_COMPILER_TARGET ${CLANG_TARGET_TRIPLE})

# Don't run the linker on compiler check
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

# Specify compiler flags
set(ARCH_FLAGS "-mcpu=cortex-a5 -mthumb -mfpu=neon-vfpv4 -mfloat-abi=hard -mno-unaligned-access")
set(CMAKE_C_FLAGS "-Wall ${ARCH_FLAGS}" CACHE STRING "Common flags for C compiler")
set(CMAKE_CXX_FLAGS "-Wall -std=c++17 -fno-exceptions -fno-rtti -fno-threadsafe-statics ${ARCH_FLAGS}" CACHE STRING "Common flags for C++ compiler")
set(CMAKE_ASM_FLAGS "-Wall ${ARCH_FLAGS} -x assembler-with-cpp" CACHE STRING "Common flags for assembler")
# set(CMAKE_EXE_LINKER_FLAGS "--Wl,-Map,kernel.map,--gc-sections -fuse-linker-plugin -Wl,--use-blx --specs=nano.specs --specs=nosys.specs" CACHE STRING "")

# C/C++ toolchain
set(GCC_ARM_SYSROOT "/usr/arm-linux-gnueabihf")
# set(CMAKE_SYSROOT ${GCC_ARM_SYSROOT})
set(CMAKE_FIND_ROOT_PATH ${GCC_ARM_SYSROOT})

# 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)

All of this looks fine. However, when i build it:

cmake --build build
[ 50%] Linking CXX executable test
CMakeFiles/test.dir/main.cpp.o: file not recognized: file format not recognized
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [CMakeFiles/test.dir/build.make:84: test] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/test.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
cmake --build build -v
/usr/bin/cmake -S/mnt/c/Users/zachs/Projects/test -B/mnt/c/Users/zachs/Projects/test/build --check-build-syste
m CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /mnt/c/Users/zachs/Projects/test/build/CMakeFiles /mnt/c/Users/zachs/Pr
ojects/test/build/CMakeFiles/progress.marks
/usr/bin/make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/mnt/c/Users/zachs/Projects/test/build'
/usr/bin/make -f CMakeFiles/test.dir/build.make CMakeFiles/test.dir/depend
make[2]: Entering directory '/mnt/c/Users/zachs/Projects/test/build'
cd /mnt/c/Users/zachs/Projects/test/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /mnt/c/Users/zac
hs/Projects/test /mnt/c/Users/zachs/Projects/test /mnt/c/Users/zachs/Projects/test/build /mnt/c/Users/zachs/Pr
ojects/test/build /mnt/c/Users/zachs/Projects/test/build/CMakeFiles/test.dir/DependInfo.cmake --color=
make[2]: Leaving directory '/mnt/c/Users/zachs/Projects/test/build'
/usr/bin/make -f CMakeFiles/test.dir/build.make CMakeFiles/test.dir/build
make[2]: Entering directory '/mnt/c/Users/zachs/Projects/test/build'
[ 50%] Linking CXX executable test
/usr/bin/cmake -E cmake_link_script CMakeFiles/test.dir/link.txt --verbose=1
/usr/bin/clang++ --target=arm-linux-gnueabihf     CMakeFiles/test.dir/main.cpp.o  -o test 
CMakeFiles/test.dir/main.cpp.o: file not recognized: file format not recognized
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [CMakeFiles/test.dir/build.make:84: test] Error 1
make[2]: Leaving directory '/mnt/c/Users/zachs/Projects/test/build'
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/test.dir/all] Error 2
make[1]: Leaving directory '/mnt/c/Users/zachs/Projects/test/build'
make: *** [Makefile:84: all] Error 2

I also did not manage to cross-compile a simple program by hand:

clang main.cpp --target=arm-linux-gnueabihf
In file included from main.cpp:2:
/usr/bin/../lib/gcc-cross/arm-linux-gnueabihf/9/../../../../include/c++/9/iostream:38:10: fatal error: 'bits/c
++config.h' file not found
#include <bits/c++config.h>
         ^~~~~~~~~~~~~~~~~~
1 error generated.

Adding any flags related to sysroot does not change anything for me, neither with the actual path, nor with an inexistent path.

Sorry if it is a trivial user error of mine, but even after hours of googling I did not find a solution that works for me…

It looks like the build using the CMake toolchain file is producing Arm code, but using the system linker /usr/bin/ld which will be x86_64. Ideally it would be using arm-linux-gnueabihf-ld (by detection of the GCC toolchain location) but that doesn’t seem to be happening. As pointed out in the diagnostic -v can be helpful in seeing what the clang driver has worked out. I’ve also found it helpful to look at the code in llvm-project/clang/lib/Driver/ToolChains at main · llvm/llvm-project · GitHub to see what it is trying to look for.

In your hand-compiled example I think this might be because you are using clang and not clang++ when compiling a C++ program. clang++ will add in extra include and library paths.