wiki:LibcxxOnOlderSystems

Version 30 (modified by jeremyhu (Jeremy Huddleston Sequoia), 8 years ago) (diff)

--

Using libc++ on older system

More and more C++ software is being written using the C++11 standard. If you have been referred to this page, you are trying to build software that uses C++11 on OS X 10.8 or earlier. The default C++ runtime on OS X 10.8 and earlier is a version of libstdc++ based on GCC 4.2.1 which does not support C++11. The default C++ runtime on OS X 10.9 and later is libc++ which does support C++11.

The recommended solution, if your computer supports it, is to upgrade to OS X 10.9 or later. If you do this, you can ignore the rest of this article, and should instead see the migration instructions.

Alternately, you can switch MacPorts on OS X 10.8 or earlier to use libc++ instead of libstdc++. This must be done for MacPorts as a whole; it can't be done for individual ports.

At this time the MacPorts project does not build binary packages for libc++ on OS X 10.8 or earlier, so if you make this change, MacPorts will be building all ports from source.

If you use MacPorts C++-using ports as dependencies for C++-using software that you compile outside of MacPorts, you must compile that software using libc++ as well.

Lion and Mountain Lion

Lion and Mountain Lion systems have libc++ installed with the OS but still use libstdc++ as the default C++ runtime. You can use it by passing -stdlib=libc++ as a command line argument to clang++, and you can instruct MacPorts to use it instead of libstdc++ as the default C++ runtime.

  1. Start with a new install of MacPorts or uninstall all ports that use C++
  2. Edit /opt/local/etc/macports/macports.conf to contain:
cxx_stdlib         libc++
buildfromsource    always
delete_la_files    yes

delete_la_files is not necessary for libc++, but it is recommended since you need to set buildfromsource and rebuild all ports from scratch anyways.

Leopard and Snow Leopard (Intel)

Leopard and Snow Leopard do not have libc++ installed by the OS, but MacPorts can build and install system roots that contain these runtimes for your use. After installing these roots (which is done automatically when the libcxxabi and libcxx ports activate), you can choose to continue using libstdc++ as the default C++ runtime in MacPorts or configure MacPorts to use the new libc++ runtime.

Warning: libc++ is fairly well tested on Snow Leopard, but Leopard has known issues (eg: exceptions don't work due to a bug in libunwind), so it is not recommended for production use on Leopard.

Bootstrapping 1: Fix the SDK (Leopard Only)

Leopard's SDK has some bugs that are more visible when using newer toolchains, so please follow the steps in Leopard SDK Fixes first.

Bootstrapping 2: Install libc++ and libc++abi

  1. Start with a new install of MacPorts or uninstall all ports that use C++
  2. Install the libcxx port. When it activates, the libcxxabi and libcxx ports will install libc++.dylib and libc++abi.dylib (which will remain after you uninstall MacPorts). This will pull in a number of dependencies.
    sudo port -v -s install libcxx
    

At this point, you will have libc++ installed and can use clang++-mp-3.4 -stdlib=libc++ for your personal projects, but like Lion and Mountain Lion, MacPorts will continue to use libstdc++ by default unless you continue with the next step.

Bootstrapping 3: Build a current version of clang against libc++

  1. Deactivate all your active ports (or at least the C++ ports) in order to prepare to rebuild them, and just activate the new toolchain:
    sudo port -v -f deactivate active
    sudo port -v install clang-3.4 ld64 cctools libcxx
    
  2. Edit /opt/local/etc/macports/macports.conf to contain:
    cxx_stdlib         libc++
    buildfromsource    always
    delete_la_files    yes
    default_compilers  macports-clang-3.4 macports-clang-3.3 gcc-4.2 apple-gcc-4.2 gcc-4.0
    
  3. Uninstall all the remaining inactive ports:
    sudo port -v -f uninstall inactive
    
  4. Build a newer clang using libc++:
    sudo port -v install clang-3.7
    
  5. Update cctools and ld64 to use the newer version of llvm:
    sudo port -v -n upgrade --force --enforce-variants cctools -llvm33 -llvm34 +llvm37 configure.compiler=macports-clang-3.7
    sudo port -v -n upgrade --force --enforce-variants ld64 -llvm33 -llvm34 +llvm37 configure.compiler=macports-clang-3.7
    
  6. Uninstall the bootstrap toolchain:
    sudo port -v uninstall llvm-3.4 clang-3.4
    
  7. Either:
    1. Edit /opt/local/etc/macports/macports.conf again to add macports-clang-3.7 before macports-clang-3.4.
    2. Rebuild clang-3.4 using clang-3.7:
      sudo port -v install clang-3.4 configure.compiler=macports-clang-3.7
      

Leopard (ppc)

Darwin/PowerPC support is still quite experimental in llvm, so expect things to not be as stable as more established toolchains. Additionally, it is non-trivial to bootstrap libc++ on Leopard because the libcxx port does not build for ppc with versions older than clang-3.6, and clang-3.5 and newer require libc++. After setting up my Leopard/Intel Mac as described above (fully upgraded through r131812), I've been able to build a universal libc++ runtime for ppc:

# clang-3.6 or newer is required for compiling ppc code with clang
sudo port -v -s install clang-3.6

# Uninstall the intel versions of these ports
sudo port -v -f uninstall libcxx libcxxabi libmacho libunwind

# Re-build libcxx, libcxxabi, libmacho, and libunwind with a ppc slice
sudo port -v -s install libcxx universal_archs="x86_64 i386 ppc" configure.compiler=macports-clang-3.6

With this, a simple PPC C++ "Hello World" app was able to run successfully on an Intel Mac, and also note that exceptions don't work due to a bug in libunwind). Not much more testing has been done than that.

Tiger

I do not have a Tiger machine or VM, so I am not sure how feasible it is to get things working on Tiger. If you are adventurous, I suggest you follow the Leopard instructions above and report issues. Note that it will take a while to build libcxx because we'll need to build apple-gcc42 and use it to build clang-3.4 and use it to build libcxx.