[cfe-users] The native "cross compiler" part - how to use it

Norbert Lange via cfe-users cfe-users at lists.llvm.org
Mon Dec 19 15:41:02 PST 2016


Hello,

I am trying to get my head around how Clang is supposedly used as
Crosscompiler. I am using it for years with a custom gcc toolchain,
but its cumbersome as it involves adding all inlude and library paths
from gcc and adding them as options.
So I tested the arguments that should allow crosscompiling and
compared what they do.

(I am on Debian Strech AMD64, but same or similar issues persist on
older Debian Versions atleast)

----------------------------------------------------
echo 'int main(){}' > test.cpp

# Works
clang++ -v test.cpp &> clang_normal.txt

# DOESNT Work. This is the same gcc toolchain clang picked up
automatically with the above command
clang++ --gcc-toolchain=/usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.1 -v
test.cpp &> clang_defaulttc.txt

# installed package g++-arm-linux-gnueabihf
# Works
clang++ --target=arm-linux-gnueabihf -v test.cpp &> clang_crossnormal.txt

 DOESNT Work. This is the same gcc toolchain clang picked up
automatically with the above command
clang++ --target=arm-linux-gnueabihf
--gcc-toolchain=/usr/lib/gcc-cross/arm-linux-gnueabihf/6 -v test.cpp
&> clang_crossdefaulttc.txt

# own cross compiler. Lacks the correct include paths, works otherwise
with the commands below
clang++ --target=arm-none-eabi
-B/opt/hip-toolchain-5/bin/arm-none-eabi- -v test.cpp
--specs=nosys.specs &> clang_extcrosstc.txt

clang++ --target=arm-none-eabi
-B/opt/hip-toolchain-5/bin/arm-none-eabi-
--sysroot=/opt/hip-toolchain-5/arm-none-eabi -v test.cpp
--specs=nosys.specs &> clang_extcrosstc_s.txt

clang++ --target=arm-none-eabi
-B/opt/hip-toolchain-5/bin/arm-none-eabi-
--gcc-toolchain=opt/hip-toolchain-5/lib/gcc/arm-none-eabi/5.3.0
--sysroot=/opt/hip-toolchain-5/arm-none-eabi -v test.cpp
--specs=nosys.specs &> clang_extcrosstc_st.txt

# Trick with sysmlinking to clang, this essentially saves the --target
and -B option.
/opt/hip-toolchain-5/bin/arm-none-eabi-clang++ -v test.cpp
--specs=nosys.specs &> clang_extcrosstc_link.txt

# Normalize tempnames for easy comparison
for file in *.txt; do
  sed -e 's,/tmp/test-.*\.o,/tmp/test-000000.o,g' -e
's,/tmp/.*\.res,/tmp/cc000000.res,g' -i $file
done
----------------------------------------------------

The results are attached as file. Findings are:

* Detection of default system compilers seems to work, also with
crosscompilers via --target

* --gcc-toolchain wont work, even if you set the very same value that
clang claims to have used

* --gcc-toolchain doesnt do anything for my cross compiler (tried
several variants with useful paths)

* -B/opt/hip-toolchain-5/bin/arm-none-eabi- makes gcc pickup the right
gcc for linking (would be system gcc otherwise)

* --sysroot=/opt/hip-toolchain-5/arm-none-eabi should pick up the
correct system includes according to
http://clang.llvm.org/docs/CrossCompilation.html. It doesnt (searches
for /usr/include instead of /include)

That means I dont know a way to make --gcc-toolchain usefull, might be
a bug (debian or upstream).
--sysroot similarly doesnt seem to make sense (and -isysroot is even worse)

The only way is adding alot include and library paths for
crosscompilers. using --gcc-toolchain makes even that impossible,
since crtbegin.o wont be found even with the correct added paths?

Is this just insanely buggy, or am I getting it totally wrong?

Using clang with cmake seems to be somewhat easier, since cmake has an
own option for sysroot and manages to do it right
-------------- next part --------------
A non-text attachment was scrubbed...
Name: cross_results.tar.gz
Type: application/x-gzip
Size: 2832 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-users/attachments/20161220/2f792847/attachment.bin>


More information about the cfe-users mailing list