Tag Archives: c++11

C++11 with eclipse-cdt

Many modern programming language has cool language features like lambdas and automatic memory management. You can also use some of these modern and cool features in C++11. But writing C++11 in eclipse is not straight forward, as you need to configure for it. I will explain the configuration requirement for eclipse-cdt so that you can writing C++11 code in it.

  • I will be using eclipse neon for this demonstration but it should work with other version of cdt as long as the compiler has C++11 support for it.
  • I will only consider GNU-C++ compiler but it should work with other compilers (give it has support and you know the flags)
  • I am using linux (ubuntu) as platform. So, to use threading, I need pthread library. You may or may not need it based on your platform.

Let us first consider a simple C++11 code in eclipse.

Fire up your eclipse and create a new C++ project, lets call it “helloworld” and put the following code to it.

#include
#include #include
#include
using namespace std;

int main() {
future val = async(std::launch::async, []() {
return string(“hello world c++11”);
});
cout << val.get() << endl;
return 0;
}

This program is demonstrating two features of C++11; threading and lambda functions. The async method spawns a thread which invokes the lamba function to returns a string. This value will be captured by the future type.

Now whenever we try to access the output by calling the val.get() it will stop executing the main process until the value is returned by the threaded function.
See how easy it was to create thread and synchronize them?

Now if we try to compile the project, it will give you bunch of errors. Because cdt is trying to compile the project according to old C++ standard.

So let us first solve the issues one by one.

Fix the compiler:

Problem: Compilation failing.

Reason: Compiler is trying to use the old C++ standard to compile the project.

Solution: Tell the compiler to compile your project in C++11 standard.

Steps:

[Project]–> Right Click –> Properties –> “C/C++ Build” –> “Miscellaneous” –> “Other Flags” –> Add “-std=c++11” at the end.

003

This flag tells the compiler that it should use C++11 standard while compiling the code.

Now add the pthread library. We need it because we are using threading library, which in turns depends on pthread in ubuntu.

[Project]–> Right Click –> Properties –> “C/C++ General” –> “Paths and Symbols” –> “Libraries” –> Add –> put “pthread” and click ok.

008

Now if you try to compile your code will compile correctly and can execute.

Fix the editor:

Problem: Auto complete is not working properly.

Reason: Editor does not see the C++11 types

Solution: Force editor to see the C++11 types

Steps:

[Project]–> Right Click –> Properties –> “C/C++ General” –> “Preprocessor Include” –> “GNU C++” –> “CDT User Setting Entries” –> Add

005

In the new popup box select type as “Preprocessor Macro”, set Name field as “__cplusplus” and the value to “201103L”. Click “Ok” and “Apply” to go back to project.

Why did we add __cplusplus=201103L? Because if you go into any C++11 headers you will find that, if this variable is not defined accordingly, then the editor cannot see the new types.

004

Right click the project and hover over “Index” and click “Rebuild”.

007.png

Happy coding in C++11.

Leave question or suggestion in the comment section.