Linking libc++ to CMake project on Linux

Ostrea picture Ostrea · Nov 27, 2014 · Viewed 19.8k times · Source

I want to use libc++ together with clang on Arch Linux in CMake project. I installed libc++ and added following lines to CMakeLists.txt as said on LLVM site in Linux section of "Using libc++ in your programs":

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -stdlib=libc++")
set(CMAKE_EXE_LINKER_FLAGS "-lc++abi")

I have tried just "++abi" in linker's flags, but it didn't help. I need some help in figuring out what i should write in my CMakeLists.txt.

Answer

emw picture emw · Feb 4, 2015

Don't forget to set the compiler to clang++:

set(CMAKE_CXX_COMPILER "clang++")

Also, purge the cmake generated files (delete the folder CMakeFiles and CMakeCache.txt).

Depending on your system, it might also help to set

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libc++ -lc++abi")