Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 946
Ostrea Avatar asked Nov 27 '14 20:11

Ostrea


People also ask

What is libc ++ abi?

libc++abi is a new implementation of low level support for a standard C++ library. All of the code in libc++abi is dual licensed under the MIT license and the UIUC License (a BSD-like license).

How does CMakeLists TXT work?

CMakeLists. txt file contains a set of directives and instructions describing the project's source files and targets (executable, library, or both). When you create a new project, CLion generates CMakeLists. txt file automatically and places it in the project root directory.

Is CMake using GCC?

cmake and make stuffs are basically just tools in using g++ / gcc.

What are CMake build files?

CMake is a meta build system that uses scripts called CMakeLists to generate build files for a specific environment (for example, makefiles on Unix machines). When you create a new CMake project in CLion, a CMakeLists. txt file is automatically generated under the project root.


2 Answers

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")
like image 115
emw Avatar answered Oct 26 '22 01:10

emw


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

like image 37
Daniel Russell Avatar answered Oct 26 '22 00:10

Daniel Russell