CS12

Lab 4

In this lab you will be learning how to use the Linux Operating system for development. Why do we want to use Linux? First of all, you will probably be required to write your programs under linux for all future CS classes. You should learn it now, as they will expect you to know it later. Most importantly, however, while Linux may not be as user-friendly as windows (although that is rapidly changing), it is definitely more flexible and powerful, especially for computer scientists and programmers. The fact that almost all programs that run under Linux are distributed with C/C++ source code, makes it easy for programmers to customize the operating system and applications to do anything they want.


Lab Tasks

You will not have to write a new program for this lab. This lab should be used to familiarize yourself with Linux, and learn how to write and compile your programs. You should take your program from lab 3 and do the following:



Notes on Linux

Makefiles

For each *.cpp file there should be a line saying how to compile it. Each line consists of the name of the file and what is needed to compile it. For example: if you have main.cpp and class.cpp you would have a line for each of these.

all: program_name

program_name: main.o class.o
    g++ -Wall -g -o program_name main.o class.o

main.o: main.cpp
    g++ -Wall -g -c main.cpp

class.o: class.cpp class.h
    g++ -Wall -g -c class.cpp

This might be confusing at first. You need to understand how things are compiled in steps. First of all each cpp file is compiled into an object (.o) file. After all the cpp files are compiled as object files then they are all combined (linked) together into one executable. Why do this? Well, when you are working on a large project you don't want to recompile the whole thing every time you make a small change. If you change one cpp file then only that object file needs to be recompiled.

Here is what happens when you type make:

Data Display Debugger (DDD)

If you compiled your program with the -g option then you can run it through a debugger. Debuggers are THE MOST USEFUL tool a programmer can use to find why their program is not working. You should really get familiar with the debugger and all it's features.

Starting the debugger

You can drag and drop an executable onto the DDD icon. You can also start the debugger from a console by running:

    ddd executable_name

Once ddd has started you should be able to see your code on the main screen. To set breakpoints you can right click on the line you want your program to stop and select "set breakpoint". After you set a breakpoint you can click on "Run" and your program will run until it gets to that line. You will see the output of your program on the window on the bottom. You can also type input there.The other basic commands you should no is the "step" and "next" buttons. They are used to go though your code line by line. The "next" command will skip over functions (the function will still execute but you won't go through all the steps). The "step" command will follow the code into the function so you'll go through all the steps. You can view variables and classes by double clicking on them (or right clicking on the variable and select "Display variablename". DDD has lot's of features and lot's of things you can customize. It also has extensive help files. You should learn how to use this tool well, as it will save you lot's of time.

KDE

Using KDE should be pretty self explanatory. There are help files availble and I have set most options to some sensible defaults. Some useful features are pressing ALT-F2 to run programs. Also holding down alt while dragging with the right mouse button will resize windows. Doing the same with the left mouse button will move the window arround. This is really useful for moving windows around without having to hunt down the corners or the edges. I included other utilities that allow you to easily access the floppy drive and printers. Most buttons work by just drag and dropping files onto it.

Editors

I am not going to force anyone to use any particular editor. I have included several editors: emacs, kwrite, and CodeCrusader If there are other editors you want to use go ahead. Emacs is really powerful but a real pain to learn. Kwrite is just a simple editor that does C++/C/Java/HTML syntax highliting. Code crusader is a nice editor which provides some support for projects and automatically generating makefiles. You should not use this feature until you have demonstrated that you can write your own makefiles however.