2

I'm trying to use C++11 but eclipse is having some trouble with it. I've used macports to get gcc48, and I've followed various guides for getting eclipse to use the new compiler, including this, this, and I've also changed the compiler command from the eclipse standard to the g++-mp-4.8 as explained here

I am trying to build the following program:

#include <iostream>
 #include<memory>
using namespace std;
int main() {
std::unique_ptr<double> ptr(new double);
*ptr = 11.345;

cout << (*ptr) << endl;

return 0;
}

The terminal will compile this fine,

make all 
Building file: ../src/C++11.cpp
Invoking: Cross G++ Compiler
/opt/local/bin/g++ -I/opt/local/bin -I/opt/local/include -O0 -g3 -Wall -c -fmessage-length=0 -std=c++0x -MMD -MP -MF"src/C++11.d" -MT"src/C++11.d" -o "src/C++11.o" "../src/C++11.cpp"
Finished building: ../src/C++11.cpp

Building target: C++11
Invoking: Cross G++ Linker
g++  -o "C++11"  ./src/C++11.o   
Finished building target: C++11

and the program runs just as expected. However, in eclipse, I still get the error message Symbol 'unique_ptr' could not be resolved.

I would like to continue using eclipse as more than just a project manager and makefile builder, so any help on this would be appreciated!

Community
  • 1
  • 1
Plamen
  • 650
  • 1
  • 8
  • 27

1 Answers1

3

Rather than using g++, one can use clang++. I used the answers Error when compiling some simple c++ code, clang 3.1 can't see unique_ptr? and How to compile a C++0x code on Eclipse CDT on mac? as guides to come up with the following steps:

  1. change the compiler in the Project->Properties->C/C++ Build -> Settings -> Gcc C++ Compiler -> Command (change g++ to clang++).

  2. in Project->Properties->C/C++ Build -> Settings -> Gcc C++ Compiler -> Miscellaneous, append -std=c++11 -stdlib=libc++ to the flags.

  3. do the same as one for the linker under C/C++ Build -> Settings -> Gcc C++ Linker -> Command

  4. in Properties->C/C++ Build -> Settings -> Gcc C++ Linker -> Miscellaneous, add -stdlib=libc++ to the Linker flags.

  5. in Properties->C/C++ General -> Preprocessor Include Paths, Macros,etc -> Providers -> CDT GCC Builtin Compiler Settings, turn off the Share option, and append -std=c++11 to the Command to get compiler specs.

The compilation works perfectly and the program runs well. That eclipse doesn't recognize the smart pointer appears to be a bug: see Turn off eclipse errors (that arent really errors)

Community
  • 1
  • 1
Plamen
  • 650
  • 1
  • 8
  • 27