Failed to replace stdlibc++ with libc++, linker phase error

Hi,

I’m currently working on one of my team’s project to build LLVM full clang toolchain (Clang, libcxx, libcxxabi) on a CentOS machine.

Previously we compiled our codebase with llvm-toolset-7/clang++, which by default takes stdlibc++ to compile and link. And now we’d like to switch to use LLVM clang with libc++. I have built libc++ and libc++abi from source (5.0.1 release) and set up related flags to compile our code base, but it turned out having some issue in the “linker” phase:

Flags set for complier:
-stdlib=libc++ -std=c++14

Flags set for linker:
-stdlib=libc++

Error:

/opt/rh/devtoolset-7/root/usr/lib/gcc/x86_64-redhat-linux/7/…/…/…/…/bin/ld: product.o: undefined reference to symbol ‘__cxa_free_exception@@CXXABI_1.3
//lib64/libstdc++.so.6: error adding symbols: DSO missing from command line
clang-5.0: error: linker command failed with exit code 1 (use -v to see invocation)

My question is:

  1. We plan to replace stdlibc++ with libc++, and we’ve set up compiler and linker to run with "-stdlib=libc++’, why is it complaining about libstdc++?
  2. How can we resolve this error? (any ideas would be appreciated!)
  3. Is there anything missing if we’d like to replace stdlibc++ with libc++?
  4. I also noticed when it’s in linker phase, these flags were set in the ld command (-lc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc ), which we didn’t have them setup anywhere in our project, just out of curiosity, are they added automatically? Why are they needed?

A little bit more details:
Environment: CentOS Linux release 7.6.1810 (Core)
Clang version:

clang version 5.0.1 (tags/RELEASE_501/final)

Target: x86_64-unknown-linux-gnu

Thread model: posix

Thanks for any ideas,
Aichi

Hi,

I'm currently working on one of my team's project to build LLVM full clang toolchain (Clang, libcxx, libcxxabi) on a CentOS machine.

Previously we compiled our codebase with llvm-toolset-7/clang++, which by default takes stdlibc++ to compile and link. And now we'd like to switch to use LLVM clang with libc++. I have built libc++ and libc++abi from source (5.0.1 release) and set up related flags to compile our code base, but it turned out having some issue in the "linker" phase:

Flags set for complier:
-stdlib=libc++ -std=c++14

Flags set for linker:
-stdlib=libc++

Error:
/opt/rh/devtoolset-7/root/usr/lib/gcc/x86_64-redhat-linux/7/../../../../bin/ld: product.o: undefined reference to symbol '__cxa_free_exception@@CXXABI_1.3'
//lib64/libstdc++.so.6: error adding symbols: DSO missing from command line
clang-5.0: error: linker command failed with exit code 1 (use -v to see invocation)

My question is:
1. We plan to replace stdlibc++ with libc++, and we've set up compiler and linker to run with "-stdlib=libc++', why is it complaining about libstdc++?

Can you post the full linker invocation with all the arguments?

What cmake arguments did you use when building libcxx and libcxxabi?

2. How can we resolve this error? (any ideas would be appreciated!)

Not sure exactly, I would try passing -DLIBCXX_CXX_ABI=libcxxabi to cmake
when configuring libcxx if you have not already.

3. Is there anything missing if we'd like to replace stdlibc++ with libc++?
4. I also noticed when it's in linker phase, these flags were set in the ld command (-lc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc ), which we didn't have them setup anywhere in our project, just out of curiosity, are they added automatically? Why are they needed?

When you are using clang++ or g++ as the linker driver, then it automatically
adds the flags for you.

-Tom

Hi,

I'm currently working on one of my team's project to build LLVM full clang toolchain (Clang, libcxx, libcxxabi) on a CentOS machine.

Previously we compiled our codebase with llvm-toolset-7/clang++, which by default takes stdlibc++ to compile and link. And now we'd like to switch to use LLVM clang with libc++. I have built libc++ and libc++abi from source (5.0.1 release) and set up related flags to compile our code base, but it turned out having some issue in the "linker" phase:

