C ++ 11的编译器标志 [英] Compiler flags for C++11

查看:329
本文介绍了C ++ 11的编译器标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个CMakeFiles.txt(从来没有做过),我不知道什么编译器标志用于C ++ 11。我使用GCC 4.8.2和标志是std = c ++ 0x,但我不知道该怎么做其他编译器。我不认为他们都使用那个标志,我相信MinGW-TDM使用std = c + + 11,什么正确的方式来确保编译器使用c ++ 11?

这里是一个示例代码:

 #test for C ++ 11 flags 
include(TestCXXAcceptsFlag)

if(NOT DISABLE_GXX0XCHECK)
#尝试使用编译器标志-std = c ++ 11
check_cxx_accepts_flag( - std = c ++ 11CXX_FLAG_CXX11)
endif(NOT DISABLE_GXX0XCHECK)

if(CXX_FLAG_CXX11)
[add flag -std = c ++ 11 where needed]
else()
if(NOT DISABLE_GXX0XCHECK)
#尝试使用编译器标志-std = c ++ 0x为旧编译器
check_cxx_accepts_flag( - std = c ++ 0xCXX_FLAG_CXX0X)
endif(NOT DISABLE_GXX0XCHECK)
if(CXX_FLAG_CXX0X)
[add flag -std = c ++ 11 where needed]
...

Mabye stackoverflow.com/ q / 10984442 也帮助你。



编辑:我尝试使用Microsoft的Visual C ++,我的解决方案缺陷:编译器发出关于无法识别的标志的警告。这意味着CMake检测到该标志为正确并添加它。因此,每个可执行文件都会收到此警告。添加-Werror,或者Visual C ++的任何标志都应该有帮助。


I'm trying to write a CMakeFiles.txt (never done it before) and I'm not sure what compiler flag to use for C++11. I use GCC 4.8.2 and the flag is std=c++0x but I'm not sure what to do about other compilers. I don't suppose they all use that flag, I believe MinGW-TDM uses std=c++11, what would the correct way to ensure the compiler uses c++11 be?

解决方案

In my project I just try to add -std=c++11 and if that does not work I try -std=c++0x. When both fail, I just leave it alone. For me that works great but I have never tried a Windows compiler. Here an example code:

# test for C++11 flags
include(TestCXXAcceptsFlag)

if(NOT DISABLE_GXX0XCHECK)
  # try to use compiler flag -std=c++11
  check_cxx_accepts_flag("-std=c++11" CXX_FLAG_CXX11)
endif(NOT DISABLE_GXX0XCHECK)

if(CXX_FLAG_CXX11)
  [add flag -std=c++11 where needed]
else()
  if(NOT DISABLE_GXX0XCHECK)
    # try to use compiler flag -std=c++0x for older compilers
    check_cxx_accepts_flag("-std=c++0x" CXX_FLAG_CXX0X)
  endif(NOT DISABLE_GXX0XCHECK)
  if(CXX_FLAG_CXX0X)
    [add flag -std=c++11 where needed]
...

Mabye stackoverflow.com/q/10984442 does help you, too. Erik Sjölund suggest to have a look at CMake nightly build's FindCXXFeatures.cmake.

Edit: I tried this with Microsoft's Visual C++ and my solution has a flaw: The compiler emits a warning about the unrecognized flag. This means CMake detects the flag as correct and adds it. Thus one gets this warning for every executable. Adding -Werror, or whatever the flag for Visual C++ is, should help.

这篇关于C ++ 11的编译器标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