1

I have written a program using C++11 features.

/* * test.cpp * * Created on: 05-Jul-2015 * Author: avirup */ 
#include<vector> 
#include<iterator> 
#include<iostream> 
using namespace std; 
int main() { 
    vector<int> v; 
    v.push_back(5); 
    v.push_back(7); 
    for(auto i=v.begin();i!=v.end();i++) { 
        cout<<*i<<endl; 
    } 
    for(auto i=v.cbegin();i!=v.cend();++i) { 
        cout<<*i<<endl; 
    } 
} 

The program is compiling correctly and showing results but the editor is showing but red lines below valid functions like cbegin() and cend() which are constant reference iterators. which is annoying. How to get rid of this?

mpromonet
  • 11,326
  • 43
  • 62
  • 91
  • This is a common problem. Please read this for example: http://stackoverflow.com/questions/13635079/enable-c11-in-eclipse-cdt-juno-kepler-luna-indexer. –  Jul 05 '15 at 15:37
  • Try this: https://stackoverflow.com/questions/17131744/eclipse-cdt-indexer-does-not-know-c11-containers/24628885#24628885 – Galik Jul 05 '15 at 15:44
  • thanks a lot.... problem solved :) – Avirup Mullick Jul 05 '15 at 16:24

1 Answers1

0

Just for completeness since this has no answer and give an explanation.

To achieve compiling with C++ 11 (or another version) and Eclipse actually supporting it you need to do two things.

First the compiler flag needs to set so -std=c++11 or -std=c++0x is appended when calling g++ or whatever is used.

  • Open the properties for the properties for the project. Select it and right click ↦ Properties (or Alt+Enter for Windows users)
  • Go to C/C++ BuildSettings, maybe select your preferred configuration, ↦ GCC C++ Compiler (or any other compiler you use) ↦ Dialect.
  • Select from the combo or write the flag to the Other dialect flags if not present in the combo (like -std=gnu++14 or -std=c++1z).

Select compiler dialect flag

CDT will now call your compiler with -std=c++0x when compiling. Now to the part so CDT supports C++11 and does not show errors for missing types and such. My libstdc++ contains lines like

#if __cplusplus < 201103L
# include <bits/c++0x_warning.h>
#else // C++0x

that causes your errors and the actual type declarations/definitions are greyed out if you view them in the C/C++ editor. __cplusplus needs to be set correctly with #define __cplusplus 201103L so they will be parsed and indexed by CDT. This is also done via the project settings or can also be done for the whole workspace.

  • Go again to the project settings.
  • C/C++ GeneralPreprocessor Include Paths, Macros etc., also maybe select your preferred configuration, ↦ tab Providers.
  • Select the entry for your compiler (for me it’s CDT GCC Built-in Compiler Settings MinGW.
  • Add -std=c++11 or -std=c++0x to the Command to get compiler specs textfield at any location.
  • Optional: Select Allocate console in the Console View and hit apply. You should now see something like #define __cplusplus 201103L in the console.

Set index flag for discovery

To set it for the whole workspace just check Use global provider shared between projects and click on Workspace Settings where an almost identical dialog opens.

I’m currently writing a plug-in that extends the new C/C++ project wizard where one can select the C++ version for the project that sets the compiler flag correctly and also the above setting for the indexer and some other stuff. But dunno if it will ever will be integrated into CDT or needs to be installed via plug-in. But it sure will be in https://www.cevelop.com in a few months.

bugybunny
  • 544
  • 5
  • 13