CMake disable -std=c++11 flag for C files

2019-02-21 20:40发布

I'm trying to build bkchaind. One build option is to use cmake, so I installed it with Homebrew (OSX 10.9.1). When I do cmake, though, I get:

[  2%] Building C object json-rpc-cpp/src/jsonrpc/CMakeFiles/jsonrpcStatic.dir/connectors/mongoose.c.o
error: invalid argument '-std=c++11' not allowed with 'C/ObjC'

I am none too sure why cmake would try to pass a C++-specific compiler option to a C/ObjC file. If I comment out this line in the main CMakeLists.txt file:

ADD_DEFINITIONS(-std=c++11)

then it no longer passes the flag to any file. However, the C++ files do need it. How do I get cmake to include the flag for C++ files, but not for C files?

2条回答
唯我独甜
2楼-- · 2019-02-21 21:03

This type of thing can be done explicitly without any dependence on the specific format of a compiler's flags:

target_compile_features(someTargetName PUBLIC cxx_std_14)

target_compile_features can also do this more implicitly I think, by requesting particular language features that are only found in the desired version of the language.

Another way is to set a target property:

set_property(TARGET server PROPERTY CXX_STANDARD 14)

Or one may set CMAKE_CXX_STANDARD, whose value is used as the default value for CXX_STANDARD on targets, and then allow targets to inherit that value implicitly.

(Here I'm using the 2014 standard, but of course one may specify 11 or some other value.)

查看更多
男人必须洒脱
3楼-- · 2019-02-21 21:08

Use CMAKE_CXX_FLAGS to set c++ specific flags:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
查看更多
登录 后发表回答