简体   繁体   English

如何在Codelite中启用C ++ 11功能

[英]How to Enable C++11 Features in Codelite

The following code compiles and runs in Xcode 5 and in Visual Studio 2013. I am interested in trying out Codelite, but Codelite will not compile the following program (a problem since I am working with scoped enums in my project). 下面的代码在Xcode 5和Visual Studio 2013中编译和运行。我有兴趣尝试Codelite,但Codelite不会编译以下程序(因为我在我的项目中使用作用域枚举,所以是一个问题)。 As far as I understand it, Codelite is using the same compiler as Xcode. 据我所知,Codelite使用与Xcode相同的编译器。

Is the code valid per C++11? 代码对每个C ++ 11有效吗? Why is Codelite unable to compile it? 为什么Codelite无法编译它?

#include <iostream>

namespace abc
{
    namespace xyz
    {
        enum class SampleEnum
        {
            SomeValue = 0,
            SomeOtherValue = 1
        };
    }
}

int main(int argc, char **argv)
{
    abc::xyz::SampleEnum e = abc::xyz::SampleEnum::SomeValue;
    return 0;
}

Here is the build output from Codelite. 这是Codelite的构建输出。 In case it's garbled, it's pointing to the word "SampleEnum" in the instantiation of the variable and saying "expected a class or namespace". 如果它是乱码,它指向变量实例化中的单词“SampleEnum”并说“期望一个类或命名空间”。

/bin/sh -c 'make -j8 -e -f  Makefile'
----------Building project:[ ClangTest - Debug ]----------
codelite-cc /usr/bin/clang++   -c  "/Users/xxx/Desktop/Test/ClangTest/main.cpp" -g -O0 -Wall  -o ./Debug/main.cpp.o -I. -I.
/Users/xxx/Desktop/Test/ClangTest/main.cpp:7:8: warning: scoped enumerations are a C++11 extension [-Wc++11-extensions]
                enum class SampleEnum
                     ^
/Users/xxx/Desktop/Test/ClangTest/main.cpp:17:40: error: expected a class or namespace
    abc::xyz::SampleEnum e = abc::xyz::SampleEnum::SomeValue;
                             ~~~~~~~~~~^
1 warning and 1 error generated.
make[1]: *** [Debug/main.cpp.o] Error 1
make: *** [All] Error 2
2 errors, 1 warnings

It is necessary to pass -std=c++11 to the compiler to enable C++11 features. 有必要将-std = c ++ 11传递给编译器以启用C ++ 11功能。 Here are the steps to do so in Codelite: 以下是在Codelite中执行此操作的步骤:

  • Right click on the project in the workspace view. 在工作区视图中右键单击项目。
  • Select Settings near the bottom of this pop-up menu. 选择此弹出菜单底部附近的“设置”。 Common Settings->Compiler->C++ Compiler Options 通用设置 - >编译器 - > C ++编译器选项
  • Click into the semicolon delimited list of compiler switches to reveal elipses and click on the elipses. 单击分号分隔的编译器开关列表以显示elipses并单击elipses。
  • Click the checkbox for -std=c++11 单击-std = c ++ 11的复选框

项目设置窗口的屏幕截图

If you are using C++11 extensions, compilers want it to be flagged. 如果您使用C ++ 11扩展,编译器希望它被标记。 Without it they may throw warnings and errors. 没有它,他们可能会发出警告和错误。 That's because some of C++11 changes are not backward-compatible, eg the use of auto . 这是因为一些C ++ 11的更改不是向后兼容的,例如使用auto

For example, in gcc you should have 例如,在gcc中你应该有

gcc -std=c++11

Check if your compiler shouldn't have such parameter as well! 检查你的编译器是否也不应该有这样的参数!

I suppose this is because that your default std version is not c++11. 我想这是因为你的默认std版本不是c ++ 11。 To change to c++11, if you are using your terminal, you should type in the following command: 要更改为c ++ 11,如果您使用终端,则应键入以下命令:

g++ yourfile.cpp -std=c++11

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

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