Issues with building the LLVM 10 C++ toolchain - c++

Specifically, I'd like to build Clang, lld, lldb, libc++, and compiler-rt, and configure Clang to use these by default. I'm on an x86-64 Linux distro (Oracle Linux), so I believe this should be supported.
This is what I did, after several tries and lots of Google / documentation-reading:
git clone https://github.com/llvm/llvm-project.git -b llvmorg-10.0.0
cd llvm-project
mkdir build
cd build
cmake \
-D CMAKE_BUILD_TYPE=Release \
-D CMAKE_C_FLAGS_RELEASE='-DNDEBUG -Ofast' \
-D CMAKE_CXX_FLAGS_RELEASE='-DNDEBUG -Ofast' \
-D CMAKE_INSTALL_PREFIX=/opt/llvm10-toolchain \
-D LLVM_ENABLE_WARNINGS=Off \
-D LLVM_TARGETS_TO_BUILD=X86 \
-D LLVM_ENABLE_PROJECTS="clang;clang-tools-extra;lld;lldb" \
-D LLVM_ENABLE_RUNTIMES="compiler-rt;libcxx;libcxxabi;libunwind" \
-D LLVM_INCLUDE_TOOLS=On \
-D LLVM_BUILD_TOOLS=On \
-D LLVM_INCLUDE_BENCHMARKS=Off \
-D LLVM_INSTALL_BINUTILS_SYMLINKS=On \
-D LLVM_INSTALL_CCTOOLS_SYMLINKS=On \
-D LLVM_APPEND_VC_REV=On \
-D LLVM_ENABLE_THREADS=On \
-D LLVM_ENABLE_LTO=On \
-D LLVM_ENABLE_BINDINGS=Off \
-D LLVM_LIBDIR_SUFFIX=64 \
-D LLVM_BUILD_32_BITS=Off \
-D LLVM_BUILD_LLVM_DYLIB=Off \
-D LLVM_INSTALL_TOOLCHAIN_ONLY=On \
-D CLANG_DEFAULT_LINKER=lld \
-D CLANG_DEFAULT_CXX_STDLIB=libc++ \
-D CLANG_DEFAULT_RTLIB=compiler-rt \
-D LIBCXX_USE_COMPILER_RT=On \
-D LIBCXXABI_USE_COMPILER_RT=On \
-D LIBCXXABI_USE_LLVM_UNWINDER=On \
-G Ninja \
../llvm
cmake --build .
cmake --build . --target install
I'm not using bootstrapping, as I have a fairly recent GCC installed and active (GCC 9.3.0) in the environment.
While build succeeds with this, I end up with a non-functional Clang:
> clang++ -v -std=c++17 -Wall -Wextra now.cpp -o now
clang version 10.0.0 (https://github.com/llvm/llvm-project.git d32170dbd5b0d54436537b6b75beaf44324e0c28)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /opt/llvm10-toolchain/bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-redhat-linux/4.8.2
Found candidate GCC installation: /usr/lib/gcc/x86_64-redhat-linux/4.8.5
Selected GCC installation: /usr/lib/gcc/x86_64-redhat-linux/4.8.5
Candidate multilib: .;#m64
Candidate multilib: 32;#m32
Selected multilib: .;#m64
"/opt/llvm10-toolchain/bin/clang-10" -cc1 -triple x86_64-unknown-linux-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name now.cpp -mrelocation-model static -mthread-model posix -mframe-pointer=all -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info -fno-split-dwarf-inlining -debugger-tuning=gdb -v -resource-dir /opt/llvm10-toolchain/lib64/clang/10.0.0 -internal-isystem /opt/llvm10-toolchain/bin/../include/c++/v1 -internal-isystem /usr/local/include -internal-isystem /opt/llvm10-toolchain/lib64/clang/10.0.0/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -Wall -Wextra -std=c++17 -fdeprecated-macro -fdebug-compilation-dir /home/gabor -ferror-limit 19 -fmessage-length 0 -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -faddrsig -o /tmp/now-139cc8.o -x c++ now.cpp
clang -cc1 version 10.0.0 based upon LLVM 10.0.0 default target x86_64-unknown-linux-gnu
ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
/opt/llvm10-toolchain/bin/../include/c++/v1
/usr/local/include
/opt/llvm10-toolchain/lib64/clang/10.0.0/include
/usr/include
End of search list.
"/opt/llvm10-toolchain/bin/ld.lld" --hash-style=gnu --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o now /usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/crt1.o /usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/crti.o /usr/lib/gcc/x86_64-redhat-linux/4.8.5/crtbegin.o -L/usr/lib/gcc/x86_64-redhat-linux/4.8.5 -L/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64 -L/opt/llvm10-toolchain/bin/../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../.. -L/opt/llvm10-toolchain/bin/../lib -L/lib -L/usr/lib /tmp/now-139cc8.o -lc++ -lm /opt/llvm10-toolchain/lib64/clang/10.0.0/lib/linux/libclang_rt.builtins-x86_64.a -lc /opt/llvm10-toolchain/lib64/clang/10.0.0/lib/linux/libclang_rt.builtins-x86_64.a /usr/lib/gcc/x86_64-redhat-linux/4.8.5/crtend.o /usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/crtn.o
ld.lld: error: unable to find library -lc++
ld.lld: error: cannot open /opt/llvm10-toolchain/lib64/clang/10.0.0/lib/linux/libclang_rt.builtins-x86_64.a: No such file or directory
ld.lld: error: cannot open /opt/llvm10-toolchain/lib64/clang/10.0.0/lib/linux/libclang_rt.builtins-x86_64.a: No such file or directory
clang-10: error: linker command failed with exit code 1 (use -v to see invocation)
For readability, here are the linker search directories mentioned above, in order:
/opt/llvm10-toolchain/lib64
/lib64
/usr/lib64
/usr/lib
/opt/llvm10-toolchain/lib
/lib
/usr/lib
However, libc++ exists in a directory it doesn't search:
> find /opt/llvm10-toolchain -type f -name 'libc++*'
/opt/llvm10-toolchain/lib64/x86_64-unknown-linux-gnu/c++/libc++.so.1.0
/opt/llvm10-toolchain/lib64/x86_64-unknown-linux-gnu/c++/libc++.so
/opt/llvm10-toolchain/lib64/x86_64-unknown-linux-gnu/c++/libc++.a
/opt/llvm10-toolchain/lib64/x86_64-unknown-linux-gnu/c++/libc++abi.so.1.0
/opt/llvm10-toolchain/lib64/x86_64-unknown-linux-gnu/c++/libc++abi.a
As for libclang_rt.builtins-x86_64.a, something similar does exist, but in a different path:
> find /opt/llvm10-toolchain -type f -name 'libclang_rt*'
/opt/llvm10-toolchain/lib/clang/10.0.0/lib/x86_64-unknown-linux-gnu/libclang_rt.builtins.a
I have also tried moving compiler-rt, libcxx, etc. from LLVM_ENABLE_RUNTIMES to LLVM_ENABLE_PROJECTS, but if I do this, compilation fails with an error message while building libc++abi:
/tmp/libc++abi.so.1.0.5xZLnU.ltrans0.ltrans.o::DW.ref.gcc_personality_v0: error: undefined reference to 'gcc_personality_v0'
Even though before it, during CMake configuration, it appears to find it:
-- Looking for gcc_personality_v0 in gcc_s
-- Looking for gcc_personality_v0 in gcc_s - found
I have also tried the same with LLVM 10.0.1-rc1, but it made no difference.

Looks like something in the configuration is off. I'm by far no LLVM/clang expert, so here are my uneducated guesses:
It's not picking up the correct GCC (you mention you have a recent 9.3.0 GCC installed and active while compiling LLVM, but while actually running clang++ it picks up a GCC in version 4.8.5). Be sure to have the same GCC in your environment while running clang++ as you had while compiling it.
You're passing LLVM_INSTALL_TOOLCHAIN_ONLY=On as an option. While looking at this site, it seems that the files needed by your clang++ call shouldn't even be installed (though that patch is rather old).
The differences between the paths to the libclang_rt*s might be caused by LLVM_LIBDIR_SUFFIX=64, though the installation process should handle this case correctly.
I'd recommend building and installing LLVM again and having a closer look at the output. Second, ensure the environments while building and while executing are the same (if you're using environment modules, which I can not recommend enough when using multiple different toolchains, you can define dependencies for this case). After all it's really hard to tell without the build logs and insight into your environment, as I said: these are just some uneducated guesses.

