Thursday, July 22, 2010

Writing your First C++ Program in Eclipse

Step 0: Launch Eclipse
Start Eclipse by running "eclipse.exe". Choose an appropriate directory for your workspace (i.e., where you wish to save your work). If the "welcome" screen shows up, close the "welcome" screen by clicking the "Close" button.

Step 1: Create a new C++ Project
From "File" menu ⇒ New ⇒ Project... ⇒ C++ ⇒ C++ project ⇒ Next ⇒ In "Project Name", enter "Hello". In the "Project Types" box, select "Executable" ⇒ Empty Project. In the "Toolchain" box, select "Cygwin GCC" ⇒ Next ⇒ Finish.

Step 2: Write a C++ Program
Right-click on the "Hello" project (or "src" folder) (or from the "File" menu), select "New" ⇒ Source File ⇒ In "Name", enter "test.cpp" ⇒ Finish.

Enter the following source code in the "test.cpp" editor panel (double-click on "test.cpp" to open the editor panel if necessary).

#include
using namespace std;

int main() {
cout << "Hello, world!" << endl;
}
Note: If error "unresolved inclusion" appears next to #include statement, the "include paths for headers" are not set properly. Right click the project ⇒ Property ⇒ C/C++ General ⇒ Paths and Symbols ⇒ Includes ⇒ GNU C ⇒ Add... ⇒ Enter "$CYGWIN_HOME\usr\include" where $CYGWIN_HOME is the Cygwin installed directory ⇒ GNU C++ ⇒ Add... ⇒ Enter "$CYGWIN_HOME\lib\gcc\i686-pc-cygwin\3.4.4\include\c++. (To find the header paths, do a search on headers such as "stdio.h" and "iostream" under the Cygwin installed folder.)

Step 3: Compile/Build
From the "Project" menu, choose "Build All" to compile and link the program.

Step 4: Run
To run the program, right-click anywhere on the source "test.cpp" (or from the "Run" menu) ⇒ Run As ⇒ Local C/C++ Application ⇒ (if ask, choose Cygwin's gdb debugger) ⇒ Observe the output "Hello, world!" on the "Console" panel.

Note: You should create a new C++ for each of your programming problems.

No comments:

Post a Comment