CSC 112 - CS Home

advertisement
CSC 112
Fundamentals of Computer Science
Spring 2009
Basic Linux for Your First Program
Installing new programs
If you try to run an executable program and you don’t have it in your Linux load – for example,
the g++ compiler, emacs, xemacs, vi, or some other utility or application program – you’ll be
prompted to install it with
sudo apt-get install <application program name>
where <application program name> is replaced by the application you want. Just type that at a
command line in the terminal window.
To get a terminal window, go to Applications/Accessories/Terminal.
Creating folders (also called directories)
It’s good organization to put your files into folders (also called directories). You can make a
directory called csc112 at the command line by typing
mkdir csc112
Then you can move into that directory by typing
cd mkdir csc112
mkdir stands for “make directory” and cd stands for “change directory.”
Within the csc112 directory, you can make a folder for each program if you want. So you can
type
mkdir prog1
cd prog1
and now you’re in the subdirectory called prog1, in which you can save your programs for the
first assignment (which we’ll call prog1a.cpp and prog1b.cpp).
You can also create directories by using the GUI, which shows directories as folder icons. To
work in the GUI, go to Places/Home folder and create subfolders as you usually do in Windows.
Typing in your program, compiling it, and running it
Type your program using a text editor. A simple one is available at
Applications/Accessories/Text Editor. You should also try either emacs or vim. You can get
tutorials for how to use these on the web.
Let’s assume you have the text editor and compiler you want. After you type your program into
the text editor, save it with the suffix .cpp or .cc on it, to identify it as a C++ program.
To compile the program called prog1a.cpp, type
g++ prog1a.cpp
If there are no syntax errors in the program, this command will create an executable (binary) file
for you called a.out. a.out is the default name. If you want your executable to have a different
name, like prog1a, then type
g++ prog1a.cpp –o prog1a
The –o is an option flag telling the compiler that you’re going to rename the executable file.
After the –o, the name of the executable is expected.
To run a program called prog1a, type
./prog1a
Don’t miss the ./ It means “look in the current directory.” If you don’t do this, the operating
system won’t be able to locate the executable file.
The Bash shell
The kind of shell that you’re using to type your commands (a Bash shell) allows for tab
completion. This means that if you type the first few letters of an existing file in your current
directory, you can hit the Tab and the rest of the letters will be filled in for you.
A shell is just an environment into which you type commands. The shell interprets the
commands and executes them for you. Opening a terminal window provides you with a shell.
You can use the up arrow to backtrack through previous commands, hitting Enter when you find
the one you want.
You can use ! to repeat the previous command.
You can use !g to repeat the last command you executed that starts with g.
These are handy shortcuts.
Renaming and deleting files
To change the name of the file called a.out to prog1a, type
mv a.out prog1a
To remove the program called a.out, type
rm a.out
To remove all programs that end in .cpp, type
rm *.cpp
The remove command can be dangerous, especially if you use it with a wild card like *. You
can type something quickly by mistake and end up removing files that you really need. To
protect yourself from this, you can type
rm -i *.cpp
The –i will make the rm command act differently. In particular, it will prompt you for each file
that it tries to delete, asking you if you’re sure.
If you want this to happen every time you type the rm command, then you should set an alias
that says, in essence, “every time I type rm I really mean rm –i”.
You can put the alias in your .bashrc file. The .bashrc file is in your home directory, which for
me is /home/burg. You can use a text editor to edit it, placing in it the line
alias rm=’rm –i’
The .bashrc file is executed every time you start a new bash shell, setting defaults and aliases for
that shell.
Listing files in Linux from the command line
To see the files in your current directory, you can type
ls
which stands for “list.”
By default, files beginning with a dot are not listed, like .bashrc, even though they’re really
there. To see all the files, type
ls –a
Then you’ll see the .bashrc file in the listing.
To see more details about the files, including their permissions, type
ls –l
Getting more information
To get more information about commands, check the man pages. For example, if you want to
know about the options with the rm command, type
man rm
You can find lots of information about Linux online as well.
Debugging programs
One way to debug a program is to put print statements in, printing out the value of variables at
strategic spots.
Another way is to run the program with a debugger.
You have a text based debugger already installed in Ubuntu. It is called gdb. To use it, you have
to compile your program using the –g option, as in
g++ prog1a.cpp –g
Then start the debugger on that executable file, which by default is called a.out.
gdb a.out
If you want a debugger that has a GUI, you should install ddd. As with gdb, you have to add the
–g option to the compile command. Then, assuming your executable program is called a.out,
you can start the debugger with
ddd a.out
The debugger window will open, showing the text of your program. You set breakpoints with
the stop sign button and then click the run button. When the program stops at a breakpoint, you
can check the value of variables to see if they are what they should be. To do so, you can open
the Data window from the View menu, right click, and ask for a New Display to continuously
display the value of a variable. Alternatively, you can hover the arrow over a variable to see its
value. Be sure to learn how to use a debugger. It’s not hard, and it’s worth the time!
Download