Related

Ubuntu Clang linker error: undefined reference to symbol '__gxx_personality_v0

I wanted to try and use clang instead of gcc on my Ubuntu system. I set up the most basic C++ project possible, with a minimal CMakeLists.txt:
cmake_minimum_required(VERSION 3.20)
project(pcb_engine)
add_executable(main main.cpp)
My main.cpp is also very simple:
#include <iostream>
int main() {
std::cout << "Hello World" << std::endl;
return 0;
}
No when I run g++ -o main_g++ ../main.cpp everything compiles just fine. Also, if I set my system's default compiler to be gcc and g++ using
sudo update-alternatives --config c++
sudo update-alternatives --config cc
and I run cmake .. and make from my build folder, everything works!
The error:
Now I switch to using clang and clang++ as my default compiler via the update-alternatives command and while the build file generation succeeds, the make command fails with the following error message:
fatal error: 'iostream' file not found
#include <iostream>
When I run clang++ -v -stdlib=libc++ -Wall ../main.cpp -o main_clang manually, I get a linker error:
Ubuntu clang version 14.0.0-1ubuntu1
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/11
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/12
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/12
Candidate multilib: .;#m64
Selected multilib: .;#m64
"/usr/lib/llvm-14/bin/clang" -cc1 -triple x86_64-pc-linux-gnu -emit-obj -mrelax-all --mrelax-relocations -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name main.cpp -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -mllvm -treat-scalable-fixed-error-as-warning -debugger-tuning=gdb -v -fcoverage-compilation-dir=/home/ivan/Documents/TEST/build -resource-dir /usr/lib/llvm-14/lib/clang/14.0.0 -internal-isystem /usr/lib/llvm-14/bin/../include/c++/v1 -internal-isystem /usr/lib/llvm-14/lib/clang/14.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -Wall -fdeprecated-macro -fdebug-compilation-dir=/home/ivan/Documents/TEST/build -ferror-limit 19 -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -fcolor-diagnostics -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/main-482542.o -x c++ ../main.cpp
clang -cc1 version 14.0.0 based upon LLVM 14.0.0 default target x86_64-pc-linux-gnu
ignoring nonexistent directory "/usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../x86_64-linux-gnu/include"
ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/lib/llvm-14/bin/../include/c++/v1
/usr/lib/llvm-14/lib/clang/14.0.0/include
/usr/local/include
/usr/include/x86_64-linux-gnu
/usr/include
End of search list.
"/usr/bin/ld" -pie -z relro --hash-style=gnu --build-id --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o main_clang /lib/x86_64-linux-gnu/Scrt1.o /lib/x86_64-linux-gnu/crti.o /usr/bin/../lib/gcc/x86_64-linux-gnu/12/crtbeginS.o -L/usr/bin/../lib/gcc/x86_64-linux-gnu/12 -L/usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../lib64 -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib64 -L/usr/lib/llvm-14/bin/../lib -L/lib -L/usr/lib /tmp/main-482542.o -lc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/bin/../lib/gcc/x86_64-linux-gnu/12/crtendS.o /lib/x86_64-linux-gnu/crtn.o
/usr/bin/ld: /tmp/main-482542.o: undefined reference to symbol '__gxx_personality_v0'
/usr/bin/ld: /lib/x86_64-linux-gnu/libc++abi.so.1: error adding symbols: DSO missing from command line
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The non-verbose error message:
/usr/bin/ld: /tmp/main-f39fec.o: undefined reference to symbol '__gxx_personality_v0'
/usr/bin/ld: /lib/x86_64-linux-gnu/libc++abi.so.1: error adding symbols: DSO missing from command line
Can anyone help me identify where to start fixing this?
Interestingly, when I run clang++ -v -stdlib=libstdc++ -Wall ../main.cpp -o main_clang (note the -stdlib=libstdc++ flag change to the gnu standard library), then I get the same simple fatal error: 'iostream' file not found error as with cmake! So there is a difference in the behavior with the two different standard c++ libraries...
The background:
I come from using gcc, and installed clang via get-apt install clang. I've read that clang does not come with its own standard C++ library, but I have trouble understanding the implications for me: Previously, I had issues with clang finding a correct standard library .so file... I tried some more installs and I have got the libstdc++.so (default gnu) and the libc++.so as well as libc++.a and libc++.abi.so in my /usr/lib/x86_64-linux-gnu folder with a correct symlink hierarchy. For some reason, after a system restart, the /usr/bin/ld: cannot find -lstdc++ error went away, and now I have this linker error where it seems that clang can not find the include header files when running cmake. Now, I am at the end of my limited wisdom.

