0

Possible Duplicate:
Eclipse CDT C++11/C++0x support

I tried everything to compile C++11 code namely std::unique_ptr and it never compiles.

I followed this and this yet it still doesn't compile. I also installed gcc 4.7, and made sure that it's added to the includes directories of my eclipse c++ project, yet it still doesn't work!!

Is there anything missing please?

Community
  • 1
  • 1
Ahmed Fakhry
  • 139
  • 1
  • 4
  • 12
  • 2
    Add `-std=c++11` to the command line. – Kerrek SB Sep 04 '12 at 10:34
  • what compilation errors do you get? – juanchopanza Sep 04 '12 at 10:37
  • @KerrekSB error: unrecognized command line option ‘-std=c++11’ – Ahmed Fakhry Sep 04 '12 at 10:38
  • @juanchopanza `main.cpp: In function ‘int main()’:` `main.cpp:69:14: error: ‘unique_ptr’ is not a member of ‘std’` `main.cpp:69:30: error: expected primary-expression before ‘char’` `main.cpp:69:30: error: unable to deduce ‘auto’ from ‘’` `main.cpp:69:30: error: expected ‘,’ or ‘;’ before ‘char’ `main.cpp:69:7: warning: unused variable ‘text’ [-Wunused-variable]` When compiling this: `auto text = std::unique_ptr(new char[10]);` – Ahmed Fakhry Sep 04 '12 at 10:41
  • 2
    @AhmedFakhry sounds like Eclipse is not using the GCC 4.7 you installed. – R. Martinho Fernandes Sep 04 '12 at 10:42
  • 1
    Try running "g++ --version" on a command line, to see whether 4.7 is actually the "default" compiler. If not, you have to somehow tell Eclipse to use it, or somehow tell the Ubuntu system to make "g++" the 4.7 version. – Christian Stieber Sep 04 '12 at 10:43
  • @R.MartinhoFernandes I made sure that its directories are added to the include directories of the project – Ahmed Fakhry Sep 04 '12 at 10:44
  • @ChristianStieber you are right, it's using gcc 4.6.3, how can I make it use 4.7 by default? – Ahmed Fakhry Sep 04 '12 at 10:46
  • @KerrekSB yes that's what I'm using – Ahmed Fakhry Sep 04 '12 at 10:48
  • 1
    @AhmedFakhry: Did you `#include ` and qualify the name as `std::unique_ptr`? – Kerrek SB Sep 04 '12 at 10:48
  • 1
    @AhmedFakhry the include directories only point it to the right header files. There should be some other option to point it to the right binaries (I don't know where that option would be, I'm not very familiar with Eclipse). – R. Martinho Fernandes Sep 04 '12 at 10:49
  • @KerrekSB Thank you so much. This is the answer, please add it below so that I can mark it as an answer. – Ahmed Fakhry Sep 04 '12 at 10:58

1 Answers1

3

Which language standard GCC defaults to depends on how it has been compiled, but most distributions still set this to something like gnu++98 for C++. To use C++11, you have to pass one of the following language standard options:

g++ --std=c++0x     # <= 4.6.*

g++ --std=c++11     # >= 4.7.* (but c++0x is still accepted)

To use a unique pointer:

#include <memory>

std::unique_ptr<base[]> AllYourBase(::new base[1024]);
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084