Flags set for complier:
-stdlib=libc++ -std=c++14

Flags set for linker:
-stdlib=libc++

Error:
/opt/rh/devtoolset-7/root/usr/lib/gcc/x86_64-redhat-linux/7/../../../../bin/ld: product.o: undefined reference to symbol '__cxa_free_exception@@CXXABI_1.3'
//lib64/libstdc++.so.6: error adding symbols: DSO missing from command line
clang-5.0: error: linker command failed with exit code 1 (use -v to see invocation)

My question is:
1. We plan to replace stdlibc++ with libc++, and we've set up compiler and linker to run with "-stdlib=libc++', why is it complaining about libstdc++?

Can you post the full linker invocation with all the arguments?

What cmake arguments did you use when building libcxx and libcxxabi?

2. How can we resolve this error? (any ideas would be appreciated!)

Not sure exactly, I would try passing -DLIBCXX_CXX_ABI=libcxxabi to cmake
when configuring libcxx if you have not already.

If you are looking for a quick fix though, you might want to also try adding
-lc++abi to your linker flags.

-Tom

Hi Tom,

Thanks for your replies.

Following are the commands that I used to build clang/libcxx/libcxxabi:
clang:
cmake -DLLVM_ENABLE_PROJECTS=``"clang;libcxx;libcxxabi" -G ``"Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DLLVM_LIBDIR_SUFFIX=64 ..``/llvm

libcxx:
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr -DLLVM_LIBDIR_SUFFIX=64 -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ …``

libcxxabi:

cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=``/usr -DLLVM_LIBDIR_SUFFIX=64 -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DLIBCXXABI_LIBCXX_INCLUDES=../..``/libcxx/include ..

And based on what I searched on google, libcxx and libcxxabi might have circular dependency (please see the post here: https://stackoverflow.com/questions/25840088/how-to-build-libcxx-and-libcxxabi-by-clang-on-centos-7), so after I built libcxx with clang, build libcxxabi based on libcxx, I rebuilt libcxx again with libcxxabi with the following command:
`
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr -DLLVM_LIBDIR_SUFFIX=64 -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DLIBCXX_CXX_ABI=libcxxabi -DLIBCXX_CXX_ABI_INCLUDE_PATHS=…/…/libcxxabi/include …

I’ve tried what you suggested in your previous email (add linker flag: -lc++abi), but unfortunately I got the same error while building my project. Am I missing anything?

Thank you all,
Aichi

`
``









``

Hi Tom,

Thanks for your replies.

Following are the commands that I used to build clang/libcxx/libcxxabi:
clang:
> cmake -DLLVM_ENABLE_PROJECTS=||"clang;libcxx;libcxxabi"| |-G ||"Unix Makefiles"| |-DCMAKE_BUILD_TYPE=Release -DLLVM_LIBDIR_SUFFIX=64 ..||/llvm|
>
>
libcxx:
>> cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=||/usr| |-DLLVM_LIBDIR_SUFFIX=64 -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ ..|
>
>>
>>
libcxxabi:
> cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=||/usr| |-DLLVM_LIBDIR_SUFFIX=64 -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DLIBCXXABI_LIBCXX_INCLUDES=../..||/libcxx/include| |..|
>

You should only need to run cmake once. Here is a minimal example build process that
I think should work:

git clone --branch release/5.x https://github.com/llvm/llvm-project
cd llvm-project
mkdir build
cd build
cmake -DLLVM_ENABLE_PROJECTS="libcxx;libcxxabi" -DCMAKE_INSTALL_PREFIX=/usr -DLLVM_LIBDIR_SUFFIX=64 ../llvm/
make install-cxx

Then verify that libc++.so is actually a linker script with the correct libraries:

cat /usr/lib64/libc++.so

The output should look like this:

INPUT(libc++.so.1 -lc++abi)

-Tom