简体   繁体   English

在cmake中添加c ++ 0x支持

[英]add c++0x support in cmake

I have a rather large application configured in cmake. 我在cmake中配置了一个相当大的应用程序。 I have recently added a couple of classes that utilize C++0x functionality, and it breaks the build, since cmake is not configured to compile with C++0x support. 我最近添加了几个利用C ++ 0x功能的类,它打破了构建,因为cmake没有配置为使用C ++ 0x支持进行编译。 How do I add that as an option to cmake? 如何将其添加为cmake的选项?

您需要将标志添加到CMAKE_CXX_FLAGS

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")

I use this snippet for gcc, but it is a more complicated way 我将此代码段用于gcc,但这是一种更复杂的方式

if(CMAKE_COMPILER_IS_GNUCXX)
   SET(ENABLE_CXX11 "-std=c++11")

   EXECUTE_PROCESS(COMMAND "${CMAKE_CXX_COMPILER} -dumpversion" OUTPUT_VARIABLE GCC_VERSION)
   if (GCC_VERSION VERSION_LESS 4.7)
      SET(ENABLE_CXX11 "-std=c++0x")
   endif()

   SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${ENABLE_CXX11}")
endif()

I have the following module: 我有以下模块:

$ cat FindCXX11.cmake 
# - Finds if the compiler has C++11 support
# This module can be used to detect compiler flags for using C++11, and checks
# a small subset of the language.
#
# The following variables are set:
#   CXX11_FLAGS - flags to add to the CXX compiler for C++11 support
#   CXX11_FOUND - true if the compiler supports C++11
#
# TODO: When compilers starts implementing the whole C++11, check the full set

include(CheckCXXSourceCompiles)
include(FindPackageHandleStandardArgs)

set(CXX11_FLAG_CANDIDATES
    #Gnu and Intel Linux
    "-std=c++0x"
    #Microsoft Visual Studio, and everything that automatically accepts C++11
    " "
    #Intel windows
    "/Qstd=c++0x"
    )

set(CXX11_TEST_SOURCE
"
class Matrix
{
public:
    Matrix(int a, int b, int c, int d)
        : data {a, b, c, d}
    {}

private:
    int data[4];
};

int main()
{
    int n[] {4,7,6,1,2};
    for (auto i : n)
        Matrix mat (3,5,1,2);
    return 0;
}
")

foreach(FLAG ${CXX11_FLAG_CANDIDATES})
    set(SAFE_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
    set(CMAKE_REQUIRED_FLAGS "${FLAG}")
    unset(CXX11_FLAG_DETECTED CACHE)
    message(STATUS "Try C++11 flag = [${FLAG}]")
    check_cxx_source_compiles("${CXX11_TEST_SOURCE}" CXX11_FLAG_DETECTED)
    set(CMAKE_REQUIRED_FLAGS "${SAFE_CMAKE_REQUIRED_FLAGS}")
    if(CXX11_FLAG_DETECTED)
        set(CXX11_FLAGS_INTERNAL "${FLAG}")
        break()
    endif(CXX11_FLAG_DETECTED)
endforeach(FLAG ${CXX11_FLAG_CANDIDATES})

set(CXX11_FLAGS "${CXX11_FLAGS_INTERNAL}")

find_package_handle_standard_args(CXX11 DEFAULT_MSG CXX11_FLAGS)
mark_as_advanced(CXX11_FLAGS)

I use this to check if the subset of C++11 I'm using (actually this is redundant now, as I'm using a lot more), is supported by the compiler. 我使用它来检查我正在使用的C ++ 11的子集(实际上这是多余的,因为我正在使用更多),是否受到编译器的支持。 It is easy to build this further and add more features, and add other compiler switches. 很容易进一步构建它并添加更多功能,并添加其他编译器开关。 Actually the only compilers I have found so far which passes this test, is gcc >= 4.6 and clang >= 3.1 if I remember correctly. 实际上到目前为止我发现的唯一通过此测试的编译器是gcc> = 4.6而clang> = 3.1如果我没记错的话。

As shown in this answer , it's better to use target_compile_features to require support for the specific features that you need. 本答案所示,最好使用target_compile_features来要求支持您需要的特定功能。

This explains/documents better what functionality your code requires, is cross-platform compatible, and will give the user a more helpful error message if the required feature is not available in their compiler. 这解释/更好地记录了代码所需的功能,跨平台兼容,并且如果编译器中没有所需的功能,将为用户提供更有用的错误消息。

From the manual page for this option: 从此选项的手册页

 target_compile_features 

Add expected compiler features to a target. 将预期的编译器功能添加到目标。

 target_compile_features(<target> <PRIVATE|PUBLIC|INTERFACE> <feature> [...]) 

Specify compiler features required when compiling a given target. 指定编译给定目标时所需的编译器功能。 If the feature is not listed in the CMAKE_C_COMPILE_FEATURES variable or CMAKE_CXX_COMPILE_FEATURES variable, then an error will be reported by CMake. 如果该功能未在CMAKE_C_COMPILE_FEATURES变量或CMAKE_CXX_COMPILE_FEATURES变量中列出,则CMake将报告错误。 If the use of the feature requires an additional compiler flag, such as -std=gnu++11 , the flag will be added automatically. 如果使用该功能需要额外的编译器标志,例如-std=gnu++11 ,则会自动添加该标志。

The (hopefully complete) list of features that can be tested with this macro is here . 可以使用此宏测试的(希望是完整的)功能列表在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM