Linker issue when using both --sysroot and --gcc_toolchain: libraries not found

Hi!

I have been using --gcc_toolchain with clang successfully to build my C++ application using a custom GCC toolchain:

clang --gcc-toolchain=path/to/gcc/usr /tmp/main.cpp -lstdc++ -o /tmp/main

Now, I want to have a custom libc too, so I thought using --sysroot would be right way of doing it:

clang --sysroot=path/to/sysroot --gcc-toolchain=path/to/gcc/usr /tmp/main.cpp -lstdc++ -o /tmp/main

However this leads to a linker error: cannot find libstdc++ nor libgcc_s:

/usr/bin/ld: cannot find -lstdc++
/usr/bin/ld: cannot find -lgcc_s

Inspecting the linker command, I notice that without --sysroot, Clang adds this line:

-Lpath/to/gcc/usr/lib/gcc/x86_64-pc-linux-gnu/7.5.0/../../../../lib64

Whereas if I do add --sysroot, clang does not add that line. Indeed that folder is where the missing libraries are to be found.

Is this expected behavior? I could add the -L line myself but I thought it was very neat that it was automagically taken care of by --gcc_toolchain.

Thanks!

If you are using a sysroot you should have libstdc++ and libgcc_s in the sysroot as well. Since these are linked directly into the binary it will have to come from the same distribution as libc, otherwise you can have conflicts.

Or you can replace libstdc++ with libc++ and libgcc with compiler-rt which both are available in the LLVM distribution.

Since these are linked directly into the binary it will have to come from the same distribution as libc, otherwise you can have conflicts.

Hmm, why is that? I mean, using --gcc_toolchain, the compiler will use libstdc++ and libgcc_s from my custom GCC build, even though I have a much older libstdc++ and libgcc_s installed in my system alongside libc. That’s fine, right?

Or you can replace libstdc++ with libc++ and libgcc

I’d love that, unfortunately I have some pre-built 3rd-party libraries with C++ interfaces so I can’t use libc++ (different internal namespaces).