[llvm-dev] Cross Compilation Problem

Peter Smith via llvm-dev llvm-dev at lists.llvm.org
Mon Jul 2 05:54:23 PDT 2018


On 2 July 2018 at 12:25, Muhui Jiang <jiangmuhui at gmail.com> wrote:

> ================
> Here I am a little bit confusing. In my test.c, I need to include some .h
> files like stdio.h. I think I need to use the -L or -I to tell clang where
> to find these headers.
>

Headers such as stdio.h should come from the Linaro gcc installation,
the --sysroot and --gcc-toolchain should be sufficient to find these
headers and libraries. For example stdio.h can be found in:
gcc-linaro-7.3.1-2018.05-x86_64_arm-linux-gnueabi/arm-linux-gnueabi/libc/usr/include/stdio.h
crtbegin.o can be found in:
gcc-linaro-7.3.1-2018.05-x86_64_arm-linux-gnueabi/lib/gcc/arm-linux-gnueabi/7.3.1/crtbegin.o

>
> Just to make sure you are aware, the arm-linux-gnueabi gcc tooltchain
> has soft-floating point libraries. If you want hard floating point
> libraries, such as used on the Raspberry Pi, you'll need the
> arm-linux-gnueabihf gcc toolchain and to use
> --target=arm-linux-gnueabihf.
> =============
> I see. I don't know before. But I think arm-linux-gnueabi now is enough.

Ok. I guess the main thing to watch out for is when you come to run
the executable. An arm-linux-gnueabi executable is not going to run on
an arm-linux-gnueabihf system and vice versa.

>
> Depending on how simple your test.c is I suggest:
> /home/jmh/Downloads/llvm/build/bin/clang --target=arm-linux-gnueabi
> --sysroot=/home/jmh/Downloads/gcc-linaro-7.3.1-2018.05-x86_64_arm-linux-gnueabi/arm-linux-gnueabi/libc
> --gcc-toolchain=/home/jmh/Downloads/gcc-linaro-7.3.1-2018.05-x86_64_arm-linux-gnueabi/lib/gcc/arm-linux-gnueabi/
> -target arm-linux-gnueabi -fuse-ld=lld -o test test.c
> ===============
> Actually, the same exception occurs, which is shown below.
>
> /home/jmh/Downloads/llvm/build/bin/ld.lld: error: cannot open crtbegin.o: No
> such file or directory
> /home/jmh/Downloads/llvm/build/bin/ld.lld: error: unable to find library
> -lgcc
> /home/jmh/Downloads/llvm/build/bin/ld.lld: error: unable to find library
> -lgcc
> /home/jmh/Downloads/llvm/build/bin/ld.lld: error: cannot open crtend.o: No
> such file or directory
> clang-7: error: linker command failed with exit code 1 (use -v to see
> invocation)
>

That is strange. It will be worth adding the -v output to clang to see
if it has found your gcc installation correctly. For my installation I
get:
clang test.c -fuse-ld=lld --target=arm-linux-gnueabi
--gcc-toolchain=/home/psmith/Downloads/gcc-linaro-7.3.1-2018.05-x86_64_arm-linux-gnueabi
--sysroot=/home/psmith/Downloads/gcc-linaro-7.3.1-2018.05-x86_64_arm-linux-gnueabi/arm-linux-gnueabi/libc
-o test.axf -v

clang version 7.0.0 (trunk 336072)
Target: arm--linux-gnueabi
Thread model: posix
InstalledDir: /linaro/upstream/buildclang/bin
Found candidate GCC installation:
/home/psmith/Downloads/gcc-linaro-7.3.1-2018.05-x86_64_arm-linux-gnueabi/lib/gcc/arm-linux-gnueabi/7.3.1
Selected GCC installation:
/home/psmith/Downloads/gcc-linaro-7.3.1-2018.05-x86_64_arm-linux-gnueabi/lib/gcc/arm-linux-gnueabi/7.3.1
Candidate multilib: .;@m32
Selected multilib: .;@m32
...

In the -v output I see that the clang driver has added the necessary
includes and libraries.


> I use clang -emit-llvm -c to generate the LLVM bitcode. It works. I think
> this problem is related to the llvm linkers, lld. Do you have any ideas.
>

It is likely a toolchain problem in that clang isn't finding your gcc
installation. Apologies I don't have any more ideas, the only way I
can reproduce the error you are seeing is if I don't add the
--target=arm-linux-gnueabi

> The last, but might be more important to me. Do you have any experience to
> cross compile the arm binaries with autotools(configure, make, make install)
> and save the LLVM IR at the same time. Many Thanks
>

If I understand you correctly you would like clang to emit an object
and bitcode at the same time? I'm not sure if that is possible with
just the executable. You might be able to make the compiler a script
that invokes clang twice once with an object file to satisfy the build
tools and once with -emit-llvm. Apologies I don't have much experience
in that area.

