# "libc++" C++ Standard Library

libc++ is a new implementation of the C++ standard library, targeting C++11.

All of the code in libc++ is dual licensed under the MIT license and the UIUC License (a BSD-like license).

## Features and Goals

- Correctness as defined by the C++11 standard.
- Fast execution.
- Minimal memory use.
- Fast compile times.
- ABI compatibility with gcc's libstdc++ for some low-level features such as exception objects, rtti and memory allocation.
- Extensive unit tests.

## Why a new C++ Standard Library for C++11?

After its initial introduction, many people have asked "why start a new library instead of contributing to an existing library?" (like Apache's libstdcxx, GNU's libstdc++, STLport, etc). There are many contributing reasons, but some of the major ones are:

- From years of experience (including having implemented the standard library before), we've learned many things about implementing the standard containers which require ABI breakage and fundamental changes to how they are implemented. For example, it is generally accepted that building std::string using the "short string optimization" instead of using Copy On Write (COW) is a superior approach for multicore machines (particularly in C++11, which has rvalue references). Breaking ABI compatibility with old versions of the library was determined to be critical to achieving the performance goals of libc++.

- Mainline libstdc++ has switched to GPL3, a license which the developers of libc++ cannot use. libstdc++ 4.2 (the last GPL2 version) could be independently extended to support C++11, but this would be a fork of the codebase (which is often seen as worse for a project than starting a new independent one). Another problem with libstdc++ is that it is tightly integrated with G++ development, tending to be tied fairly closely to the matching version of G++.

- STLport and the Apache libstdcxx library are two other popular candidates, but both lack C++11 support. Our experience (and the experience of libstdc++ developers) is that adding support for C++11 (in particular rvalue references and move-only types) requires changes to almost every class and function, essentially amounting to a rewrite. Faced with a rewrite, we decided to start from scratch and evaluate every design decision from first principles based on experience.

  Further, both projects are apparently abandoned: STLport 5.2.1 was released in Oct'08, and STDCXX 4.2.1 in May'08.

## Platform Support

libc++ is known to work on the following platforms, using g++-4.2 and clang (lack of C++11 language support disables some functionality).

- Mac OS X i386
- Mac OS X x86_64

## Current Status

libc++ is a 100% complete C++11 implementation on Apple's OS X.

LLVM and Clang can self host in C++ and C++11 mode with libc++ on Linux.

C++1Y (C++14) implementation is in progress. The current status is here

Ports to other platforms are underway. Here are recent test results for Windows and Linux.

## Get it and get involved!

First please review our Developer's Policy.

To check out the code, use:

- `svn co http://llvm.org/svn/llvm-project/libcxx/trunk libcxx`

On Mac OS 10.7 (Lion) and later, the easiest way to get this library is to install Xcode 4.2 or later. However if you want to install tip-of-trunk from here (getting the bleeding edge), read on. However, be warned that Mac OS 10.7 will not boot without a valid copy of `libc++.1.dylib` in `/usr/lib`.

Next:

- `cd libcxx/lib`
- `export TRIPLE=-apple-`
- `./buildit`
- `ln -sf libc++.1.dylib libc++.dylib`

That should result in a libc++.1.dylib and libc++.dylib. The safest thing to do is to use it from where your libcxx is installed instead of replacing these in your Mac OS.

To use your system-installed libc++ with clang you can:

- `clang++ -stdlib=libc++ test.cpp`
- `clang++ -std=c++11 -stdlib=libc++ test.cpp`

To use your tip-of-trunk libc++ on Mac OS with clang you can:

- `export DYLD_LIBRARY_PATH=<path-to-libcxx>/lib`
- `clang++ -std=c++11 -stdlib=libc++ -nostdinc++ -I<path-to-libcxx>/include -L<path-to-libcxx>/lib test.cpp`

To run the libc++ test suite (recommended):

- `cd libcxx/test`
- `./testit`

- You can alter the command line options `testit` uses with `export OPTIONS="whatever you need"`

### Notes

Building libc++ with `-fno-rtti` is not supported. However linking against it with `-fno-rtti` is supported.

