clang appears not to be linking to a library - c++

I boiled down the problem to the following example:
int main()
{
try {
throw false;
} catch (bool x)
{
if (x)
{
return 0;
}
else
{
return 1;
}
}
}
generates the following errors on Coliru:
/tmp/main-c8b47a.o: In function `main':
main.cpp:(.text+0xf): undefined reference to `typeinfo for bool'
/tmp/main-c8b47a.o: In function `GCC_except_table0':
main.cpp:(.gcc_except_table+0x30): undefined reference to `typeinfo for bool'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Cmd line:
clang++ -std=c++11 -stdlib=libc++ -O2 -Wall -pedantic -pthread main.cpp && ./a.out
This sounds like it's not linking to a library. Does anyone know which and what the command line switches would be? I've not used clang before. This works under g++.
This is the output with the -v switch:
clang version 3.6.0 (tags/RELEASE_360/final 235480)
Target: x86_64-unknown-linux-gnu
Thread model: posix
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.6
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.6.4
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.7
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.7.3
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.8
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.8.1
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9.2
Found candidate GCC installation: /usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.8.2
Found candidate GCC installation: /usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.0
Found candidate GCC installation: /usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.2
Found candidate GCC installation: /usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/5.1.0
Found candidate GCC installation: /usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/5.2.0
Selected GCC installation: /usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/5.2.0
Candidate multilib: .;#m64
Selected multilib: .;#m64
"/usr/local/bin/clang" -cc1 -triple x86_64-unknown-linux-gnu -emit-obj -disable-free -disable-llvm-verifier -main-file-name main.cpp -mrelocation-model static -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -target-linker-version 2.22 -momit-leaf-frame-pointer -v -dwarf-column-info -resource-dir /usr/local/bin/../lib/clang/3.6.0 -internal-isystem /usr/include/c++/v1 -internal-isystem /usr/local/include -internal-isystem /usr/local/bin/../lib/clang/3.6.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wall -pedantic -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /tmp/1441759762.34715 -ferror-limit 19 -fmessage-length 0 -pthread -mstackrealign -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -vectorize-loops -vectorize-slp -o /tmp/main-47c098.o -x c++ main.cpp
clang -cc1 version 3.6.0 based upon LLVM 3.6.0 default target x86_64-unknown-linux-gnu
ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/include/c++/v1
/usr/local/include
/usr/local/bin/../lib/clang/3.6.0/include
/usr/include/x86_64-linux-gnu
/usr/include
End of search list.
"/usr/bin/ld" --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o a.out /usr/lib/x86_64-linux-gnu/crt1.o /usr/lib/x86_64-linux-gnu/crti.o /usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/5.2.0/crtbegin.o -L/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/5.2.0 -L/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/5.2.0/../../../../lib64 -L/usr/local/bin/../lib64 -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/5.2.0/../../.. -L/usr/local/bin/../lib -L/lib -L/usr/lib /tmp/main-47c098.o -lc++ -lm -lgcc_s -lgcc -lpthread -lc -lgcc_s -lgcc /usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/5.2.0/crtend.o /usr/lib/x86_64-linux-gnu/crtn.o
/tmp/main-47c098.o: In function `main':
main.cpp:(.text+0xf): undefined reference to `typeinfo for bool'
/tmp/main-47c098.o: In function `GCC_except_table0':
main.cpp:(.gcc_except_table+0x30): undefined reference to `typeinfo for bool'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