Peter

> Regards
> Muhui
>
>
> 2018-07-02 5:04 GMT-04:00 Peter Smith <peter.smith at linaro.org>:
>>
>> Hello Muhui,
>>
>> From what I can see from your commands I think:
>> - You're missing --target=arm-linux-gnueabi , without that clang will
>> be building an X86 object and will not expect to be cross compiling.
>> - --rtlib=compiler-rt will probably not work out of the box. The clang
>> that you download or build on an X86 machine will not have compiler-rt
>> libraries for Arm. You can cross compile these and install them into
>> the clang/7.0.0/lib/linux directory and there is a document to help
>> you build the builtins at
>> https://llvm.org/docs/HowToCrossCompileBuiltinsOnArm.html. If you want
>> to get started quickly I recommend taking out that option.
>> - You shouldn't need to use sudo to use clang
>> - I don't think that you need to add -L and -l for the llvm library
>> directories unless you are actually using the libraries from them.
>> Beware that the libraries in that directory will probably be compiled
>> for X86 so won't be of much use to you on an Arm system.
>>
>> Just to make sure you are aware, the arm-linux-gnueabi gcc tooltchain
>> has soft-floating point libraries. If you want hard floating point
>> libraries, such as used on the Raspberry Pi, you'll need the
>> arm-linux-gnueabihf gcc toolchain and to use
>> --target=arm-linux-gnueabihf.
>>
>> Depending on how simple your test.c is I suggest:
>> /home/jmh/Downloads/llvm/build/bin/clang --target=arm-linux-gnueabi
>>
>> --sysroot=/home/jmh/Downloads/gcc-linaro-7.3.1-2018.05-x86_64_arm-linux-gnueabi/arm-linux-gnueabi/libc
>>
>> --gcc-toolchain=/home/jmh/Downloads/gcc-linaro-7.3.1-2018.05-x86_64_arm-linux-gnueabi/lib/gcc/arm-linux-gnueabi/
>> -target arm-linux-gnueabi -fuse-ld=lld -o test test.c
>>
>> Hope this gets you a little bit further.
>>
>> Peter
>>
>> On 1 July 2018 at 18:19, Muhui Jiang <jiangmuhui at gmail.com> wrote:
>> > Hi Peter
>> >
>> > I guess this is the document written by you
>> >
>> > https://fosdem.org/2018/schedule/event/crosscompile/attachments/slides/2107/export/events/attachments/crosscompile/slides/2107/How_to_cross_compile_with_LLVM_based_tools.pdf
>> >
>> > I follow it to try to use the clang to do cross compilation. Actually,
>> > my
>> > target binaries is SPECCPU2006 and autotools based binaries.
>> >
>> > However, I failed on the first step. I write a hello world as test.c.  I
>> > refer to your document and download the newest version of arm toolchain.
>> >
>> > I use the following command to try to compile the code to binary.
>> >
>> > sudo /home/jmh/Downloads/llvm/build/bin/clang
>> >
>> > --sysroot=/home/jmh/Downloads/gcc-linaro-7.3.1-2018.05-x86_64_arm-linux-gnueabi/arm-linux-gnueabi/libc
>> >
>> > --gcc-toolchain=/home/jmh/Downloads/gcc-linaro-7.3.1-2018.05-x86_64_arm-linux-gnueabi/lib/gcc/arm-linux-gnueabi/
>> > -target arm-linux-gnueabi -fuse-ld=lld -L
>> > /home/jmh/Downloads/llvm/build/lib
>> > -I/home/jmh/Downloads/llvm/build/include/ --rtlib=compiler-rt -o test
>> > test.c
>> >
>> > However, I come across the following exception.
>> >
>> > /home/jmh/Downloads/llvm/build/bin/ld.lld: error: cannot open
>> > crtbegin.o: No
>> > such file or directory
>> > /home/jmh/Downloads/llvm/build/bin/ld.lld: error: cannot open
>> >
>> > /home/jmh/Downloads/llvm/build/lib/clang/7.0.0/lib/linux/libclang_rt.builtins-arm.a:
>> > No such file or directory
>> > /home/jmh/Downloads/llvm/build/bin/ld.lld: error: cannot open
>> >
>> > /home/jmh/Downloads/llvm/build/lib/clang/7.0.0/lib/linux/libclang_rt.builtins-arm.a:
>> > No such file or directory
>> > /home/jmh/Downloads/llvm/build/bin/ld.lld: error: cannot open crtend.o:
>> > No
>> > such file or directory
>> > clang-7: error: linker command failed with exit code 1 (use -v to see
>> > invocation)
>> >
>> > Do you have any ideas or suggestions. I also cc the problem to llvm
>> > community. Anyone who has the same experience. Please help me. Many
>> > Thanks
>> >
>> > Regards
>> > Muhui
>> >
>> >
>
>


More information about the llvm-dev mailing list