Why does lld-link say that the symbol "__memset_nt_iters" is undefined when cross-compiling from Linux to Windows?

I am trying to use clang to cross-compile to Windows on Linux. I think that I need to get the standard library stuff from elsewhere, so I used -v when compiling on Windows to see what Windows clang was using, and then copied those files to Linux. Right now I am compiling using a script that looks something like this:
#!/bin/bash
clang++ -fuse-ld=lld -target x86_64-pc-windows-msvc \
-I /home/.../include/clang \
-I /home/.../include/msvc \
-I /home/.../include/atlmfc \
-I /home/.../include/ucrt \
-I /home/.../include/shared \
-I /home/.../include/um \
-I /home/.../include/winrt \
-L /home/.../lib/msvc \
-L /home/.../lib/atlmfc \
-L /home/.../lib/ucrt \
-L /home/.../lib/um \
-L /home/.../lib/clang \
$#
This is the test program that I am trying to compile:
int main(int argc, char* argv[])
{
return argc;
}
At first, I ran into an issue where some libraries (kernel32.Lib and Uuid.Lib) couldn't be found, but it seemed to arise from case-sensitivity, so this was easily fixed using two all-lowercase symlinks.
But now lld-link is complaining about how "__memset_nt_iters" is undefined, and I have no idea what this is about. Look at the verbose output:
clang version 10.0.0-4ubuntu1
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: /usr/bin
"/usr/lib/llvm-10/bin/clang" -cc1 -triple x86_64-pc-windows-msvc19.11.0 -emit-obj -mrelax-all -mincremental-linker-compatible -disable-free -disable-llvm-verifier -discard-value-names -main-file-name ret.cpp -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=none -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info -v -resource-dir /usr/lib/llvm-10/lib/clang/10.0.0 -I /home/.../include/clang -I /home/.../include/msvc -I /home/.../include/atlmfc -I /home/.../include/ucrt -I /home/.../include/shared -I /home/.../include/um -I /home/.../include/winrt -internal-isystem /usr/lib/llvm-10/lib/clang/10.0.0/include -fdeprecated-macro -fdebug-compilation-dir /home/... -ferror-limit 19 -fmessage-length 0 -fno-use-cxa-atexit -fms-extensions -fms-compatibility -fms-compatibility-version=19.11 -std=c++14 -fdelayed-template-parsing -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -faddrsig -o /tmp/ret-004e99.o -x c++ ret.cpp
clang -cc1 version 10.0.0 based upon LLVM 10.0.0 default target x86_64-pc-linux-gnu
#include "..." search starts here:
#include <...> search starts here:
/home/.../include/clang
/home/.../include/msvc
/home/.../include/atlmfc
/home/.../include/ucrt
/home/.../include/shared
/home/.../include/um
/home/.../include/winrt
/usr/lib/llvm-10/lib/clang/10.0.0/include
End of search list.
"/usr/lib/llvm-10/bin/lld-link" -out:ret.exe -libpath:lib/amd64 -libpath:atlmfc/lib/amd64 -libpath:/home/.../lib/msvc -libpath:/home/.../lib/atlmfc -libpath:/home/.../lib/ucrt -libpath:/home/.../lib/um -libpath:/home/.../lib/clang -nologo /tmp/ret-004e99.o libcmt.lib
lld-link: error: undefined symbol: __memset_nt_iters
>>> referenced by D:\a01\_work\9\s\src\vctools\crt\vcruntime\src\string\amd64\memset.asm:134
>>> libvcruntime.lib(memset.obj):(XmmSetLarge)
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I have now managed to get it to compile. What I did was add the arguments -Xlinker /force, which make the linker ignore the fact that there's an undefined symbol and continue anyway. The resulting executable (from compiling the test program) seems to run fine, at least in Wine.
What's interesting is that when I compiled a larger project the error (now warning) about "__memset_nt_iters" went away. I have not been able to isolate what solved the issue in that case.