It looks like you need to add -lsupc++ after main.cpp (see it live):
clang++ -std=c++11 -stdlib=libc++ -O2 -Wall -pedantic -pthread main.cpp -lsupc++
^^^^^^^^
As Andre Kostur notes the libc++ documentation recommends the following, although I can not seem to get this to work on Coliru:
Unfortunately you can't simply run clang with "-stdlib=libc++" at this
point, as clang is set up to link for libc++ linked to libsupc++. To
get around this you'll have to set up your linker yourself (or patch
clang). For example,
clang++ -stdlib=libc++ helloworld.cpp -nodefaultlibs -lc++ -lcxxrt -lm -lc -lgcc_s -lgcc
Alternately, you could just add libcxxrt to your libraries list, which
in most situations will give the same result:
clang++ -stdlib=libc++ helloworld.cpp -lcxxrt
This looks related to issues being discussed in this thread Making libc++ on Linux user-friendly, with selective quotes below:
Here's the problem: when building libc++, the linker finds the various
ABI functions in libstdc++, and is quite happy with them being there.
When Clang calls the linker for the actual program, though, it doesn't
pass along a link flag for libstdc++, only for libc++. Thus, the links
fails.
and:
This again can be worked around by explicitly specifying linking against the source library, and here -lsupc++ works.
Also see Linux equivalent of Windows DLL forwarders or MacOS reexport_library.

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.

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?

clang on 64bit compile error with -m32

I installed gcc-multilib and g++-multilib but when I try to compile a binary using clang with -m32 It can't find libgcc location properly.. :(
I want to fix it. what should I do? I'm a noob..
root#ubuntu:~# clang -m32 test.c -o test
/usr/bin/ld: skipping incompatible /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/libgcc.a when searching for -lgcc
/usr/bin/ld: cannot find -lgcc
/usr/bin/ld: skipping incompatible /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/libgcc_s.so when searching for -lgcc_s
/usr/bin/ld: cannot find -lgcc_s
clang: error: linker command failed with exit code 1 (use -v to see invocation)
root#ubuntu:~# find / -name libgcc.a
/usr/lib/gcc/x86_64-linux-gnu/4.8/x32/libgcc.a
/usr/lib/gcc/x86_64-linux-gnu/4.8/libgcc.a
/usr/lib/gcc/x86_64-linux-gnu/4.8/32/libgcc.a
root#ubuntu:~# find / -name libgcc_s.so
/usr/lib/gcc/x86_64-linux-gnu/4.8/x32/libgcc_s.so
/usr/lib/gcc/x86_64-linux-gnu/4.8/32/libgcc_s.so
/usr/lib/gcc/x86_64-linux-gnu/4.8/libgcc_s.so
root#ubuntu:~# clang -m32 test.c -o test -v
Ubuntu clang version 3.5-1ubuntu1 (trunk) (based on LLVM 3.5)
Target: i386-pc-linux-gnu
Thread model: posix
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8.2
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9.1
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.8
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.8.2
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9.1
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8
Selected multilib: .;
"/usr/bin/clang" -cc1 -triple i386-pc-linux-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name test.c -mrelocation-model static -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -fuse-init-array -target-cpu pentium4 -target-linker-version 2.24 -v -resource-dir /usr/bin/../lib/clang/3.5 -internal-isystem /usr/local/include -internal-isystem /usr/bin/../lib/clang/3.5/include -internal-externc-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdebug-compilation-dir /root -ferror-limit 19 -fmessage-length 174 -mstackrealign -fobjc-runtime=gcc -fdiagnostics-show-option -fcolor-diagnostics -vectorize-slp -o /tmp/test-7046de.o -x c test.c
clang -cc1 version 3.5 based upon LLVM 3.5 default target x86_64-pc-linux-gnu
ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/usr/bin/../lib/clang/3.5/include
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/include
/usr/include
End of search list.
"/usr/bin/ld" -z relro --hash-style=gnu --build-id --eh-frame-hdr -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 -o test /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../lib32/crt1.o /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../lib32/crti.o /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/crtbegin.o -L/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8 -L/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../lib32 -L/usr/bin/../lib32 -L/lib/../lib32 -L/usr/lib/../lib32 -L/usr/lib/x86_64-linux-gnu/../../lib32 -L/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../.. -L/usr/bin/../lib -L/lib -L/usr/lib /tmp/test-7046de.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/crtend.o /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../lib32/crtn.o
/usr/bin/ld: skipping incompatible /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/libgcc.a when searching for -lgcc
/usr/bin/ld: cannot find -lgcc
/usr/bin/ld: skipping incompatible /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/libgcc_s.so when searching for -lgcc_s
/usr/bin/ld: cannot find -lgcc_s
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I ran into a similar issue while compiling 32 bit statically linked vim on a 64 bit Linux box. I managed to solve that. Here is how the output looked like
Before with errors:
gcc -static -L/usr/lib -Wl,--as-needed -o vim objects/buffer.o objects/blowfish.o ...
/usr/bin/ld: skipping incompatible /usr/lib/libm.a when searching for -lm
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-redhat-linux/4.8.2/../../../libm.a when searching for -lm
...
After downloading and extracting 32 bit static glibc libraries, I still got similar errors:
gcc -static -L/tmp/a/usr/lib -Wl,--as-needed -o vim objects/buffer.o objects/blowfish.o ...
/usr/bin/ld: skipping incompatible /tmp/a/usr/lib/libm.a when searching for -lm
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-redhat-linux/4.8.2/../../../libm.a when searching for -lm
...
Linking succeeded:
gcc -m32 -static -L/tmp/a/usr/lib -Wl,--as-needed -o vim objects/buffer.o objects/blowfish.o ...
Note that I was adding -m32 only to CFLAGS and not to LDFLAGS. That lead to this error which I had to fix. This is how the two look like after the fix:
export CFLAGS="-m32"
export LDFLAGS="-m32 -static"

