Lab 3 September 12, 2010 CS-2303, System Programming Concepts, A-term 2012 Objective Laboratory Assignment #3 Eclipse CDT Due: at 11:59 pm on the day of your lab session To learn to learn to use the Eclipse C/C++ Development Toolkit. Introduction An Integrated Development Environment (IDE) is a software application (or set of software applications) to provide a coordinated set of editors, compilers, debuggers, and other tools for software development, both for individual programmers but more importantly for teams and large organizations. IDE s have been around for many years and have grown in features and functionality. Eclipse began in around 2000 as an IBM project to compete with Microsoft s Visual Studio. It has since grown into an open-source system capable of support a wide variety of languages and tools via plug-ins. Some students are already familiar with Eclipse for development of Java programs. Eclipse CDT is a configuration of Eclipse that has the plug-in for C and C++ development already installed. Eclipse CDT is installed on the WPI CCC Linux systems and on the Windows workstations in most of the WPI public computer laboratories. It is freely downloadable to your Windows, Macintosh, or Linux personal computer or laptop. In this Laboratory Assignment, you will learn how to import a simple C programming project into Eclipse, build it, and get it running. In subsequent assignments, we will learn how to take advantage of other tools in Eclipse CDT. Getting Started Sign the attendance sheet. Create a directory to hold the files for Lab 3. Download and unzip the same zip file that you used for Lab 2, namely from the following URL: http://web.cs.wpi.edu/~cs2303/a12/common/lab2files.zip Your directory should now contain the four files intarray.c, intarray.h, sinewave.c, and makefile. Start the version of Eclipse named Eclipse x86. This can be found in the Start Menu under Start Menu All Programs Eclipse Eclipse x86 If you have never used Eclipse before, it may ask you where you want to keep your Workspace. Your workspace is a directory where Eclipse stores files and information regarding your various Eclipse pro- 1
jects. A convenient place is in your home (i.e., FILER) directory. If you already have an Eclipse workspace, you may simply add the projects of this lab to it. Once Eclipse finishes loading, you should switch to the CDT perspective by doing the following: Open the Window menu, hover over Open Perspective, and click Other. Select C/C++ from the list and click OK. In the C/C++ perspective, you can create a new C Project. To create the new project: Open the File menu, hover over New, and click C Project. In the window that appears, you should enter a name for your project (for example, Sinewave) and select the Empty Project type under the Makefile project category. Once you select the project type, you will be able to select the MinGW GCC toolchain from the list box on the right. Click Finish to create the new project. Next, you need to make changes to the Project configuration so that the project will build on the WPI public laboratory computers. 1 You may need to alter this configuration if you bring the Project back to your personal computer. Right-click on the project in Project Explorer and click Properties. Open the C/C++ Build section and click Environment. Select the PATH variable and click the Edit button. Click in the Value field and place the cursor at the beginning of the line. Note: It is important that you do not change the existing value of the PATH variable, you simply want to append the following. Copy and paste the following path, including the ending semicolon: C:\cccapps\MinGW\msys\1.0\bin; Click OK to close the Edit variable window and click OK again to save your changes to the project configuration. Populating and Building the Project Open the directory where you extracted the files in Windows Explorer. Hold the CTRL key and click on each of the files you want to import: intarray.c, intarray.h, makefile, and sinewave.c. Once you have selected the files, drag them into the Eclipse window and release them over your project folder in the Project Explorer. Select the option to copy the files and click OK. Copies of the files should now be included in your project within your workspace. To build/compile your project you need to edit the makefile to include an all target. This is because when Eclipse runs make, it invokes the command make all. Add the following line above the sinewave target: all: sinewave To ensure that the clean command does what you expect, you should also add *.exe to the arguments of the rm command under the clean target. Save the changes to your makefile. 1 These changes up through the path C:\cccapps\MinGW\msys\1.0\bin are due to the configuration of the software on the WPI laboratory computers. If you install Eclipse CDT and MinGW on your own Windows systems as instructed below, the change to the path environment variable may not be necessary. 2
To build the project, Right-click on the Project in Project Explorer Click Build Project Alternatively, you can select Build Project or Build All under the Project menu. If you open the Console tab at the bottom of the Eclipse perspective (i.e., window), you should see that your project compiled successfully. To run the project, Right-click on the Project in Project Explorer and hover over Run As Click Local C/C++ Application, select the MinGW gdb option, and click OK. Assuming everything went well, your program should have run and you should see the output of the sinewave program on the Console tab. You can expand the Console window by double clicking on the tab itself. The row of four tabs in the bottom panel of the Eclipse window will expand to fill the entire window. Double click again on the tab, and those four tabs will shrink back to their original size. Exporting your Project To submit your project or to easily transfer it to another computer, you should export the project as a zip archive from Eclipse. To do this, Clean the project first. Do this before anything else, so that you don t submit a lot of temporary and/or object files. Right click on the project name in the Project Explorer and select Clean Project. Alternatively, select Clean from the Project menu. Right-click on the Project and click Export. Under the General category, choose the Archive File option and click Next. Click Browse and choose where you would like to save the project archive. Click Finish to export the Project. You should now have a zip file suitable for submitting to Turnin or for porting to another environment. If you open the zip file, you should find copies of the three original source files, your modified makefile, plus several other files and/or folders with names that begin with a dot. These are Eclipse s configuration files for this project. To run the program another platform without Eclipse, unzip the archive file and run the make command to build on that platform. You can then run the program there. The graders will be using this method when grading your projects during the rest of this course. Submit your archive file to the web-based Turnin system at https://turnin.cs.wpi.edu:8088/ This project is Lab 3. Be sure to complete this step before midnight on the day of the lab session. Providing input and arguments to your program Most programs need some sort of input or some arguments on the command line. These can be provided in the project properties. Right-click on the project name and select Properties. Select Run/Debug settings in the left panel, and select the name of the executable file in the middle panel, and click the Edit button on the right. This should bring up a dialog resembling the following: 3
Click the Arguments tab and enter the arguments, file redirections, and pipes exactly as you would have entered them from a command shell. Do not, however, include the name of the program, because that will be provided by Eclipse. For example, the Professor s version of the Game of Life assignment has the following text in the Program Arguments box: <./LifeTextFile.txt If you have time in this Lab, import your Game Of Life project following these steps. Show it running in an Eclipse console window. Editing In the Sinewave project the Project Explorer, double click on the file sinewave.c. This brings up the Eclipse program editor. It is well-adapted to editing C and C++ programs. For example, select the variable i in the first declaration under main. Notice that all other instances of i are automatically highlighted so that you can see where it is used. Next, hover over the call to histogram() at the end of main(). Eclipse brings up a popup box showing the first few lines of code of the histogram function as it is declared in another.c file. Next, add a printf statement to main after the call to histogram. Notice that after you type the opening parenthesis, the closing parenthesis is automatically added and the cursor is positioned between the two. Likewise, when you type the double quote mark for the format string, a closing double quote is 4
automatically added, and the cursor is again positioned between the two. Similar support is provided for square brackets and curly brackets. The editor also provides automatic indentation of C and C++ programs according to generally accepted coding conventions. This makes for more readable programs. Play around with the program editor to learn more about what it can do. There are many other tricks documented under the C/C++ Development Guide that can be found under the Help > Help Contents menu. Using Eclipse on Linux Eclipse CDT is installed on the WPI Linux systems. It is easily accessible from PuTTY or your SSH client, provided that you have an X-window server running on your own computer. In a command shell, type eclipse & This will bring up Eclipse in a new window on your desktop. Everything works the same as the version on Windows or Macintosh personal computers, but the compilation is done by the GNU gcc compiler and debugging tools. While this works well functionally, it is not nearly so responsive as Eclipse running locally on your own or a public computer. 5