40

I am wondering what is the right/easy way to install a binary libc++ on Ubuntu, in my case Trusty aka 14.04?

On the LLVM web site there are apt packages http://apt.llvm.org/ and I have used these to install 3.9. However these packages don't seem to include libc++. I install the libc++-dev package but that seems to be a really old version. There are also binaries that can be downloaded http://llvm.org/releases/download.html#3.9.0. These do seem to contain libc++ but I'm not sure if I can just copy bits of this into places like /usr/include/c++/v1, in fact I'm not really sure what bits I would need to copy. I am aware I can use libc++ from an alternate location as documented here http://libcxx.llvm.org/docs/UsingLibcxx.html which I have tried. However I can't modify the build system of the large code base I work on to do this.

So is three any reason the apt packages don't include libc++ and any pointers to installing a binary would be gratefully recieved.

0

6 Answers 6

39

How to build libc++ on Ubuntu 16.04

I had a similar issue as you do. While testing clang with libstdc++ worked fine with C++11 and C++14 there still might be licensing issues with libstdc++. So I ended up installing Clang toolchain from their repos and compiling libc++ on Ubuntu 16.04.

Disclaimer: This post is summary of long search on how to build the libc++ on Ubuntu Linux. Many of the posts I found in 2017 were either outdated or described a partial solution on other systems e.g. CentOS. Links to these posts are:

