[cfe-dev] Cross compiling with clang (and sysroot support)

Stephen Kelly steveire at gmail.com
Thu May 2 09:03:40 PDT 2013

Tim Northover wrote:

> Hi Steve,
>>  /usr/lib/gcc-cross/arm-linux-gnueabi/4.7/crtbegin.o
>> which looks appropriate. But what should I pass in the --sysroot?
> Unfortunately, it looks like this is odd enough that Clang won't
> detect it by itself no matter what sysroot you specify. In particular
> it doesn't know anything about "gcc-cross" as a possible location.
> Four options come to mind:
> + Fix clang so that it does try to look in gcc-cross.
> + Mess around with your filesystem so that it resembles what Clang is
> searching for (e.g. symlink /usr/lib/gcc-cross/arm-linux-gnueabi to
> /usr/lib/gcc/arm-linux-gnueabi; other things may be needed too but
> there's not enough information to say yet).
> + Use Clang for the compile step ("clang -target ... -c") and gcc for
> the link step ("arm-linux-gnueabi-gcc main.o -omain"). This is still
> dodgy w.r.t. headers, if clang is trying to use the ones from
> /usr/include, but has a good chance of working anyway.
> + Pass these special options and files yourself on the Clang command-line.

Option 5: Use a different toolchain :)

I have two other toolchains lying around. locate crtbegin.o finds a bunch of 
stuff including this stuff from a raspberry pi toolchain:


I using it:

stephen at hal:/tmp$ clang -v -target arm-linux-gnueabi --sysroot 
/home/stephen/rpi/gcc-4.7-linaro-rpi-gnueabihf/ main.c 
Ubuntu clang version 3.2-1~exp5ubuntu2 (tags/RELEASE_32/final) (based on 
LLVM 3.2)                                                                                                                                 
Target: arm--linux-gnueabi                                                                                                                                                                                         
Thread model: posix                                                                                                                                                                                                
 "/usr/bin/clang" -cc1 -triple armv4t--linux-gnueabi -S -disable-free -
disable-llvm-verifier -main-file-name main.c -mrelocation-model static -
mdisable-fp-elim -fmath-errno -mconstructor-aliases -target-abi aapcs-linux 
-target-cpu arm7tdmi -mfloat-abi soft -target-feature +soft-float-abi -
target-linker-version 2.23.1 -momit-leaf-frame-pointer -v -resource-dir 
/usr/bin/../lib/clang/3.2 -isysroot /home/stephen/rpi/gcc-4.7-linaro-rpi-
gnueabihf/ -fmodule-cache-path /var/tmp/clang-module-cache -internal-isystem 
/home/stephen/rpi/gcc-4.7-linaro-rpi-gnueabihf//usr/local/include -internal-
isystem /usr/bin/../lib/clang/3.2/include -internal-isystem 
/usr/include/clang/3.2/include/ -internal-externc-isystem 
-internal-externc-isystem /home/stephen/rpi/gcc-4.7-linaro-rpi-
gnueabihf//usr/include/x86_64-linux-gnu -internal-externc-isystem 
/home/stephen/rpi/gcc-4.7-linaro-rpi-gnueabihf//usr/include -fno-dwarf-
directory-asm -fdebug-compilation-dir /tmp -ferror-limit 19 -fmessage-length 
211 -mstackrealign -fno-signed-char -fobjc-runtime=gcc -fdiagnostics-show-
option -fcolor-diagnostics -o /tmp/main-eBabK4.s -x c main.c                                                                        
clang -cc1 version 3.2 based upon LLVM 3.2svn default target x86_64-pc-
ignoring nonexistent directory "/home/stephen/rpi/gcc-4.7-linaro-rpi-
ignoring nonexistent directory "/usr/bin/../lib/clang/3.2/include"                                                                                                                                                 
ignoring nonexistent directory "/home/stephen/rpi/gcc-4.7-linaro-rpi-
ignoring nonexistent directory "/home/stephen/rpi/gcc-4.7-linaro-rpi-
ignoring nonexistent directory "/home/stephen/rpi/gcc-4.7-linaro-rpi-
#include "..." search starts here:
#include <...> search starts here:
End of search list.
 "/usr/bin/arm-linux-gnueabi-as" -mfloat-abi=softfp -o /tmp/main-AvS3Kc.o 
 "/usr/bin/arm-linux-gnueabi-ld" --sysroot=/home/stephen/rpi/gcc-4.7-linaro-
rpi-gnueabihf/ -z relro -X --hash-style=gnu --build-id --eh-frame-hdr -m 
armelf_linux_eabi -dynamic-linker /lib/ld-linux.so.3 -o a.out crt1.o crti.o 
crtbegin.o -L/home/stephen/rpi/gcc-4.7-linaro-rpi-gnueabihf//lib /tmp/main-
AvS3Kc.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -
lgcc_s --no-as-needed crtend.o crtn.o
/usr/bin/arm-linux-gnueabi-ld: cannot find crt1.o: No such file or directory
/usr/bin/arm-linux-gnueabi-ld: cannot find crti.o: No such file or directory
/usr/bin/arm-linux-gnueabi-ld: cannot find crtbegin.o: No such file or 
/usr/bin/arm-linux-gnueabi-ld: cannot find -lgcc
/usr/bin/arm-linux-gnueabi-ld: cannot find -lgcc_s
clang: error: linker command failed with exit code 1 (use -v to see 

I find it odd that it considers 


before finding it is non-existant. It's mixing triples.



More information about the cfe-dev mailing list