Tuesday

Getting started with C++ 2011 in Eclipse IDE

Download Eclipse IDE for C/C++ Developers Windows 32-bit at
http://www.eclipse.org/downloads/
http://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/luna/SR2/eclipse-cpp-luna-SR2-win32.zip

Extract the zip file to
C:\eclipse-cpp-luna-SR2-win32
Don't name the folder as eclipse only

Create the following executable file shortcut on the desktop
C:\eclipse-cpp-luna-SR2-win32\eclipse.exe
Name the shortcut link as eclipse-cpp-luna-SR2-win32

Download and install CodeBlocks v13.12
http://www.codeblocks.org/downloads/
http://sourceforge.net/projects/codeblocks/files/Binaries/13.12/Windows/codeblocks-13.12mingw-setup.exe

Copy MinGW folder from
C:\Program Files (x86)\CodeBlocks\MinGW
to
C:\MinGW

Open Eclipse C++ IDE
Workspace: D:\my_cpp_2011
Tick the box "Use this as the default and do not ask again"
Click button OK
Click the Workbench curve down icon to start C++ programming

Eclipse Menu > Window > Preferences

Left menu > General > Editors > Text Editors
Tick the box "Insert spaces for tabs"
Tick the box "Show line numbers"
Click the button Apply

Left menu > C/C++ > Editor > Folding
Tick the box "Enable folding of preprocessor branches (#if/#endif)"
Tick the box "Enable folding of control flow statements (if/else, do/while, for, switch)"
Untick the box "Header Comments"
Click the button Apply

Left menu > C/C++ > New C/C++ Project Wizard
Check if you are on "Preferred Toolchains" tab
Go to "Project type" section box
Select "Empty Project" under Executable
Go to Toolchains section box
Select MinGW GCC row
Click the button "Make toolchain(s) preferred"
Click the button Apply

Click the button OK

Eclipse Menu > New > C++ Project
Project name: prjCpp3
Project type: Hello World C++ Project
Toolchains: MinGW GCC
Click button Finish

Go to Project Explorer frame
Right-click the prjCpp3 project folder > select "Build Project"
Right-click the prjCpp3 project folder > Run As > "1 Local C/C++ Application"

You can view the C++ code output in the Console frame

Replace the entire file content of prjCpp3.cpp with the following.
File name: prjCpp3.cpp
File content:
/**********/
/*
Output:
6 7 8
8
123
*/

#include <iostream>
#include <vector>
#include <string>
#include <cstdlib> //for atoi
#include <sstream> //for sringstream

using namespace std;

int main(int argc, char* argv[])
{
    //vector initialization list
    vector<int> vec = {6, 7, 8};

    //auto data type, range-based for loop using colon
    for(auto elt : vec)
    {
        cout << elt << " ";
    }
    cout << endl;

    //convert from int to string
    //error: 'to_string' function is not available yet
    //string s1 = to_string(vec[2]);
    stringstream ss;
    ss << vec[2];
    string s1 = ss.str();
    cout << s1 << endl;

    //convert from string to int
    //error: 'stoi' function is not available yet
    string s2 = "123";
    //int i = stoi(s2);
    int i = atoi(s2.c_str());
    cout << i << endl;

    return 0;
}
/**********/


Go to Project Explorer frame
Right-click the project and go to Properties
Left menu > C/C++ Build > Settings > Tool Settings tab
Left menu > GCC C++ Compiler > Miscellaneous
Textfield: Other Flags: put "-std=c++11" at the end
Click button OK

Toolbar > click Run button

Close Eclipse C++ IDE

No comments:

Measure execution time with Julia, example using sorting algorithms

# random integers between 1 and 100 inclusive, generate thousands of them x = rand ( 1 : 100 , 100000 ) @time sort (x; alg=InsertionSort, r...