Dana Vrajitoru
C151 Multi-User Operating Systems
Compiling on Linux
Links
- The link files are the Linux equivalent of the Windows shortcuts.
- The links can be hard links or symbolic links. Hard links
are pointers to files, while the symbolic links are indirect links that
contain the path to that file.
- Symbolic links were created because the hard links have some limitations,
like they cannot point to a directory, or they cannot point to a file in
a different filesystem.
- Creating a link with the command ln:
ln source_file link_file
Option: -s to make the link a symbolic one.
Environment Variables
- The environment variables are global variables that are defined within the
shell and that can be used for many purposes.
- Their names are in general in all uppercase, like ENV.
Display the value of an environment variable:
echo $DISPLAY
- Set a value to an environment variable:
for bash:
DISPLAY=local:0.0; export DISPLAY
for tcsh:
setenv DISPLAY local:0.0
- Examples: PATH - lookup list for binaries to be executed.
HOME: user's home directory
Compiling Programs with g++
- The g++ gnu compiler for compiling and linking C++programs. gcc is the C
compiler/linker.
- Compiling just one program:
g++ source_file.cc
- By default it produces an executable called a.out.
- Some options for the g++:
-o executable_name defines the name of the result
-c
compilation
only: creates an object
-lname
links a library to the executable
-Lpath
specifies a path into which g++ should
look for libraries
-Ipath
specifies a path into which g++ should
look for header files
-g
produce
debugging information
-O
optimize
the code
Compiling Multiple Source Files
- Each file can be compiled separately with the option -c to produce an object
(the translation into machine code of the source file without links to all
the function calls).
All the files are linked together to create the executable.
- The name of all the objects to be linked in an executable must be listed
in the command.
- All the libraries we need must be linked with -l followed by the library
name minus the prefix "lib" and minus the extension.
- Example: the math library is called libm.a. To link this library, use the
command -lm.
Input and Output Redirection
- When we run a command, we can redirect the input, output, and errors from
that command from/to a file with the <, >, >> options.
- The simple > rewrites the output file, while the double one
>> appends to the file.
- Redirecting the input:
command <filename
- Any "cin >>" statement will read from that file.
- Redirecting the output:
command > filename
command >> filename
- Redirecting the standard error from a program:
command 2> filename
- An equivalent of the output redirection:
command 1> filename
- Ignoring the output of a command:
command > /dev/null
Makefiles
- Makefiles are command files that we can use for easy compilation of programs.
- A Makefile is invoked with the command make. If the makefile is called anything
but Makefile, use the -f option with its name.
- A makefile can contain the following things:
- Comments starting with a #
- Definition of variables. The makefile can also use the shell environment
variables. The are defined bash-style:
VAR = value
and invoked with the $(name)
- Entries containing list of commands to be executed under some conditions.
Makefile Entries
- A makefile contains several entries. For each of them we can define the following
things:
entry_name: list of files
command1
command2
- The list of files will be checked when we invoke the entry. If any of these files has
been modified after the last time we invoked this entry, the commands are executed.
- An entry in the makefile can invoke other entries.
- The list of commands will be executed when the entry is invoked. Each command
must be placed on a line starting with a tab.
Special Entries
- default - an entry to be executed when the command make has no argument.
- If we want to invoke any other entry, we have to use
make entry_name
- clean - a traditional entry that usually contains commands to delete executables and objects.
- all - a traditional entry that will allow you to compile anything that the makefile makes reference to.
- filename - usually there's a particular entry for each of the files to be compiled.
It will check all of its dependencies and invoke the command to compile the file.
Writing a Makefile
- Special entries: one named after the executable, and one for each object file.
- The dependencies of the executable are all the object files.
- Each object file should have as dependencies:
the source file (.cc or .cpp)
all the user-defined header files included in the source file.
- The default or all entry has the executable as dependency.
- The make command will compare the last modification date for each entry with the
last modification date for each dependency. If any dependency has changed in the meantime
or if the file does not exit, it recompiles it.
Example
- Suppose that we want to compile a program called rover and that it is composed of the following 3 files:
wheels.cc including wheels.h
rover.cc including wheels.h and rover.h
terrain.cc including terrain.h
main.cc including rover.h and terrain.h
- Suppose that the executable also needs to link to a special library called libmars.so that can be found in /usr/lib/planets/
default: rover
rover: wheels.o rover.o terrain.o main.o
g++ wheels.o rover.o terrain.o main.o -lmars -L/usr/lib/planets -o rover
wheels.o: wheels.cc wheels.h
g++ -c wheels.cc
rover.o: rover.cc wheels.h rover.h
g++ -c rover.cc
terrain.o: terrain.cc terrain.h
g++ -c terrain.cc
main.o: main.cc rover.h terrain.h
g++ -c main.cc
clean:
rm rover *.o
Using Variables
Suppose that we want to store all the objects in a variable objects and all the libraries to include in a variable called libs:
objects = wheels.o rover.o terrain.o \
main.o
libs = -lmars
libdir = -L/usr/lib/planets
rover: $(objects)
g++ $(objects) $(libs) $(libdir) \
-o rover