Eclipse Tutorial, Part 07: Eclipse Project CDT (C/C++) Plugin Tutorial
Eclipse Tutorial, Part 07: Eclipse Project CDT (C/C++) Plugin Tutorial
By Brian Lee
Overview:
This tutorial describes the installation of the Eclipse Project CDT (C/C++ Development
Tools) plugin on the Windows platform (running under Linux is mentioned at the end of
this tutorial). It describes the entire installation process in detail and is intended for
developers who are familiar with the Eclipse environment and would like to develop
C/C++ applications in this environment. As a result, it has been assumed that Eclipse has
already been installed and that the developer has a basic understanding of how to use
Eclipse. To learn more about the Eclipse Project, please visit:
http://www.eclipse.org
In addition, this tutorial is especially useful for students who are uncomfortable writing
C/C++ applications in a UNIX or Linux console environment. By using the Eclipse CDT
plugin, students can develop C/C++ applications within the Eclipse IDE which provides
an intuitive GUI for development. As well, applications developed this way require
minimal effort in porting to a UNIX or Linux environment.
Requirements:
This tutorial has been written in a Windows XP Professional environment, satisfying the
following requirements:
Java 2 SDK v1.4.1
Eclipse v2.1
MinGW v3.0.0-1
CDT v1.1.0 GA
1
This work was funded by an IBM Eclipse Innovation Grant.
2
© Brian Lee and David Scuse
http://www.cs.umanitoba.ca/~eclipse/1-Install.pdf
Links to downloading MinGW and the Eclipse CDT plugin will be mentioned later in this
document.
In order to be able to compile C and C++ source code using Eclipse, it is necessary to
install a C/C++ compiler for Windows. There are several C/C++ compilers available for
Windows but MinGW has been selected for this tutorial due to its easy installation and
configuration. MinGW is based on GNU toolsets and provides the header files and
libraries needed for C/C++ development. At the time of writing this tutorial, the latest
version of MinGW that has been tested to work properly with the Eclipse CDT plugin is
version 3.0.0-1. To download MinGW, please visit the following site:
http://sourceforge.net/projects/mingw/
Once the download is complete, locate the file on your system and double-click on the
file to begin the installation of MinGW.
Note: If you install to another directory, remember the directory you install to as it
will be used later in this tutorial to complete the installation of the plugin.
3
Taken from: http://www.mingw.org/mingwfaq.shtml
The following screen shows the progress of the installation. MinGW installs a
number of GNU GCC and binutils projects, including GDB which is very useful in
debugging your C/C++ applications.
7.) In order for your system to recognize the MinGW executables from any directory
without having to type the full path of the command, the PATH variable needs to be
modified:
a. Click on Start Æ Control Panel.
b. Double-click on System.
c. Click on the Advanced tab and then Environment Variables.
;C:\MinGW\bin
Note: The semi-colon is used to separate different directories for the
PATH variable and must precede the pathname C:\MinGW\bin. Also,
please make sure that there are no spaces between the semi-colon and
the path name!
f. Click OK, then OK, and OK again to apply the changes.
8.) To verify that the installation of MinGW is successful and that the PATH variable has
been changed correctly, perform the following steps:
a. Click on Start Æ Run …
b. Type cmd into the dialog box and press OK.
c. Type the following into the command prompt and then press ENTER:
gcc –v
e. If the output of the above command is similar to the screen shown below,
this means that the PATH variable has not been changed successfully.
Follow the instructions carefully on the previous pages to change the
PATH variable:
9.) After verifying that MinGW has been installed and that the PATH variable has been
changed successfully, you are ready to install the Eclipse CDT plugin.
Now that your system is ready to compile C/C++ applications, download the Eclipse
CDT plugin at the following site: http://www.eclipse.org/cdt/
4.) Once the download is complete, unzip the contents of the file:
org.eclipse.cdt-win32_1.1.0.bin.dist.zip
C:\eclipse-SDK-2.1-win32\eclipse
5.) To verify that the Eclipse CDT plugin has been installed successfully, run Eclipse.exe
from the install directory as you normally would. Then perform the following steps:
a. Click on Help Æ About Eclipse Platform.
b. Click on the Plug-In Details button.
c. If your screen looks similar to the following then the Eclipse CDT plugin
has been successfully installed:
This section describes how to create a C/C++ project in Eclipse using the CDT plugin. It
has been assumed at this point, that the system is ready to compile C/C++ applications
using tools from MinGW and that the Eclipse CDT plugin has been successfully
installed.
1.) Run Eclipse from the install directory as you normally would. Once Eclipse is open,
create a new C/C++ project by going to File Æ New Æ Project.
3.) Enter HelloWorld for the name of the project and either use the default workspace or
select an alternate location. In the example below, the project is stored in
C:\development\HelloWorld. Click Next to continue.
4.) Click on the Build Settings tab and under the Build Command section:
a. Uncheck the Use Default checkbox.
b. Enter the following in the Build Command field (note that mingw32-make
is one string, with no embedded blanks; also, the complete path the the
mingw32-make command may be specified):
mingw32-make -f makefile
c. Click Finish to continue.
6.) You should now be in the C/C++ Development Perspective, where you can see the
HelloWorld project you just created. Now that your project has been created:
a. Click on File Æ New Æ File…
b. Enter main.cpp as the name of the file.
c. Click Finish to continue.
#include <iostream>
return 0;
Press Ctrl + S to save the file. Note the following error messages in the C-Build
window below; these error messages will eventually disappear:
The above makefile is simply a generic makefile that could easily be expanded to
include a number of different source files. To learn more about the structure of
makefiles and how to write your own, check out the many resources available on the
Web.
After typing the above into the makefile, press Ctrl + S to save the file. Now, some
different error messages are displayed in the Tasks window below:
g. Now click the Run button to run your application. You should get the
following output in the console window:
Creating a C project is almost identical to creating a C++ project. First, create a new C
project called HelloWorldC. Set the Build Command property to:
mingw32-make –f makefile (the same as for the C++ project).
We did one simple test of CDT under Linux using the following configuration:
Once CDT was unzipped into the Eclipse directory, the default settings for “Standard
Make C++ project” worked correctly since GCC was already installed. If you can
compile a C++ program from a console, the default settings should be sufficient for
compiling within Eclipse.
It is quite common that C/C++ programs require user input via the console. The
following example shows how to enter input into the console of a program written using
the CDT plugin:
1.) Create a new C++ project called HelloWorld which contains the following files:
• main.cpp
• makefile
2.) Copy and paste the following code into main.cpp and save the file:
#include <iostream>
#include <string>
return 0;
}// end main
main : main.o
g++ -o main main.o -L C:/MinGW/lib/gcc-lib/mingw32/3.2.3/ -
lstdc++
main.o : main.cpp
g++ -ggdb -c main.cpp
all :
${MAKE} main
clean :
-del main.o
4.) There should be no errors in the C-Build window and the project should compile
itself successfully, generating an executable file called main.exe:
If you get a “Build Error” message in the C-Build window, verify that your build
settings are setup correctly.
mingw32-make -f makefile
d. Click OK.
e. Right-click on the HelloWorld project and click on Rebuild. The project
should compile itself successfully as shown in the screen below. Note that
main.exe has now been created.
g. Now click the Run button to run your application. You should get the
following output in the console window:
Note: If you are having problems entering text into the console, make sure
that your cursor is blinking at the end of all text in the console. To
position the cursor at the end, simply press Ctrl + End while the cursor is
blinking in the console. After performing this step, you should be able to
enter text into the console.
Occasionally, a question on an assignment may require you to create a program that reads
in command line arguments, such as the names of input and output files. The following
example shows how to provide command line arguments to applications at the beginning
of execution:
1.) Create a new C++ project called HelloWorld which contains the following files:
• main.cpp
• names.txt
• makefile
(For detailed instructions on how to do this, please refer to Part I of this tutorial under
the section “Creating a Project using the Eclipse CDT Plugin”.
2.) Copy and paste the following code into main.cpp and save the file:
#include <iostream>
#include <fstream>
#include <string>
// Constants
#define BUFFER_SIZE 50
#define ARRAY_SIZE 20
// Global variables
int numElements = 0;
ifstream inputFile;
ofstream outputFile;
if(argc != 3) {
cout << "Error: Please enter input and output files ";
cout << "as command line arguments !" << endl;
}
else {
inputFileName = argv[1];
outputFileName = argv[2];
inputFile.open(inputFileName.c_str());
outputFile.open(outputFileName.c_str());
The previous block of code simply reads in two command line arguments – the first
argument is the name of the input file and the second argument is the name of the
output file. The program will read from the input file, store the names into a vector,
3.) Copy and paste the following names into names.txt and save the file:
Brian
Michelle
Jeff
Lori
Ainsley
Jimmy
Bob
Lorisa
John
4.) Now, to specify command line arguments when running your application:
a. Click on Run Æ Run…
b. In the Configurations window, click on C/C++ Local, and then the New
button.
c. In the Name field, type HelloWorld.
d. In the C/C++ Application field, type main.exe.
e. Click the Arguments tab.
f. In the C/C++ Program Arguments textbox, enter the following:
names.txt output.txt
g. The settings on the other tabs will be sufficient for the purpose of this
example.
h. Then click on the Apply button. Your screen should look like the
following:
j. Double-click on output.txt to open the file and you should see the list of
names that was in input.txt, sorted alphabetically in ascending order:
Debugging is an essential skill that every programmer needs to master, whether to fix a
problem in an application or even to step through the application slowly to see how it
works. Fortunately debugging C/C++ applications on the Eclipse platform using the
CDT plugin is very straightforward. Continuing from the example described in the
previous section, let’s take a look at how to set breakpoints and debug the application:
c. Click the Debug button. Just minimize the Windows console window that
pops up and your screen should look something like the following:
b. The Debug window displays the current state of the stack frame of your
application. This will prove to be very helpful when debugging programs
with recursive functions.
b. Before we step into the sort method, let’s take a look at our variables. If
we expand the listOfNames array, you will notice that each element in the
array corresponds to a name read in from the unsorted input file.
i. Now click on the button or press F6 to step through the rest of the sort
method. You should notice that the array is slowly sorted into ascending
order.
7.) One of the “quirks” of the C/C++ debugger is that if you have global variables in
your program (variables defined outside your functions and main method), you will
not see them appear in the Variables window. So what if you wanted to inspect those
variables during debugging? The solution is actually quite simple:
a. Start up the application in debug mode by clicking on the bug in the
toolbar.
b. Click on the button or press F8 to hit the first breakpoint and then click
on the button or press F5 to step into the sort method.
c. Now that we’re in the sort method, to determine the value of the global
variable numElements, highlight the variable, right-click and click on
Add Expression…
e. You should now see that the global variable numElements has been added
to the Expressions window. Repeat the above steps for any other global
variable you would like to verify during debugging.