wiki:LibcxxOnOlderSystems

Version 71 (modified by mojca (Mojca Miklavec), 5 years ago) (diff)

Add a big warning sign to make it clear the page is obsolete. I know this has already been mentioned on the page, but it's more hidden.

With the release of MacPorts 2.6.0 the setup suggested on this page became obsolete.

If you used it before, below are instructions for reverting back.

Using libc++ on older system

If you have been referred to this page, you are likely 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.

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.

[Note: since the original writing of this article, MacPorts has developed the ability to build software against newer versions of libgcc that support c++11 (and newer) as well. This is currently undertaken by default in MacPorts. You may still wish to use libc++ as your default runtime to achieve maximal compatibility with currently-developed software, however. If so, continue with these instructions.]

Snow Leopard, Lion and Mountain Lion

As of version 2.6.0, MacPorts will use libc++ by default when building ports. You no longer need to make any changes on these OS versions.

If you followed the instructions that were here for earlier versions of MacPorts, you should remove or comment out the default_compilers setting in your macports.conf, or the correct compiler may not be chosen. Also remove or comment out the buildfromsource setting if you wish to be able to install from prebuilt archives. The cxx_stdlib setting can safely be removed also, though leaving it set to libc++ will do no harm since that is now the default.

If you changed the delete_la_files setting from its default value (as suggested in much older versions of these instructions), you will unfortunately need to uninstall all installed ports before changing it back, as ports built with this setting on are not compatible with those built with it off. All ports will be built from source if this setting has a value other than the default.

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 libcxx port activates), 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, and Leopard support was only recently added on November 1, 2016.

Bootstrapping 1: Fix the SDK

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, it 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 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++

The instructions below will install clang-3.7.

  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++
    default_compilers  macports-clang-3.4 gcc-4.2 apple-gcc-4.2 gcc-4.0
    revupgrade_autorun no
    
  3. Uninstall all the remaining inactive ports:
    sudo port -v -f uninstall inactive
    
  4. Install clang-3.7 against libc++.
    sudo port -v install clang-3.7
    
  5. Disable the variants corresponding to the bootstrap versions of llvm in /opt/local/etc/macports/variants.conf. This ensures that future installs of cctools and ld64 will use a newer version of llvm:
    -llvm34
    +llvm50
    
  6. Edit /opt/local/etc/macports/macports.conf again to add macports-clang-3.7 as preferred compiler:
    default_compilers  macports-clang-3.7 macports-clang-3.4 gcc-4.2 apple-gcc-4.2 gcc-4.0
    
  7. Rebuild cctools and ld64 to use the newer version of llvm (currently llvm-5.0):
    sudo port -v -n upgrade --force --enforce-variants cctools -llvm34 +llvm50
    sudo port -v -n upgrade --force --enforce-variants ld64 -llvm34 +llvm50
    
  8. Install clang-5.0
    sudo port -v install clang-5.0
    
  9. Edit /opt/local/etc/macports/macports.conf again to add macports-clang-5.0 as preferred compiler, and re-enable revupgrade_autorun:
    default_compilers macports-clang-5.0 macports-clang-3.7 macports-clang-3.4 gcc-4.2 apple-gcc-4.2 gcc-4.0
    ## delete this line you added previously ==> revupgrade_autorun  	no
    
  10. Now that you have clang-5.0 (or greater) installed, you can rebuild libcxx with the +emulatedtls variant for full functionality:
    sudo port -v upgrade --enforce-variants libcxx +emulatedtls
    
  11. Uninstall the bootstrap toolchain:
    sudo port -v deactivate llvm-3.4 clang-3.4
    sudo port -v uninstall inactive
    
  12. Snow Leopard is able to install and use versions of clang and llvm up at least 8.0 at present. Leopard is presently limited to clang-7.0.

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 [1ad7738a54d1bb11c98455154e6d625680f49b2d/macports-ports]), 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.7

# Re-build libcxx, libmacho, and libunwind with a ppc slice
sudo port -v -n upgrade --force libmacho +universal universal_archs="x86_64 i386 ppc" configure.compiler=macports-clang-3.7
sudo port -v -n upgrade --force libunwind +universal universal_archs="x86_64 i386 ppc" configure.compiler=macports-clang-3.7
sudo port -v -n upgrade --force libcxx +universal universal_archs="x86_64 i386 ppc" configure.compiler=macports-clang-3.7

With this, a simple PPC C++ "Hello World" app was able to run successfully on an Intel Mac.

Exceptions are known to not work, and there are likely other problems as well:

Not much more testing has been done than that, and getting the toolchain itself to run on ppc is not going to work without at least those above tickets getting addressed.

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.