Not able to Cross-compile and link source code using afl-clang-fast++

I am trying to fuzz a simple C++ code using afl plus plus clang compiler (llvm_mode). I'm able to fuzz the code and generate the ARM object file but it fails while linking. I guess i'm missing some linker options. Any help would be appreciated
Following is my command:
afl-clang-fast++ --target=arm-linux-gnueabihf --rtlib=compiler-rt --stdlib=libc++ -nostdinc++ -I${root}/include/c++/v1 -Wl,-L${root}/lib --sysroot ${sysroot} --gcc-toolchain=/home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf -rpath ${root}/lib TestCodeX86toARM.cpp -o Test -v
root and sysroot values are as follows
root=/path/to/clang/install_dir
sysroot=/path/to/linarogcc/arm-linux-gnueabihf/libc
From the output, i can conclude that with the help of Linaro tool-chain, compilation is going through but linking is failing
Output:
afl-clang-fast++2.66d by <lszekeres#google.com> in CLASSIC mode
clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final)
Target: arm--linux-gnueabihf
Thread model: posix
InstalledDir: /usr/lib/llvm-6.0/bin
Found candidate GCC installation: /home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/lib/gcc/arm-linux-gnueabihf/7.5.0
Selected GCC installation: /home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/lib/gcc/arm-linux-gnueabihf/7.5.0
Candidate multilib: .;#m32
Selected multilib: .;#m32
"/usr/lib/llvm-6.0/bin/clang" -cc1 -triple armv6kz--linux-gnueabihf -emit-obj -disable-free -disable-llvm-verifier -discard-value-names -main-file-name TestCodeX86toARM.cpp -mrelocation-model static -mthread-model posix -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -fuse-init-array -target-cpu arm1176jzf-s -target-feature +strict-align -target-abi aapcs-linux -mfloat-abi hard -fallow-half-arguments-and-returns -dwarf-column-info -debug-info-kind=limited -dwarf-version=4 -debugger-tuning=gdb -v -nostdinc++ -resource-dir /usr/lib/llvm-6.0/lib/clang/6.0.0 -I /home/user/Tejas/clang+llvm-10.0.0-x86_64-linux-gnu-ubuntu-18.04/include/c++/v1 -D __AFL_HAVE_MANUAL_CONTROL=1 -D __AFL_COMPILER=1 -D FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION=1 -D __AFL_FUZZ_INIT()=int __afl_sharedmem_fuzzing = 1;extern unsigned int *__afl_fuzz_len;extern unsigned char *__afl_fuzz_ptr;unsigned char __afl_fuzz_alt[1024000];unsigned char *__afl_fuzz_alt_ptr = __afl_fuzz_alt; -D __AFL_FUZZ_TESTCASE_BUF=(__afl_fuzz_ptr ? __afl_fuzz_ptr : __afl_fuzz_alt_ptr) -D __AFL_FUZZ_TESTCASE_LEN=(__afl_fuzz_ptr ? *__afl_fuzz_len : (*__afl_fuzz_len = read(0, __afl_fuzz_alt_ptr, 1024000)) == 0xffffffff ? 0 : *__afl_fuzz_len) -D "__AFL_LOOP(_A)=({ static volatile char *_B __attribute__((used)); _B = (char*)\"##SIG_AFL_PERSISTENT##\"; __attribute__((visibility(\"default\"))) int _L(unsigned int) __asm__(\"__afl_persistent_loop\"); _L(_A); })" -D "__AFL_INIT()=do { static volatile char *_A __attribute__((used)); _A = (char*)\"##SIG_AFL_DEFER_FORKSRV##\"; __attribute__((visibility(\"default\"))) void _I(void) __asm__(\"__afl_manual_init\"); _I(); } while (0)" -isysroot /home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/arm-linux-gnueabihf/libc -internal-isystem /home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/arm-linux-gnueabihf/libc/usr/local/include -internal-isystem /usr/lib/llvm-6.0/lib/clang/6.0.0/include -internal-externc-isystem /home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/arm-linux-gnueabihf/libc/include -internal-externc-isystem /home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/arm-linux-gnueabihf/libc/usr/include -O3 -Wno-unused-command-line-argument -fdeprecated-macro -fdebug-compilation-dir /home/user/Tejas/CrossCopileTestCode -ferror-limit 19 -fmessage-length 87 -funroll-loops -fno-signed-char -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -vectorize-loops -vectorize-slp -load /usr/local/lib/afl/afl-llvm-pass.so -o /tmp/TestCodeX86toARM-c0d8ac.o -x c++ TestCodeX86toARM.cpp
clang -cc1 version 6.0.0 based upon LLVM 6.0.0 default target x86_64-pc-linux-gnu
ignoring nonexistent directory "/home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/arm-linux-gnueabihf/libc/usr/local/include"
ignoring nonexistent directory "/home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/arm-linux-gnueabihf/libc/include"
#include "..." search starts here:
#include <...> search starts here:
/home/user/Tejas/clang+llvm-10.0.0-x86_64-linux-gnu-ubuntu-18.04/include/c++/v1
/usr/lib/llvm-6.0/lib/clang/6.0.0/include
/home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/arm-linux-gnueabihf/libc/usr/include
End of search list.
afl-llvm-pass++2.66d by <lszekeres#google.com> and <adrian.herrera#anu.edu.au>
[+] Instrumented 3 locations (non-hardened mode, ratio 100%).
"/home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/lib/gcc/arm-linux-gnueabihf/7.5.0/../../../../arm-linux-gnueabihf/bin/ld" --sysroot=/home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/arm-linux-gnueabihf/libc -z relro -X --hash-style=gnu --eh-frame-hdr -m armelf_linux_eabi -dynamic-linker /lib/ld-linux-armhf.so.3 -o Test /home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/arm-linux-gnueabihf/libc/usr/lib/../lib/crt1.o /home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/arm-linux-gnueabihf/libc/usr/lib/../lib/crti.o /home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/lib/gcc/arm-linux-gnueabihf/7.5.0/crtbegin.o -L/home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/lib/gcc/arm-linux-gnueabihf/7.5.0 -L/home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/lib/gcc/arm-linux-gnueabihf/7.5.0/../../../../arm-linux-gnueabihf/lib/../lib -L/home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/arm-linux-gnueabihf/libc/lib/../lib -L/home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/arm-linux-gnueabihf/libc/usr/lib/../lib -L/home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/lib/gcc/arm-linux-gnueabihf/7.5.0/../../../../arm-linux-gnueabihf/lib -L/home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/arm-linux-gnueabihf/libc/lib -L/home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/arm-linux-gnueabihf/libc/usr/lib -L/home/user/Tejas/clang+llvm-10.0.0-x86_64-linux-gnu-ubuntu-18.04/lib -rpath /home/user/Tejas/clang+llvm-10.0.0-x86_64-linux-gnu-ubuntu-18.04/lib /tmp/TestCodeX86toARM-c0d8ac.o /usr/local/lib/afl/afl-llvm-rt.o -lc++ -lm /usr/lib/llvm-6.0/lib/clang/6.0.0/lib/linux/libclang_rt.builtins-armhf.a -lc /usr/lib/llvm-6.0/lib/clang/6.0.0/lib/linux/libclang_rt.builtins-armhf.a /home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/lib/gcc/arm-linux-gnueabihf/7.5.0/crtend.o /home/user/Tejas/LLVM/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/arm-linux-gnueabihf/libc/usr/lib/../lib/crtn.o
/usr/local/lib/afl/afl-llvm-rt.o: file not recognized: File format not recognized
clang: error: linker command failed with exit code 1 (use -v to see invocation)

