7

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). As far as I understand it, Codelite is using the same compiler as Xcode.

Is the code valid per C++11? Why is Codelite unable to compile it?

#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. 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".

/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
Matthew James Briggs
  • 2,085
  • 2
  • 28
  • 58
  • Seems Codelite does not like C++11: 'warning: scoped enumerations are a C++11 extension ' –  Feb 28 '15 at 07:37
  • 1
    It looks like your compiler is not running in C++11 mode, try adding the switch `-std=c++11` – M.M Feb 28 '15 at 07:38
  • 1
    I think you should put your Edit as an answer to your own question. Indeed it may help someone in the future, but may skip reading it thinking it is a continuation of your question instead. – CygnusX1 Feb 28 '15 at 14:27
  • @CygnusX1 I have followed your suggestion and moved my Codelite instructions to an answer. – Matthew James Briggs Feb 28 '15 at 17:51

3 Answers3

23

It is necessary to pass -std=c++11 to the compiler to enable C++11 features. Here are the steps to do so in 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
  • Click into the semicolon delimited list of compiler switches to reveal elipses and click on the elipses.
  • Click the checkbox for -std=c++11

screenshot of the project settings windows

Matthew James Briggs
  • 2,085
  • 2
  • 28
  • 58
  • 1
    Just a comment: CodeLite does not "compile" codelite is an IDE, which invokes the compiler (in your case, gcc). So the title is a bit misleading. – Eran Mar 01 '15 at 06:30
  • This is a perfect/standard way in CodeLite to invoke compiler's support for C++11. BTW: a lot of compiler's options can be selected here as you can see. – htredleaf Dec 07 '15 at 02:52
4

If you are using C++11 extensions, compilers want it to be flagged. Without it they may throw warnings and errors. That's because some of C++11 changes are not backward-compatible, e.g. the use of auto.

For example, in gcc you should have

gcc -std=c++11

Check if your compiler shouldn't have such parameter as well!

masoud
  • 55,379
  • 16
  • 141
  • 208
CygnusX1
  • 20,968
  • 5
  • 65
  • 109
0

I suppose this is because that your default std version is not c++11. To change to c++11, if you are using your terminal, you should type in the following command:

g++ yourfile.cpp -std=c++11
Don Wayne
  • 11
  • 2