UsingLibcxx.rst 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. ============
  2. Using libc++
  3. ============
  4. .. contents::
  5. :local:
  6. Getting Started
  7. ===============
  8. If you already have libc++ installed you can use it with clang.
  9. .. code-block:: bash
  10. $ clang++ -stdlib=libc++ test.cpp
  11. $ clang++ -std=c++11 -stdlib=libc++ test.cpp
  12. On OS X and FreeBSD libc++ is the default standard library
  13. and the ``-stdlib=libc++`` is not required.
  14. .. _alternate libcxx:
  15. If you want to select an alternate installation of libc++ you
  16. can use the following options.
  17. .. code-block:: bash
  18. $ clang++ -std=c++11 -stdlib=libc++ -nostdinc++ \
  19. -I<libcxx-install-prefix>/include/c++/v1 \
  20. -L<libcxx-install-prefix>/lib \
  21. -Wl,-rpath,<libcxx-install-prefix>/lib \
  22. test.cpp
  23. The option ``-Wl,-rpath,<libcxx-install-prefix>/lib`` adds a runtime library
  24. search path. Meaning that the systems dynamic linker will look for libc++ in
  25. ``<libcxx-install-prefix>/lib`` whenever the program is run. Alternatively the
  26. environment variable ``LD_LIBRARY_PATH`` (``DYLD_LIBRARY_PATH`` on OS X) can
  27. be used to change the dynamic linkers search paths after a program is compiled.
  28. An example of using ``LD_LIBRARY_PATH``:
  29. .. code-block:: bash
  30. $ clang++ -stdlib=libc++ -nostdinc++ \
  31. -I<libcxx-install-prefix>/include/c++/v1
  32. -L<libcxx-install-prefix>/lib \
  33. test.cpp -o
  34. $ ./a.out # Searches for libc++ in the systems library paths.
  35. $ export LD_LIBRARY_PATH=<libcxx-install-prefix>/lib
  36. $ ./a.out # Searches for libc++ along LD_LIBRARY_PATH
  37. Using libc++ on Linux
  38. =====================
  39. On Linux libc++ can typically be used with only '-stdlib=libc++'. However
  40. some libc++ installations require the user manually link libc++abi themselves.
  41. If you are running into linker errors when using libc++ try adding '-lc++abi'
  42. to the link line. For example:
  43. .. code-block:: bash
  44. $ clang++ -stdlib=libc++ test.cpp -lc++ -lc++abi -lm -lc -lgcc_s -lgcc
  45. Alternately, you could just add libc++abi to your libraries list, which in
  46. most situations will give the same result:
  47. .. code-block:: bash
  48. $ clang++ -stdlib=libc++ test.cpp -lc++abi
  49. Using libc++ with GCC
  50. ---------------------
  51. GCC does not provide a way to switch from libstdc++ to libc++. You must manually
  52. configure the compile and link commands.
  53. In particular you must tell GCC to remove the libstdc++ include directories
  54. using ``-nostdinc++`` and to not link libstdc++.so using ``-nodefaultlibs``.
  55. Note that ``-nodefaultlibs`` removes all of the standard system libraries and
  56. not just libstdc++ so they must be manually linked. For example:
  57. .. code-block:: bash
  58. $ g++ -nostdinc++ -I<libcxx-install-prefix>/include/c++/v1 \
  59. test.cpp -nodefaultlibs -lc++ -lc++abi -lm -lc -lgcc_s -lgcc