Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake clang and c++0x

Tags:

c++

c++11

cmake

When using clang++, how can I make CMake use the -std=c++0x flag when compiling, but not when linking?

There are several other posts regarding using clang as compiler, but I have not found any hints on setting the c++ standard.

Here is what I tried:

CMakeLists.txt:

project(test)
add_executable(main main.cxx)

ClangOverride.txt:

SET (CMAKE_C_FLAGS_INIT                "-Wall -std=c99")
SET (CMAKE_C_FLAGS_DEBUG_INIT          "-g")
SET (CMAKE_C_FLAGS_MINSIZEREL_INIT     "-Os -DNDEBUG")
SET (CMAKE_C_FLAGS_RELEASE_INIT        "-O3 -DNDEBUG")
SET (CMAKE_C_FLAGS_RELWITHDEBINFO_INIT "-O2 -g")

SET (CMAKE_CXX_FLAGS_INIT                "-Wall -std=c++0x -stdlib=libc++")
SET (CMAKE_CXX_FLAGS_DEBUG_INIT          "-g")
SET (CMAKE_CXX_FLAGS_MINSIZEREL_INIT     "-Os -DNDEBUG")
SET (CMAKE_CXX_FLAGS_RELEASE_INIT        "-O3 -DNDEBUG")
SET (CMAKE_CXX_FLAGS_RELWITHDEBINFO_INIT "-O2 -g")
SET (CMAKE_EXE_LINKER_FLAGS_INIT         "")

main.cxx:

int main(){ return 0; }

Command used to invoke cmake

CC=clang CXX=clang++ cmake .. -DCMAKE_USER_MAKE_RULES_OVERRIDE=ClangOverride.txt -DCMAKE_BUILD_TYPE=Release

Building the project:

VERBOSE=1 make

This will invoke the following two commands:

/usr/bin/clang++    -Wall -std=c++0x -stdlib=libc++ -O3 -DNDEBUG   -o CMakeFiles/main.dir/main.cxx.o -c /tmp/asdf/main.cxx
/usr/bin/clang++    -Wall -std=c++0x -stdlib=libc++ -O3 -DNDEBUG    CMakeFiles/main.dir/main.cxx.o  -o main -rdynamic

The second command results in warning because if the un-used flag: -std=c++0x

clang: warning: argument unused during compilation: '-std=c++0x'
CMakeFiles/main.dir/main.cxx.o: file not recognized: File format not recognized
clang: error: linker command failed with exit code 1 (use -v to see invocation)

How can I avoid this?

like image 514
Allan Avatar asked Jan 20 '12 08:01

Allan


People also ask

What is CC clang?

Clang is a "LLVM native" C/C++/Objective-C compiler using LLVM as a backend and optimizer. It aims to be GCC compatible yet stricter, offers fast compile times with low memory usage, and has useful error and warning messages for easier compile troubleshooting.

How do you make CMake use clang instead of GCC?

Setup Clang Tooling Using CMake and Make If you want to use clang instead of GCC, you can add -DCMAKE_C_COMPILER=/path/to/clang -DCMAKE_CXX_COMPILER=/path/to/clang++ . You can also use ccmake , which provides a curses interface to configure CMake variables.

What is Cmake_cxx_compiler_id?

CMAKE_CXX_COMPILER_ID : The unique compiler identification string. CMAKE_COMPILER_IS_GNUCXX : True if the C++ compiler is part of GCC. CMAKE_CXX_COMPILER_VERSION : A string of the C++ compiler version. CMAKE_CXX_COMPILER : Path to the selected C++ compiler. CMAKE_C_COMPILER : Path to the selected C compiler.

Should I use GCC or clang?

Clang is much faster and uses far less memory than GCC. Clang aims to provide extremely clear and concise diagnostics (error and warning messages), and includes support for expressive diagnostics. GCC's warnings are sometimes acceptable, but are often confusing and it does not support expressive diagnostics.


1 Answers

Use ADD_DEFINITIONS("-std=c++0x") instead of setting CXX flags.

like image 124
belkiss Avatar answered Oct 02 '22 19:10

belkiss