Here are the steps to build LLVM + Clang + libc++ from the 4.0 release branch:

  1. Install the key of LLVM Repositories

    # apt-get update && apt-get dist-upgrade -y && apt-get install -y vim curl && \
         curl -q https://apt.llvm.org/llvm-snapshot.gpg.key |apt-key add -
    
  2. Create a new new APT Repository File (you can also exclude 2 lines referring to v3.9 repos)

    # cat > /etc/apt/sources.list.d/llvm-repos.list << EOF
         deb http://apt.llvm.org/xenial/ llvm-toolchain-xenial main
         deb-src http://apt.llvm.org/xenial/ llvm-toolchain-xenial main
         deb http://apt.llvm.org/xenial/ llvm-toolchain-xenial-3.9 main
         deb-src http://apt.llvm.org/xenial/ llvm-toolchain-xenial-3.9 main
         deb http://apt.llvm.org/xenial/ llvm-toolchain-xenial-4.0 main
         deb-src http://apt.llvm.org/xenial/ llvm-toolchain-xenial-4.0 main
         EOF
    
  3. Install Clang and all Packages needed to build libc++ from LLVM repos

        # apt-get update && apt-get install -y clang-4.0 clang-4.0-doc \
       libclang-common-4.0-dev libclang-4.0-dev libclang1-4.0 libclang1-4.0-dbg \
       libllvm4.0 libllvm4.0-dbg lldb-4.0 llvm-4.0 llvm-4.0-dev llvm-4.0-runtime \
       clang-format-4.0 python-clang-4.0 liblldb-4.0-dev lld-4.0 libfuzzer-4.0-dev \
       subversion cmake
    
  4. Create an alternative for C++ compiler and linker. This is not a must, but lets you switch compilers or linkers if needed. Also some build files needed cc or c++ or clang++ as far as I remember. Keep in mind, that we switch to LLD linker as default:

    update-alternatives --install /usr/bin/cc cc /usr/bin/clang-4.0 100 \
     && update-alternatives --install /usr/bin/c++ c++ /usr/bin/clang++-4.0 100 \
     && update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-4.0 100 \
     && update-alternatives --install /usr/bin/clang clang /usr/bin/clang-4.0 100 \
     && update-alternatives --install /usr/bin/ld ld /usr/bin/ld.lld-4.0 10 \
     && update-alternatives --install /usr/bin/ld ld /usr/bin/ld.gold 20 \
     && update-alternatives --install /usr/bin/ld ld /usr/bin/ld.bfd 30 \
     && ld --version && echo 3 | update-alternatives --config ld && ld --version
    
  5. Checkout sources of libc++ and libc++abi:

    $ cd /tmp
    $ svn co http://llvm.org/svn/llvm-project/libcxx/branches/release_40/ libcxx
    $ svn co http://llvm.org/svn/llvm-project/libcxxabi/branches/release_40/ libcxxabi
    $ mkdir -p libcxx/build libcxxabi/build
    
  6. To run libc++ on Linux one needs ABI compatibility to the standard library, e.g. libstdc++. This is where libc++abi comes into game. The only problem is that it needs libc++ to be on the system for which it is build. Thus libc++ is built in 2 steps. First: without any ABI compatibility. But it will be used for bootstrapping of ABI lib and than the second step is to recompile libc++ with the proper ABI present on system:

    Bootstrapping => build libc++ without proper ABI:

    cd /tmp/libcxx/build
    cmake -DCMAKE_BUILD_TYPE=Release -DLLVM_CONFIG_PATH=/usr/bin/llvm-config-4.0\
         -DCMAKE_INSTALL_PREFIX=/usr .. \
    && make install
    

    Building libc++abi with libstdc++ compatible ABI:

    cd /tmp/libcxxabi/build
    CPP_INCLUDE_PATHS=`echo | c++ -Wp,-v -x c++ - -fsyntax-only 2>&1 \
      |grep ' /usr'|tr '\n' ' '|tr -s ' ' |tr ' ' ';'`
    CPP_INCLUDE_PATHS="/usr/include/c++/v1/;$CPP_INCLUDE_PATHS"
    cmake -G "Unix Makefiles" -DLIBCXX_CXX_ABI=libstdc++ \
          -DLIBCXX_LIBSUPCXX_INCLUDE_PATHS="$CPP_INCLUDE_PATHS" \
          -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr \
          -DLLVM_CONFIG_PATH=/usr/bin/llvm-config-4.0 \
          -DLIBCXXABI_LIBCXX_INCLUDES=../../libcxx/include  ..
    make install
    

    Rebuild libc++ with proper ABI lib deployed on system:

    cd /tmp/libcxx/build
    cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr \
          -DLIBCXX_CXX_ABI=libcxxabi -DLLVM_CONFIG_PATH=/usr/bin/llvm-config-4.0\
          -DLIBCXX_CXX_ABI_INCLUDE_PATHS=../../libcxxabi/include .. \
    && make install
    
  7. Create a test file to check whether everything works fine. IMO you should also test cerr stream, as previously it was not supported with the libc++abi and there were some segfaults. Please refer to this question.

    Create a test.cpp file:

    #include <iostream>
    int main()
    {
      using namespace std;
      cout << "[OK] Hello world to cout!" << endl;
      cerr << "[OK] Hello world to cerr!" << endl;
      clog << "[OK] Hello world to clog!" << endl;
      return 0;
    }
    

    And compile it and run it using this command line:

    clang++ -std=c++11 -stdlib=libc++ -lc++abi test.cpp && ./a.out

Reason there is no package

I found libc++ packages for Ubuntu but they are a bit behind recent version: https://packages.ubuntu.com/xenial/libc++-dev

Why they are not current, I can't answer, but my guess is that LLVM+Clang can work with mostly any Standard Library, whereas libc++ as you see must be linked to particular runtime ABI and might heavily depend on available C runtime library. I agree there should be a package which covers 90% of the cases. May be this is just the lack of resources. Searching the mailing archive did not bring up anything special.

Sign up to request clarification or add additional context in comments.

3 Comments