Send discussions to the (clang mailing list).

## Build on Linux using CMake and libsupc++.

You will need libstdc++ in order to provide libsupc++.

Figure out where the libsupc++ headers are on your system. On Ubuntu this is `/usr/include/c++/<version>` and `/usr/include/c++/<version>/<target-triple>`

You can also figure this out by running

    $ echo | g++ -Wp,-v -x c++ - -fsyntax-only
    ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
    ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../x86_64-linux-gnu/include"
    #include "..." search starts here:
    #include <...> search starts here:
     /usr/include/c++/4.7
     /usr/include/c++/4.7/x86_64-linux-gnu
     /usr/include/c++/4.7/backward
     /usr/lib/gcc/x86_64-linux-gnu/4.7/include
     /usr/local/include
     /usr/lib/gcc/x86_64-linux-gnu/4.7/include-fixed
     /usr/include/x86_64-linux-gnu
     /usr/include
    End of search list.

Note the first two entries happen to be what we are looking for. This may not be correct on other platforms.

We can now run CMake:

- `CC=clang CXX=clang++ cmake -G "Unix Makefiles" -DLIBCXX_CXX_ABI=libstdc++ -DLIBCXX_LIBSUPCXX_INCLUDE_PATHS="/usr/include/c++/4.7/;/usr/include/c++/4.7/x86_64-linux-gnu/" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr <libc++-source-dir>`
- You can also substitute `-DLIBCXX_CXX_ABI=libsupc++` above, which will cause the library to be linked to libsupc++ instead of libstdc++, but this is only recommended if you know that you will never need to link against libstdc++ in the same executable as libc++. GCC ships libsupc++ separately but only as a static library. If a program also needs to link against libstdc++, it will provide its own copy of libsupc++ and this can lead to subtle problems.
- `make`
- `sudo make install`

You can now run clang with -stdlib=libc++.

## Build on Linux using CMake and libc++abi.

You will need to keep the source tree of libc++abi available on your build machine and your copy of the libc++abi shared library must be placed where your linker will find it.

We can now run CMake:

- `CC=clang CXX=clang++ cmake -G "Unix Makefiles" -DLIBCXX_CXX_ABI=libcxxabi -DLIBCXX_LIBCXXABI_INCLUDE_PATHS="<libc++abi-source-dir>/include" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr <libc++-source-dir>`
- `make`
- `sudo make install`

Unfortunately you can't simply run clang with "-stdlib=libc++" at this point, as clang is set up to link for libc++ linked to libsupc++. To get around this you'll have to set up your linker yourself (or patch clang). For example,

- `clang++ -stdlib=libc++ helloworld.cpp -nodefaultlibs -lc++ -lc++abi -lm -lc -lgcc_s -lgcc`

Alternately, you could just add libc++abi to your libraries list, which in most situations will give the same result:

- `clang++ -stdlib=libc++ helloworld.cpp -lc++abi`

## Build on Linux using CMake and libcxxrt.

You will need to keep the source tree of libcxxrt available on your build machine and your copy of the libcxxrt shared library must be placed where your linker will find it.

We can now run CMake:

- `CC=clang CXX=clang++ cmake -G "Unix Makefiles" -DLIBCXX_CXX_ABI=libcxxrt -DLIBCXX_LIBCXXRT_INCLUDE_PATHS="<libcxxrt-source-dir>/src" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr <libc++-source-dir>`
- `make`
- `sudo make install`

Unfortunately you can't simply run clang with "-stdlib=libc++" at this point, as clang is set up to link for libc++ linked to libsupc++. To get around this you'll have to set up your linker yourself (or patch clang). For example,

- `clang++ -stdlib=libc++ helloworld.cpp -nodefaultlibs -lc++ -lcxxrt -lm -lc -lgcc_s -lgcc`

Alternately, you could just add libcxxrt to your libraries list, which in most situations will give the same result:

- `clang++ -stdlib=libc++ helloworld.cpp -lcxxrt`

## Design Documents

- `<atomic>`
- `<type_traits>`
- Excellent notes by Marshall Clow
- Status of debug mode