I am not sure if there is a practical value in building LLVM+Clang linked with libc++ as opposed to libstdc++. There may be, but in my testing, Clang linked with libstdc++ could be used to build software linked with libc++/libc++abi. But, in case there is a value in this or you are just as curious as I am, here we go!
Assuming we are in the build directory and the script is named clang++w and is in the path, the LLVM/Clang/libc++(abi) source tree, located in ../llvm_src in this example, can be configured as follows:
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++w -G "Unix Makefiles" ../llvm_src/
lib/Support/CMakeLists.txt
to add the following line after everything else was set in the system_libs
variable:
set(system_libs ${system_libs} c++ c++abi c m gcc_s gcc)
See below about the files that had to be edited in the build directory.
make
in the build directory. If cmake was used with a clang++ wrapper, everything should go smoothly. If we edited the CMake files, then we may need to edit some link.txt files in the build directory. If a build fails at some point, we can run make VERBOSE=1
to see what the command line was and edit the appropriate link.txt file to append -lc++ -lc++abi -lm -lc -lgcc_s -lgcc
to the command line found in the file. In my case, with Clang 3.6.2, I had to edit the following files:
utils/PerfectShuffle/CMakeFiles/llvm-PerfectShuffle.dir/link.txt
utils/count/CMakeFiles/count.dir/link.txt
tools/clang/tools/c-index-test/CMakeFiles/c-index-test.dir/link.txt
tools/clang/tools/c-arcmt-test/CMakeFiles/c-arcmt-test.dir/link.txt
Now do make install
, and you have an LLVM, Clang, and libc++/libc++abi installation in /usr/local/lib
, which is where it goes by default. Congratulations!
make check-all
after building your code, it may fail with the following error:
/tmp/cxa_thread_atexit_test-aed951.o: In function `main':
/root/llvm_src/projects/libcxxabi/test/cxa_thread_atexit_test.cpp:(.text+0x115): undefined reference to `__cxa_thread_atexit'
Looking at the source being compiled, cxa_thread_atexit_test.cpp
, the problem must be with the call to __cxxabiv1::__cxa_thread_atexit()
. The function is defined in projects/libcxxabi/src/cxa_thread_atexit.cpp
, which you can find in your build directory:
namespace __cxxabiv1 {
extern "C" {
#ifdef HAVE___CXA_THREAD_ATEXIT_IMPL
int __cxa_thread_atexit(...) {...}
However, looking at projects/libcxxabi/src/CMakeLists.txt
the macro HAVE___CXA_THREAD_ATEXIT_IMPL
is conditionally defined thus:
if (LIBCXXABI_HAS_CXA_THREAD_ATEXIT_IMPL)
add_definitions(-DHAVE___CXA_THREAD_ATEXIT_IMPL)
endif()
An in projects/libcxxabi/cmake/config-ix.cmake
:
check_library_exists(c __cxa_thread_atexit_impl ""
LIBCXXABI_HAS_CXA_THREAD_ATEXIT_IMPL)
So, it's looking for __cxa_thread_atexit_impl()
function in libc. The one on my machine doesn't have such a symbol, which explains the error. I guess the tests could have been written better.