Great details. The fact that you had to go through all this is odd to me. There is already a great apt-get package for everything else, why not have libc++ in it. I can't believe the libc++ developers don't want their stuff used but that is the effect. I know of a number of companies that use clang on Linux but none use libc++ because it's not in the package. Sure you can call them lazy but I can understand why they just default to using libstdc++. Actually a couple of them didn't even realize they weren't using libc++ as they just assumed it would be there.
@goneskiing Feel free to accept the answer, if it answers your question :)
Note for clang-6.0: * Replace "4.0" and "release_40" with "6.0" and "release_50" will do. * -DLIBCXX_CXX_ABI and -DLIBCXX_LIBSUPCXX_INCLUDE_PATHS are not required
35
sudo apt-get update
sudo apt-get install libc++-dev

2 Comments

This gives a 3.7 version. As the poster said, rather old.
sudo apt install libc++-dev libc++abi-dev worked for me (needed ABI package also to compile using clang).
16

The accepted answer gave me some errors (it is Nov 2021 currently). Also, sudo apt install libc++-dev libc++abi-dev did not provide the latest libc++. Here is an alternate solution.

Use the LLVM apt package maintainer's script:

sudo bash -c "$(wget -O - https://apt.llvm.org/llvm.sh)"

This automatically sets up the apt repositories and gives you the latest LLVM toolchain.

clang-13 --version

Since you now have the latest LLVM packages available from http://apt.llvm.org you can install the latest libc++. First determine the latest version:

apt search libc++ | grep libc++

Then:

sudo apt install libc++-13-dev libc++abi-13-dev

Optionally, you can use update-alternatives to make your system use clang-13 instead of the default. There is a gist for that:

wget https://gist.githubusercontent.com/junkdog/70231d6953592cd6f27def59fe19e50d/raw/update-alternatives-clang.sh
chmod +x update-alternatives-clang.sh
sudo ./update-alternatives-clang.sh 13 1000

Now:

clang --version
Debian clang version 13.0.1-++20211110062941+9dc7d6d5e326-1~exp1~20211110183517.26
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

3 Comments

before running update-alternatives-clang.sh I also installed clang-tools-13, clang-format-13, and clang-tidy-13
Many thanks!! You saved my evening! I just installed LLVM-13 on my Debian 10 machine and could not for my life understand why #include <iostream> did not work! Still, I have a faint memory this is the second time I assumed a full LLVM install would come with all you need to clang++ build with the llvm tool chain. That the libc++ has to be added separately makes sense o n l y once you read up on the incitements for being gnu compatible. Being a developer and having to tinker with the system is like being an architect having to solve what batteries works with what power tools ;)
Yep, similar thing happened to me. It’s quite mystifying at first.
1

The easiest way to get a working libc++ is to install the entire 3.9.0 toolchain under /usr/local. This will allow /usr/local/bin/clang++ to find the headers correctly and also allow the linker to find /usr/local/lib/libc++.so.

Comments

0

check my shell-script automation version:

https://github.com/sailfish009/llvm_all

$ sudo apt-get install libffi-dev libedit-dev swig git

$ git clone https://github.com/sailfish009/llvm_all

$ git clone https://github.com/llvm/llvm-project

$ cd llvm_all

$ cp *.sh ../llvm-project/

$ cd ../llvm-project/

$ ./set.sh

$ ./install.sh

$ clang++ --version
clang version 11.0.0 (https://github.com/llvm/llvm-project 032251e34d17c1cbf21e7571514bb775ed5cdf30)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

Comments

-1

Just build it yourself, as explained here. I added -D CMAKE_CXX_COMPILER=clang++ -D CMAKE_C_COMPILER=clang to use clang as the compiler:

$ git clone https://github.com/llvm/llvm-project.git
$ cd llvm-project
$ mkdir build
$ cmake -G Ninja -S runtimes -B build -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind" -D CMAKE_CXX_COMPILER=clang++ -D CMAKE_C_COMPILER=clang # Configure for clang++
$ ninja -C build cxx cxxabi unwind # Build
$ ninja -C build check-cxx check-cxxabi check-unwind # Test
$ ninja -C build install-cxx install-cxxabi install-unwind # Install

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.