How to compile with clang a Google Benchmark using libc++

I would like to compile the example given in the Google Benchmark documentation with clang using libc++:
#include <benchmark/benchmark.h>
static void BM_StringCreation(benchmark::State& state) {
for (auto _ : state)
std::string empty_string;
}
// Register the function as a benchmark
BENCHMARK(BM_StringCreation);
// Define another benchmark
static void BM_StringCopy(benchmark::State& state) {
std::string x = "hello";
for (auto _ : state)
std::string copy(x);
}
BENCHMARK(BM_StringCopy);
BENCHMARK_MAIN();
I use the following command:
clang++ -stdlib=libc++ example.cpp -lbenchmark -lpthread -o example
And I got a lot of linker errors:
[...]
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The full output is available here (more than 1500 lines). If I use the -v option, I can see:
clang version 7.0.1-8 (tags/RELEASE_701/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/8
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/8
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/8
Candidate multilib: .;#m64
Selected multilib: .;#m64
"/usr/lib/llvm-7/bin/clang" -cc1 -triple x86_64-pc-linux-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name example.cpp -mrelocation-model static -mthread-model posix -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -v -resource-dir /usr/lib/llvm-7/lib/clang/7.0.1 -internal-isystem /usr/lib/llvm-7/bin/../include/c++/v1 -internal-isystem /usr/include/clang/7.0.1/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-7/lib/clang/7.0.1/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdeprecated-macro -fdebug-compilation-dir /root -ferror-limit 19 -fmessage-length 0 -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -o /tmp/example-9660e7.o -x c++ example.cpp -faddrsig
clang -cc1 version 7.0.1 based upon LLVM 7.0.1 default target x86_64-pc-linux-gnu
ignoring nonexistent directory "/include"
ignoring duplicate directory "/usr/include/clang/7.0.1/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/lib/llvm-7/bin/../include/c++/v1
/usr/include/clang/7.0.1/include
/usr/local/include
/usr/include/x86_64-linux-gnu
/usr/include
End of search list.
"/usr/bin/ld" --hash-style=both --build-id --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o example /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu/crt1.o /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu/crti.o /usr/bin/../lib/gcc/x86_64-linux-gnu/8/crtbegin.o -L/usr/bin/../lib/gcc/x86_64-linux-gnu/8 -L/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../.. -L/usr/lib/llvm-7/bin/../lib -L/lib -L/usr/lib /tmp/example-9660e7.o -lbenchmark -lpthread -lc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/bin/../lib/gcc/x86_64-linux-gnu/8/crtend.o /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu/crtn.o
[...]
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I don't know if the issue comes from my clang or my Google Benchmark installation. Here is the script I made to install both:
#!/bin/sh
apt-get -y install clang libc++-dev libc++abi-dev git cmake
git clone https://github.com/google/benchmark.git
git clone https://github.com/google/googletest.git benchmark/googletest
pushd benchmark
cmake -E make_directory "build"
cmake -E chdir "build" cmake -DCMAKE_BUILD_TYPE=Release -DLLVMAR_EXECUTABLE=/usr/bin/llvm-ar-7 -DLLVMNM_EXECUTABLE=/usr/bin/llvm-nm-7 -DLLVMRANLIB_EXECUTABLE=/usr/bin/llvm-ranlib-7 ../
cmake --build "build" --config Release --target install
popd
During the second cmake command, I have the following message:
CMake Warning:
Manually-specified variables were not used by the project:
LLVMAR_EXECUTABLE
LLVMNM_EXECUTABLE
LLVMRANLIB_EXECUTABLE
Does it mean Google Benchmark is not build using libc++ and I can't link it to my benchmark?
How can I fix this issue?
Here is my system information:
$ uname -a
Linux 1b3149ded49c 4.9.0-8-amd64 #1 SMP Debian 4.9.144-3.1 (2019-02-19) x86_64 GNU/Linux
In the GitHub repository of the project is a travis file. The line corresponding to the cmake command is:
cmake -DCMAKE_C_COMPILER=${C_COMPILER} -DCMAKE_CXX_COMPILER=${COMPILER} -DCMAKE_BUILD_TYPE=${BUILD_TYPE} -DCMAKE_C_FLAGS="${EXTRA_FLAGS}" -DCMAKE_CXX_FLAGS="${EXTRA_FLAGS} ${EXTRA_CXX_FLAGS}" -DBENCHMARK_DOWNLOAD_DEPENDENCIES=ON -DBENCHMARK_BUILD_32_BITS=${BUILD_32_BITS} ${EXTRA_OPTIONS} ..
All I have to do is replace the travis variables by what I want:
cmake -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS="-stdlib=libc++" -DBENCHMARK_DOWNLOAD_DEPENDENCIES=ON ../
After installation, I can compile my benchmark with clang and libc++:
clang++ -stdlib=libc++ example.cpp -lbenchmark -lpthread -o example

Clang fails to compile with -m32 on open SUSE Leap 15

I've a SUSE Leap 15 virtual machine, and installed clang5 by zypper and I want to compile a software, that must be compiled to 32bit, but it seems clang wont compile a simple example with -m32. (necessary 32bit libs are installed).
I'm trying to compile this file:
#include <iostream>
int main()
{
auto x = 2;
std::cout << x << std::endl;
}
With the following command (without -m32 it works fine):
clang++ main.cpp -o alma -std=c++17 -m32 -v
clang version 5.0.1 (tags/RELEASE_501/final 312548)
Target: i386-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib64/gcc/x86_64-suse-linux/7
Found candidate GCC installation: /usr/lib64/gcc/x86_64-suse-linux/7
Selected GCC installation: /usr/bin/../lib64/gcc/x86_64-suse-linux/7
Candidate multilib: .;#m64
Candidate multilib: 32;#m32
Selected multilib: 32;#m32
"/usr/bin/clang-5.0.1" -cc1 -triple i386-unknown-linux-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name main.cpp -mrelocation-model static -mthread-model posix -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -fuse-init-array -target-cpu i586 -v -dwarf-column-info -debugger-tuning=gdb -resource-dir /usr/lib64/clang/5.0.1 -internal-isystem /usr/bin/../lib64/gcc/x86_64-suse-linux/7/../../../../include/c++/7 -internal-isystem /usr/bin/../lib64/gcc/x86_64-suse-linux/7/../../../../include/c++/7/x86_64-suse-linux/32 -internal-isystem /usr/bin/../lib64/gcc/x86_64-suse-linux/7/../../../../include/c++/7/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib64/clang/5.0.1/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -std=c++17 -fdeprecated-macro -fdebug-compilation-dir /home/martonka/test -ferror-limit 19 -fmessage-length 227 -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o /tmp/main-d5b20e.o -x c++ main.cpp
clang -cc1 version 5.0.1 based upon LLVM 5.0.1 default target x86_64-unknown-linux-gnu
ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/bin/../lib64/gcc/x86_64-suse-linux/7/../../../../include/c++/7
/usr/bin/../lib64/gcc/x86_64-suse-linux/7/../../../../include/c++/7/x86_64-suse-linux/32
/usr/bin/../lib64/gcc/x86_64-suse-linux/7/../../../../include/c++/7/backward
/usr/local/include
/usr/lib64/clang/5.0.1/include
/usr/include
End of search list.
"/usr/bin/ld" -z relro --hash-style=gnu --hash-style=both --build-id --enable-new-dtags --eh-frame-hdr -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 -o alma /usr/bin/../lib64/gcc/x86_64-suse-linux/7/../../../crt1.o /usr/bin/../lib64/gcc/x86_64-suse-linux/7/../../../crti.o /usr/bin/../lib64/gcc/x86_64-suse-linux/7/32/crtbegin.o -L/usr/bin/../lib64/gcc/x86_64-suse-linux/7/32 -L/usr/bin/../lib64/gcc/x86_64-suse-linux/7 -L/usr/bin/../lib64/gcc/x86_64-suse-linux/7/../../../../x86_64-suse-linux/lib -L/usr/bin/../lib64/gcc/x86_64-suse-linux/7/../../.. -L/usr/bin/../lib -L/lib -L/usr/lib /tmp/main-d5b20e.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/bin/../lib64/gcc/x86_64-suse-linux/7/32/crtend.o /usr/bin/../lib64/gcc/x86_64-suse-linux/7/../../../crtn.o
/usr/bin/ld: skipping incompatible /usr/bin/../lib64/gcc/x86_64-suse-linux/7/../../../libm.so when searching for -lm
/usr/bin/ld: skipping incompatible /usr/bin/../lib64/gcc/x86_64-suse-linux/7/../../../libc.so when searching for -lc
/usr/bin/ld: i386:x86-64 architecture of input file `/usr/bin/../lib64/gcc/x86_64-suse-linux/7/../../../crt1.o' is incompatible with i386 output
/usr/bin/ld: i386:x86-64 architecture of input file `/usr/bin/../lib64/gcc/x86_64-suse-linux/7/../../../crti.o' is incompatible with i386 output
/usr/bin/ld: i386:x86-64 architecture of input file `/usr/bin/../lib64/gcc/x86_64-suse-linux/7/../../../crtn.o' is incompatible with i386 output
clang-5.0.1: error: unable to execute command: Segmentation fault (core dumped)
clang-5.0.1: error: linker command failed due to signal (use -v to see invocation)
" g++ main.cpp -o alma -std=c++17 -m32 " works fine.
It seems the problem is that clang passes the 64 bit versions of crtn.o, crti.o and crt1.o to ld.
If I just compile an object file:
clang++ main.cpp -std=c++17 -c -o alma.o
and then execute the above linker command with crtn.o, crti.o and crt1.o paths fixed, then it works.
It looks like clang thinks that g++ is under /usr/lib/ (which is true on ubuntu, centos, but not on suse), and it just adds ../../../ to reach the 32bit libraries, which is clearly wrong.
How can this be fixed?
UPDATE: After a bit more searching
here is an open suse bug for this: bugzilla.suse.com/show_bug.cgi?id=1051881#c4
And an open llvm bug: bugs.llvm.org/show_bug.cgi?id=12108
In the second one, there is a workaround:
cd /usr/lib64/gcc/x86_64-suse-linux/7/32
ln -s /usr/lib/crt1.o
ln -s /usr/lib/crti.o
ln -s /usr/lib/crtn.o
But clang still gives warnings:
/usr/bin/ld: skipping incompatible /usr/bin/../lib64/gcc/x86_64-suse-linux/7/../../../libm.so when searching for -lm
/usr/bin/ld: skipping incompatible /usr/bin/../lib64/gcc/x86_64-suse-linux/7/../../../libc.so when searching for -lc
Is there any better solution?