SE 333, Lab 2: Using Eclipse

advertisement
SE 333, Lab 2: Using Eclipse
The following are directions to run Xming, to run putty with X Windows enabled, to run eclipse off of the
Computer Science and Software Engineering Linux box, which is named IO; to setup a C Makefile project in
Eclipse and lastly to run and debug a C++ program.
Running (and installing if necessary) Xming
1. Xming is an X Server for Microsoft Windows. It is needed to support connections to IO and to be able to
use X programs like xman, xcalc, xclock, xterm, Eclipse etc. Computers in labs 206 and 217 have Xming
already installed. If you need to install Xming on your personal machine, see
http://sourceforge.net/projects/xming/
To run Xming in the labs, do the following: Start  All Programs  Computer Science  Xming (see
the following diagram)
2. Once this is done you will see the Xming icon in the icon tray.
3. You can right-click on this icon to find out “about” Xming or exit from it. IT is VERY important that
Xming is running to do the following steps.
Running putty
1. Run putty: Start  All Programs  Applications  putty
Type in io.uwplatt.edu into the Host field:
2. Click on the Keyboard item under Terminal and change the backspace key to Control-H:
3. Expand the + in front of SSH:
4. Click on X11, check the X11 forwarding check box and type into the X display location textbox :
localhost:0
5. Click the Open button and enter your system username and password.
6. Now you should be able to run X commands. A simple test is to run xeyes:
The eyes will follow your mouse around – this is a simple test that X11 is working. You can close this by
clicking on the red X; you may then get a warning about closing your X client session. If you do, click on
the OK button.
7. Let’s try another command. It’s generally a good idea to follow X11 commands with an ampersand ( & )
so as to run the program in the background. This then frees up your terminal session to give other
commands. See both windows below where the Unix command “xcalc & “ was given. There may be
warnings given in the Io window about missing fonts. If you do get warnings, you can hit the return key
in the Io window to get the Unix prompt to reappear.
Running Eclipse on Io
8. Enter the command: “eclipse &” from the Io prompt and click ok to the dialog asking about your
workspace.
9. When Eclipse starts, the first page will generally be blue with about five icons on it. Hover your mouse
over them and click on the one with an arrow that says "Go to the workbench".
10. The next step is to set up editor preferences so that code is formatted correctly. You only need to do
this once.
a.
b.
c.
d.
e.
f.
Select Window|Preferences
Expand the General item by clicking on the triangle next to it.
Expand Editors
Click on "Text Editors"
Check "Insert spaces for tabs"
Change "Displayed tab width" to 3
If you like, you can also turn on showing the print margin and line numbers.
g. Expand "C/C++" (about half way down the list on the left)
h. Select "Code Style" (under C/C++).
i.
j.
Click on the Import... button
You can browse to the correct location, but it's probably fastest to just type it in. Click on the
pencil on paper icon
and enter
/usr/local/eclipse/profiles/eclipse-uwp-cpp-profile.xml
k. Click on the OK button.
l. Once it's imported, make sure "UWP C++ General" is the selected profile:
m.
n.
o.
p.
Scroll down to the Java item on the left-hand-side.
Expand the Java entry, then “Code Style”.
Click on “Formatter”.
Import the style file
/usr/local/eclipse/profiles/eclipse-uwp-java-profile.xml
q. Make sure the active profile is “UWP Java Baseline”.
r. Click on the OK button to close the preferences window.
You should now be back at the main workbench.
11. An important concept in Eclipse is the notion of a “perspective”. This consists of how the workbench is
arranged for a particular class of projects. You can create your own perspectives, but for now we’ll just
use the default one for C++. Your perspective is shown at the upper right corner and should currently
be Java:
The following steps show how to change it:
s. Click on the Open Perspective button:
t. Select “Other…”
u. Select C/C++.
v. Click on OK
The C/C++ perspective should be active. If it’s not, then the following directions won’t work for you.
Note that Eclipse is fully set up at this point, so you can exit any time you need to.
Greeting the world
The classic next step is to write a hello world program. Eclipse will even write it for you! Do the
following:
12. If you haven’t done so yet, check out your group repository to your home directory:
> cd
> svn co file:///home/svn/se333X
> cd se333X/lab2
where X is your group identifier.
13. If you have already checked it out, all you need to do is update:
> cd ~/se333X
> svn update
> cd lab2
14. In Eclipse, select File| New | C++ Project
15. Set the project name to login-hello where login is your username.
16. Clear the “Use default location” box and click on the Browse… button.
17. Browse to the se333X/lab2 folder and create a folder with your login name:
18. Click on OK.
19. In the Project type window, expand “Makefile project” and click on “Hello World C++ Project”.
Caution: there are two hello world project items – you want the one under Makefile project.
20. In the Toolchains window on the right, select Linux GCC.
You should now see something like the following:
21. Click on the Next> button.
22. Set the Author and copyright notice if you like.
23. Change the greeting to something besides “Hello world”.
24. Click on the Finish button. You should now be back at the Eclipse workbench.
25. With the login-hello project selected on the workbench, click on the Run button
26. Eclipse will prompt you for a configuration to run, select “gdb Debugger” as shown below. This is what
will allow you to set breakpoints.
27. Click on OK. Your program will run and the result shown in the Console window at the bottom of the
screen:
28. Use the Project Explorer on the left of the screen to open up your source code. You may be somewhat
surprised to find C code in the file – this appears to be a bug. The indentation style is also not following
the standard you already selected. Fix your program to use <iostream> instead of stdio and to use cout
statements.
29. Add a line after the output statement to print your name. Don’t forget the endl’s!
30. Fix the indentation and put the braces in the right places.
31. Remove the ‘void’ in main() – first, main actually accepts up to three parameters, and second the ‘void’
construct is a C-ism that isn’t appropriate for modern C++ programmers (or even ancient ones).
32. Click on the hammer
to re-build your solution. You can actually simply click on the run button, but
this allows you to see how the program is being built. Note the Console window shows that g++ is being
used to compile your program. You are now a Unix programmer.
33. Run your program to make sure it works.
34. Click on the line printing your name.
35. Right click and select Toggle Breakpoint.
36. Click on the bug icon
to run your program in debug mode. When it prompts you for the launcher,
click on “Change Workspace Settings…”.
37. Under C/C++ Application, select “GDB (DSF) Create Process Launcher” and click on OK.
38. Click on the OK button for the “Select Preferred Launcher” dialog.
39. For the Save and Launch dialog, you probably want to check the “Always save resources before
launching” box.
40. The program will then run. You may get a prompt to confirm the perspective switch to the debug
perspective; it would make sense to check in the “Remember my decision” box and then click on the Yes
button.
41. Step through and otherwise run your program until you get to the end.
42. Click on the C/C++ perspective button to return to the editor.
43. Make a trivial change to your program – perhaps a change to the documentation at the top or add some
whitespace. Then save your solution, but do not rebuild it.
44. Select File | Exit to exit eclipse. Note that clicking on the X in the upper right corner risks deleting your
work.
45. At the command-line prompt, cd to your directory:
> cd ~/se333X/lab2/login
Don’t forget to put in your group identifier and login name as appropriate.
46. A nice thing about using Eclipse is that the build can be done from the command-line prompt. Enter
> make
You should then see build commands as before. Run your solution:
> ./login-hello
The ./ part means execute the command out of your current directory. Generally commands are run
just by typing their names, but for commands not in the system directories you have to type in the path.
47. Finally, add your work to the group repository so it can be graded. But first we want to get rid of the
files that don’t need to be checked in. Type
> make clean
Now switch to the right directory, add the files, and commit the result:
> cd ../..
> svn add login
> svn commit –m “submitted lab2 solution"
Be sure to type the above commands in precisely – in particular make sure you get the right number of
dots and use double quotes in the right places.
48. Log out of Io by typing
> logout
And you’re done!
Download