Step 0: Install MinGW GCC or Cygwin GCC
To use Eclipse for C/C++ programming,you need a C/C++ compiler. On Windows,you could install either MinGW GCC or Cygwin GCC. Choose MinGW if you are not sure,because MinGW is lighter and easier to install,but having less features.
- MinGW GCC: Read "".
- Cygwin GCC: Read "". Make sure that you select "
gcc
","g++
","gdb
",and "make
" packages under the "Devel
" (Development) category - these packages are not part of the default installation.
Step 1: Install Eclipse C/C++ Development Tool (CDT)
Two ways to install CDT,depending on whether you have previously installed an Eclipse:
- If you have already installed "Eclipse for Java Developers" or other Eclipse packages,you could install the CDT plug-in as follows: Launch Eclipse ? Help ? Install New Software ? In "Work with" field,pull down the drop-down menu and select "Kepler - http://download.eclipse.org/releases/kepler" (or juno for Eclipse 4.2; or helios for Eclipse 3.7). In "Name" box,expand "Programming Language" node ? Check "C/C++ Development Tools" ? "Next" ? ... ? "Finish".
- If you have not install any Eclipse package,you could download "Eclipse IDE for C/C++ Developers" from ,and unzip the downloaded file into a directory of your choice.
Step 2: Configuration
You do NOT need to do any configuration,as long as the Cygwin or MinGW binaries are included in the PATH
environment variable. CDT searches the PATH
to discover the C/C++ compilers.
2.1??C++ Program
Step 0: Launch Eclipse
- Start Eclipse by running "
eclipse.exe
" in the Eclipse installed directory.
- Choose an appropriate directory for your workspace (i.e.,where you would like to save your works).
- If the "welcome" screen shows up,close it by clicking the "close" button.
Step 1: Create a new C++ Project
eclipse.exe
" in the Eclipse installed directory.For each C++ application,you need to create a project to keep all the source codes,object files,executable files,and relevant resources.
To create a new C++ project:
- Choose "File" menu ? "New" ? Project... ? C/C++ ? C++ project.
- The "C++ Project" dialog pops up.
- In "Project name" field,enter "
FirstProject
". - In "Project Types" box,select "Executable" ? "Empty Project".
- In "Toolchains" box,choose your compiler,e.g.,"Cygwin GCC" or "MinGW GCC" ? Next.
- In "Project name" field,enter "
- The "Select Configurations" dialog appears. Select both "Debug" and "Release" ? Finish.
Step 2: Write a Hello-world C++ Program
- In the "Project Explorer" (leftmost panel) ? Right-click on "
FirstProject
" (or use the "File" menu) ? New ? Source File. - The "New Source File" dialog pops up.
- In "Source file" field,enter "
Hello.cpp
". - Click "Finish".
- In "Source file" field,enter "
- The source file "
Hello.cpp
" opens on the editor panel (double-click on "test.cpp
" to open if necessary). Enter the following codes:using namespace std;
int main() {
cout << "Hello,world!" << endl;
return 0;
}
If ""
If error "unresolved inclusion" appears next to #include
statement,the "include paths for headers" are not set properly. Select "Project" menu ? Properties ? C/C++ General ? Paths and Symbols ? In "Includes" tab:
For Cygwin GCC:
- "Add" the following directories to "GNU C",where
$CYGWIN_HOME
is your Cygwin installed directory:$CYGWIN_HOMElibgcci686-pc-cygwin4.5.xinclude
$CYGWIN_HOMElibgcci686-pc-cygwin4.5.xinclude-fixed
$CYGWIN_HOMEusrinclude
$CYGWIN_HOMEusrincludew32api
- "Add" the following directories to "GNU C++",where
$CYGWIN_HOME
is your Cygwin installed directory:$CYGWIN_HOMElibgcci686-pc-cygwin4.5.xincludec++
$CYGWIN_HOMElibgcci686-pc-cygwin4.5.xincludec++i686-pc-cygwin
$CYGWIN_HOMElibgcci686-pc-cygwin4.5.xincludec++backward
$CYGWIN_HOMElibgcci686-pc-cygwin4.5.xinclude
$CYGWIN_HOMElibgcci686-pc-cygwin4.5.xinclude-fixed
$CYGWIN_HOMEusrinclude
$CYGWIN_HOMEusrincludew32api
For MinGW GCC:
- "Add" the following directories to "GNU C",where
$MINGW_HOME
is your MinGW installed directory:$MINGW_HOMElibgccmingw324.6.xinclude
$MINGW_HOMEinclude
$MINGW_HOMElibgccmingw324.6.xinclude-fixed
- "Add" the following directories to "GNU C++",where
$MINGW_HOME
is your Cygwin installed directory:$MINGW_HOMElibgccmingw324.6.xincludec++
$MINGW_HOMElibgccmingw324.6.xincludec++mingw32
$MINGW_HOMElibgccmingw324.6.xincludec++backward
$MINGW_HOMElibgccmingw324.6.xinclude
$MINGW_HOMEinclude
$MINGW_HOMElibgccmingw324.6.xinclude-fixed
NOTE: To find the header paths,you can do a search on headers such as "stdio.h
" (for C) and "iostream
" (for C++) under the Cygwin or MinGW installed directory.
libmpfr4
" in cygwin.Step 3: Compile/Build
Right-click on the "FirstProject
" (or use the "Project" menu) ? choose "Build Project" to compile and link the program.
Step 4: Run
To run the program,right-click on the "FirstProject
" (or anywhere on the source "test.cpp
",or select the "Run" menu) ? Run As ? Local C/C++ Application ? (If ask,choose Cygwin's gdb debugger) ? The output "Hello,world!" appears on the "Console" panel.
NOTE: You need to create a new C++ project for EACH of your programming problems. This is messy for writing toy programs!
2.2??C Program
Follow the same steps as above. Create a "C Project" (instead of "C++ Project"). Try the following Hello-world program (called "Hello.c
").
int main() {
printf("Hello,world!n");
return 0;
}
2.3??C++ Program with Makefile
In the previous examples,we use so-called managed-make where Eclipse automatically generated a makefile to build the program. We can also choose to write our own makefile for complete control of the building process.
Step 1: Create a C++ Makefile Project
From "File" menu ? New ? Project... ? C/C++ ? C++ project ? In "Project name",enter "HelloCppMakefile
" ? In "Project type",choose "Makefile Project ","Empty Project" ? In "Toolchains",choose "Cygwin GCC" or "MinGW GCC". Ignore the warning message.
Step 2: Write a C++ Program
Right-click on the project ? New ? Source File ? In "Source file",enter "Hello.cpp
" ? Enter the following source codes:
using namespace std;int main() {
cout << "Hello,world!" << endl;
return 0;
}
Step 3: Write a Makefile
Right-click on the project ? New ? File ? In "File name",enter "makefile
" ? Enter the following codes. Take note that you need to use a Tab (NOT Spaces) for the indent.
clean:
rm Hello.o Hello.exeHello.exe: Hello.o
g++ -g -o Hello.exe Hello.oHello.o: Hello.cpp
g++ -c -g Hello.cpp
Step 4: Build the Project
Right-click on the project ? Build Project.
Step 5: Run the Program
Right-click on the project ? Run As ? Local C/C++ Application.
[TODO] Write a makefile to compile toy-programs under one project.
3.??Read the Documentation
At a minimum,you SHOULD browse through Eclipse's "Workbench User Guide" and "C/C++ Development User Guide" - accessible via the Eclipse's "Welcome" page or "Help" menu. This will save you many agonizing hours trying to figure out how to do some things later.
Able to use a graphics debugger to debug program is crucial in programming. It could save you countless of hours guessing on what went wrong.
The following program computes and prints the factorial of n
(=1*2*3*...*n
). The program,however,has a logical error and produce a wrong answer for n
=20
("The Factorial of 20 is -2102132736
" - a negative number?!).