llvm / llvm-project

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

clang and clang++ can't build static linked binary,but dynamic linked binary 's compilation is ok #106838

Open paleknight894 opened 2 months ago

paleknight894 commented 2 months ago

my env is ubuntu 24.04 the llvm source code is git clone from the last(20.0) Configured by:

cmake
-G Ninja
-DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind;compiler-rt"
-DLLVM_ENABLE_PROJECTS="clang;lld;clang-tools-extra"
-DCMAKE_BUILD_TYPE="Release"
-DBUILD_SHARED_LIBS=on
-DLLVM_TARGETS_TO_BUILD="X86"
-DCMAKE_INSTALL_PREFIX="/usr/local/llvm"
-DCMAKE_C_COMPILER_LAUNCHER="ccache"
-DCMAKE_CXX_COMPILER_LAUNCHER="ccache"
-DLLVM_USE_LINKER="lld"
-DLIBCXXABI_USE_LLVM_UNWIND=ON
-DLIBUNWIND_ENABLE_SHARED=1
-DCLANG_DEFAULT_CXX_STDLIB="libc++"
-DCLANG_DEFAULT_RTLIB="compiler-rt"
-D_CMAKE_TOOLCHAIN_PREFIX=llvm-
-DCMAKE_C_COMPILER="clang"
-DCMAKE_CXX_COMPILER="clang++"
-DCMAKE_ASM_COMPILER="clang"
-DLIBCXXABI_USE_COMPILER_RT=YES
-DLIBCXX_USE_COMPILER_RT=YES
-DLIBCXX_HAS_GCC_LIB=OFF
-DLIBCXX_HAS_ATOMIC_LIB=OFF
-DCLANG_DEFAULT_LINKER
../llvm/

when i compile by clang hello.c ,it works as i expected.(hello.c is just a printf hello world test) but if i compile by clang -static hello.c

ld.lld: error: undefined symbol: _Unwind_Resume
referenced by pthread_once.o:(__pthread_once_slow.cold) in archive /lib/x86_64-linux-gnu/libc.a
 referenced by iofclose.o:(_IO_new_fclose.cold) in archive /lib/x86_64-linux-gnu/libc.a
referenced by iogetdelim.o:(__getdelim.cold) in archive /lib/x86_64-linux-gnu/libc.a
 referenced 4 more times

ld.lld: error: undefined symbol: _Unwind_GetLanguageSpecificData
 referenced by gcc_personality_v0.c
               gcc_personality_v0.c.o:(__gcc_personality_v0) in archive /usr/local/llvm/lib/clang/20/lib/x86_64-unknown-linux-gnu/libclang_rt.builtins.a

ld.lld: error: undefined symbol: _Unwind_GetIP
 referenced by gcc_personality_v0.c
               gcc_personality_v0.c.o:(__gcc_personality_v0) in archive /usr/local/llvm/lib/clang/20/lib/x86_64-unknown-linux-gnu/libclang_rt.builtins.a

ld.lld: error: undefined symbol: _Unwind_GetRegionStart
 referenced by gcc_personality_v0.c
               gcc_personality_v0.c.o:(__gcc_personality_v0) in archive /usr/local/llvm/lib/clang/20/lib/x86_64-unknown-linux-gnu/libclang_rt.builtins.a

ld.lld: error: undefined symbol: _Unwind_SetGR
 referenced by gcc_personality_v0.c
               gcc_personality_v0.c.o:(__gcc_personality_v0) in archive /usr/local/llvm/lib/clang/20/lib/x86_64-unknown-linux-gnu/libclang_rt.builtins.a
 referenced by gcc_personality_v0.c
               gcc_personality_v0.c.o:(__gcc_personality_v0) in archive /usr/local/llvm/lib/clang/20/lib/x86_64-unknown-linux-gnu/libclang_rt.builtins.a

ld.lld: error: undefined symbol: _Unwind_SetIP
 referenced by gcc_personality_v0.c
               gcc_personality_v0.c.o:(__gcc_personality_v0) in archive /usr/local/llvm/lib/clang/20/lib/x86_64-unknown-linux-gnu/libclang_rt.builtins.a
clang: error: linker command failed with exit code 1 (use -v to see invocation)

looks like something missing about libunwind,so I compile by clang -static -lunwind hello.c, It works just fine. Is there something wrong about my configuration,I want build a llvm toolchain replace gcc

shafik commented 2 months ago

CC @Endilll

Endilll commented 2 months ago

Looking at https://github.com/llvm/llvm-project/blob/3d5e1ec6508c8425601d4cfaba4c8a8f18791e2b/clang/CMakeLists.txt#L253-L268 I think what happens is that -DCLANG_DEFAULT_RTLIB="compiler-rt" is not properly propagated to CLANG_DEFAULT_UNWINDLIB, because compiler-rt is not libunwind. I'm not sure whether and how this should be fixed, but can you try building with -DCLANG_DEFAULT_UNWINDLIB="libunwind" on top of your CMake configuration?

paleknight894 commented 2 months ago

Looking at

https://github.com/llvm/llvm-project/blob/3d5e1ec6508c8425601d4cfaba4c8a8f18791e2b/clang/CMakeLists.txt#L253-L268

I think what happens is that -DCLANG_DEFAULT_RTLIB="compiler-rt" is not properly propagated to CLANG_DEFAULT_UNWINDLIB, because compiler-rt is not libunwind. I'm not sure whether and how this should be fixed, but can you try building with -DCLANG_DEFAULT_UNWINDLIB="libunwind" on top of your CMake configuration? Add "-DCLANG_DEFAULT_UNWINDLIB="libunwind"" solved part of my problems,now I can build static c executables in "gcc way",but when I try to build static c++ executable,it complains some undefined reference about libcxxabi. Also,build dynamic c++ executables works normally. I try to build it by "clang++ -static -lc++abi hello.cpp",It works.... Is there some configuration needed with libcxxabi, should I try LIBCXX_STATICALLY_LINK_ABI_IN_SHARED_LIBRARY=on but it should turn on in linux by default.