Ostrea
Ostrea

Reputation: 289

Linking libc++ to CMake project on Linux

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.

Upvotes: 28

Views: 45039

Answers (2)

Daniel Russell
Daniel Russell

Reputation: 769

The "proper" way to do this in CMake at the moment, until a specific base feature is added to switch standard libraries that is, is to use a toolchain file.

In that toolchain file you specify the compiler etc similarly to the other answers here.

BUT what's great about toolchains is they can be swapped out quickly either on the commandline (using -DCMAKE_TOOLCHAIN_FILE=path/to/file) OR in VSCode with CMakeTools extension installed, and probably other editors too.

But having to hand code your own toolchain files is yet another obscure chore! No fun!

Luckily, I stumbled upon this github that maintains a suite of them so you don't have to write them from scratch! Should be a lot less likely to get them wrong.

https://github.com/ruslo/polly

Upvotes: 4

emw
emw

Reputation: 364

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")

Upvotes: 34

Related Questions