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

Stephen Kelly steveire at gmail.com
Wed Apr 24 08:27:29 PDT 2013


Tim Northover wrote:

> Hi Steve,
> 
>>  /usr/bin/arm-linux-gnueabi-gcc-4.7 main.c
>>
>> 'Just Works'. /usr/bin/arm-linux-gnueabi-as also exists.
> 
> In that case, I'd suggest using "-target arm-linux-gnueabi" as a
> starting-point. Clang does use this target in its search for an
> appropriate assembler and linker. I think I concluded that it'll find
> it if arm-linux-gnueabi-as is on the path, but I'm not certain.
> 
> Other than that, the key point is where the ARM libraries and headers
> are. There's a chance Clang will find them by itself with the correct
> -target option. If not, the --sysroot option is what you'll have to
> use to give it a hint.

Thanks, that is progress at least:

 stephen at hal:/tmp$ clang -target arm-linux-gnueabi main.c 
 /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 
directory
 /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 
invocation)

Running 

 locate crtbegin.o | grep arm

reveals 

 /usr/lib/gcc-cross/arm-linux-gnueabi/4.7/crtbegin.o

which looks appropriate. But what should I pass in the --sysroot? 

I tried using /usr/lib/gcc-cross/arm-linux-gnueabi/4.7/ and also all parent 
directories of it, with the same result as above each time.

>> All I need is some way to cross compile something for some platform, so
>> if I can invoke clang in a way that it will use that cross-as, that would
>> be great.
> 
> A parallel option would be to add "-integrated-as" as an option. Then
> Clang does the assembly step itself. I didn't mention it because you'd
> still need the linker to produce a working binary.

Right. Using that I get a linker error because it uses the wrong linker:

 stephen at hal:/tmp$ clang -target armv7--eabi -integrated-as main.c 
 /usr/bin/ld: unknown architecture of input file `/tmp/main-CFNTHI.o' is 
incompatible with i386:x86-64 output
 collect2: error: ld returned 1 exit status
 clang: error: linker (via gcc) command failed with exit code 1 (use -v to 
see invocation)

Thanks,

Steve.






More information about the cfe-dev mailing list