llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
26.98k stars 11.05k forks source link

cannot find libc when cross compile for aarch64 #97890

Closed xiedeacc closed 6 days ago

xiedeacc commented 1 week ago

my clang/llvm build from source use tag:18.1.8, target header and library download from clang+llvm-18.1.8-aarch64-linux-gnu, when I use below command to build a hello_world program for aarch64 on x86_64, it said cannot find -lc, llvm contain libc runtime, but I cannot find libc.so, where it is?

➜  /root/src/software /usr/local/llvm/18/bin/clang++ -v --target=aarch64-unknown-linux-gnu \
  --sysroot=/root/src/software/clang_sysroot \
  -I/root/src/software/clang_sysroot/include \
  -I/root/src/software/clang_sysroot/include/aarch64-unknown-linux-gnu/c++/v1 \
  -I/root/src/software/clang_sysroot/include/c++/v1 \
  -I/root/src/software/gcc-arm-11.2/usr/include \
  -B/usr/local/llvm/18/bin \
  -L/root/src/software/clang_sysroot/lib \
  -L/root/src/software/clang_sysroot/lib/aarch64-unknown-linux-gnu \
  -L/root/src/software/clang_sysroot/lib/clang/18/lib/aarch64-unknown-linux-gnu \
  -L/root/src/software/clang_sysroot/usr/lib \
  -static -fuse-ld=lld --rtlib=compiler-rt \
  main.cc