clang-2.9 crt1 not found error

Just compiled clang-2.9 release and it can't link an hello world example. There is the error:
crt1.o: No such file: No such file or directory
LLVM is configured as default + --enable-shared. The llvm-2.8 build with same options works normally on the same machine.
$ clang -v a.c
clang version 2.9 (tags/RELEASE_29/final)
Target: i386-pc-linux-gnu
Thread model: posix
"/root/bin/clang" -cc1 -triple i386-pc-linux-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name a.c -mrelocation-model static -mdisable-fp-elim -masm-verbose -mconstructor-aliases -target-cpu pentium4 -target-linker-version 2.18 -momit-leaf-frame-pointer -v -resource-dir /root/lib/clang/2.9 -ferror-limit 19 -fmessage-length 157 -fgnu-runtime -fdiagnostics-show-option -fcolor-diagnostics -o /tmp/cc-2ueoYy.o -x c a.c
clang -cc1 version 2.9 based upon llvm 2.9 hosted on i386-pc-linux-gnu
ignoring nonexistent directory "/usr/local/include"
#include "..." search starts here:
#include <...> search starts here:
/root/lib/clang/2.9/include
/usr/include
End of search list.
a.c:1:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
main(){}
^~~~
1 warning generated.
"/usr/bin/ld" --eh-frame-hdr -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 -o a.out crt1.o crti.o crtbegin.o -L -L/../../.. /tmp/cc-2ueoYy.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed crtend.o crtn.o
/usr/bin/ld: crt1.o: No such file: No such file or directory
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The same error was here http://permalink.gmane.org/gmane.comp.compilers.clang.devel/12101
and http://comments.gmane.org/gmane.comp.compilers.clang.devel/14269 and http://permalink.gmane.org/gmane.comp.compilers.llvm.bugs/11352
What I can learn while comparing with llvm-2.8 -- newer version tries to assemble file itself, and older version calls gcc to do assemble and link steps.
Please, this is not a duplicate of clang linker problem as I can't edit sources of LLVM, so the accepted solution isn't useful to me.
As suggested at http://permalink.gmane.org/gmane.comp.compilers.llvm.bugs/11352 (llvm bug 8897) this is because
gcc used to build clang is not in a standard location
or
system gcc is not listed in the ToolChains.cpp GccVersions list:
./tools/clang/lib/Driver/ToolChains.cpp: const char* GccVersions[] = {"4.5.2", "4.5.1", "4.5", "4.4.5", "4.4.4",
./tools/clang/lib/Driver/ToolChains.cpp- "4.4.3", "4.4", "4.3.4", "4.3.3", "4.3.2",
./tools/clang/lib/Driver/ToolChains.cpp- "4.3", "4.2.4", "4.2.3", "4.2.2", "4.2.1",
./tools/clang/lib/Driver/ToolChains.cpp- "4.2"};
Both cases are true for my installation.
To fix this, I'll try to add my version of gcc into list GccVersions[].
This helps me.