Comp 2673, Spring 2002
March 29, lecture notes
A few random Unix commands (you're asked about these on homework):
diff - determine the differences between two files
grep - search for a pattern in a file or files
env - print or set environment variables. These variables include, for
example, PATH, TERM, HOME, SHELL, LOGNAME. It's one of the ways
the system keeps track of who you are what how to interact with you.
du - How much disk space is used by a directory
touch - set the modified time for a file to be the current time; creates
the file if it doesn't exist.
cat filename - displays a file to the screen
more filename - displays a file one screen at a time
view filename - read-only version of vi
Compiling a c++ program in Unix
The compiler is called g++ - developed by the GNU project
Simplest usage:
% g++ foo.cpp
Automatically produces an executable called a.out
In class today we'll write the simplest possible c++ program and compile it.
g++ supports many flags, some of which you are required to know.
Separate compilation
First recall that when you compile a program which is made up of lots of
`.cpp' files, the compilation proceeds in 2 steps. The first step is to
compile each of the `.cpp' ("source") files to a `.o' ("object") file.
The second step is to link together all of the `.o' files, as well as
any `library' files into an executable file (such as `a.out').
To make a .o file from a .cpp file, use the `-c' flag, as in:
% g++ -c fname1.cpp
This command will create an object file called fname1.o
To link a bunch of object files together, no special flag is needed,
we just list all in the g++ command:
% g++ fname1.o fname2.o fname3.o
This will link all of the named .o files together, and create an
executable file called `a.out'.
The compiler always names executables `a.out' unless we tell it
otherwise, by using the -o flag, as in:
% g++ -o myprog fname1.o fname2.o fname3.o
This command does the same thing as the previous command, except that
it puts the result in `myprog' rather than `a.out'.
Recompiling when a file is modified:
Let's say you now modify fname1.cpp, to recompile you just do:
% g++ -c fname1.cpp
% g++ -o myprog fname1.o fname2.o fname3.o
You do NOT need to recompile fname2.cpp and fname3.cpp, hence if you
have a project with 20 .cpp files this save LOTS of time.
Separate compilation - makefiles
A Unix program called make is used to handle the grungy
details of compiling programs with multiple source/header files
It lets you organize dependencies so only the necessary files are compiled
when file(s) are modified.