clang version 18.1.8 (https://github.com/llvm/llvm-project.git 3b5b5c1ec4a3095ab096dd780e84d7ab81f3d7ff)
Target: aarch64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/local/llvm/18/bin
Found candidate GCC installation: /root/src/software/clang_sysroot/usr/lib/gcc/aarch64-none-linux-gnu/11.2.1
 "/usr/local/llvm/18/bin/clang-18" -cc1 -triple aarch64-unknown-linux-gnu -emit-obj -mrelax-all -dumpdir a- -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name main.cc -static-define -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=non-leaf -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu generic -target-feature +v8a -target-feature +fp-armv8 -target-feature +neon -target-abi aapcs -debugger-tuning=gdb -fdebug-compilation-dir=/root/src/software -v -fcoverage-compilation-dir=/root/src/software -resource-dir /usr/local/llvm/18/lib/clang/18 -I /root/src/software/clang_sysroot/include -I /root/src/software/clang_sysroot/include/aarch64-unknown-linux-gnu/c++/v1 -I /root/src/software/clang_sysroot/include/c++/v1 -I /root/src/software/gcc-arm-11.2/usr/include -isysroot /root/src/software/clang_sysroot -internal-isystem /usr/local/llvm/18/bin/../include/c++/v1 -internal-isystem /usr/local/llvm/18/lib/clang/18/include -internal-isystem /root/src/software/clang_sysroot/usr/local/include -internal-externc-isystem /root/src/software/clang_sysroot/include -internal-externc-isystem /root/src/software/clang_sysroot/usr/include -fdeprecated-macro -ferror-limit 19 -fno-signed-char -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fcxx-exceptions -fexceptions -fcolor-diagnostics -target-feature +outline-atomics -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/main-a82756.o -x c++ main.cc
clang -cc1 version 18.1.8 based upon LLVM 18.1.8 default target x86_64-unknown-linux-gnu
ignoring nonexistent directory "/root/src/software/clang_sysroot/usr/local/include"
ignoring nonexistent directory "/root/src/software/clang_sysroot/usr/include"
ignoring duplicate directory "/root/src/software/clang_sysroot/include"
  as it is a non-system directory that duplicates a system directory
#include "..." search starts here:
#include <...> search starts here:
 /root/src/software/clang_sysroot/include/aarch64-unknown-linux-gnu/c++/v1
 /root/src/software/clang_sysroot/include/c++/v1
 /root/src/software/gcc-arm-11.2/usr/include
 /usr/local/llvm/18/bin/../include/c++/v1
 /usr/local/llvm/18/lib/clang/18/include
 /root/src/software/clang_sysroot/include
End of search list.
 "/usr/local/llvm/18/bin/ld.lld" --sysroot=/root/src/software/clang_sysroot -EL -z relro --hash-style=gnu --eh-frame-hdr -m aarch64linux -static -o a.out /root/src/software/clang_sysroot/usr/lib/crt1.o /root/src/software/clang_sysroot/usr/lib/crti.o /root/src/software/clang_sysroot/usr/lib/crtbeginT.o -L/root/src/software/clang_sysroot/lib -L/root/src/software/clang_sysroot/lib/aarch64-unknown-linux-gnu -L/root/src/software/clang_sysroot/lib/clang/18/lib/aarch64-unknown-linux-gnu -L/root/src/software/clang_sysroot/usr/lib -L/root/src/software/clang_sysroot/lib -L/root/src/software/clang_sysroot/usr/lib /tmp/main-a82756.o -lc++ -lm --start-group /usr/local/llvm/18/lib/clang/18/lib/linux/libclang_rt.builtins-aarch64.a -lc --end-group /root/src/software/clang_sysroot/usr/lib/crtend.o /root/src/software/clang_sysroot/usr/lib/crtn.o
ld.lld: error: unable to find library -lc
clang++: error: linker command failed with exit code 1 (use -v to see invocation)

clang/llvm build command

cmake -G "Unix Makefiles" ../llvm \
    -DLLVM_ENABLE_PROJECTS="bolt;clang;clang-tools-extra;libclc;lld;lldb;mlir;polly" \
    -DLLVM_ENABLE_RUNTIMES="libc;libunwind;libcxxabi;pstl;libcxx;compiler-rt" \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=/usr/local/llvm/18 \
    -DMLIR_INCLUDE_INTEGRATION_TESTS=OFF \
    -DLLVM_INCLUDE_TESTS=OFF \
    -DLLVM_BUILD_TESTS=OFF \
    -DLLDB_INCLUDE_TESTS=OFF \
    -DCLANG_INCLUDE_TESTS=OFF \
    -DCLANG_DEFAULT_CXX_INCLUDE_DIRS="/usr/local/llvm-18/include/c++/v1" \
    -DCLANG_DEFAULT_CXX_STDLIB=libc++ \
    -DCLANG_DEFAULT_RTLIB=compiler-rt \
    -DCLANG_DEFAULT_LINKER=lld
xiedeacc commented 1 week ago

it seems libc in llvm cannot be used for now???

for anyone who want to cross compile use libc++ not stdlibc++ can use below command

/usr/local/llvm/18/bin/clang++ -v --target=aarch64-unknown-linux-gnu \
  --sysroot=/root/src/software/clang_sysroot \
  -I/root/src/software/clang_sysroot/include \
  -I/root/src/software/clang_sysroot/include/aarch64-unknown-linux-gnu/c++/v1 \
  -I/root/src/software/clang_sysroot/include/c++/v1 \
  -I/root/src/software/gcc-arm-11.2/usr/include \
  -B/usr/local/llvm/18/bin \
  -L/root/src/software/clang_sysroot/lib \
  -L/root/src/software/clang_sysroot/lib/aarch64-unknown-linux-gnu \
  -L/root/src/software/clang_sysroot/lib/clang/18/lib/aarch64-unknown-linux-gnu \
  -L/root/src/software/clang_sysroot/usr/lib \
  -static -stdlib=libc++ -lc++ -lc++abi -lunwind -fuse-ld=lld --rtlib=compiler-rt \
  main.cc
lntue commented 1 week ago

LLVM libc is still in complete, and there are some recent effort to bootstrap clang and/or libc++ with LLVM libc.

https://github.com/llvm/llvm-project/issues/97191 might be interesting to you?