Lecture 3

C151 Multi-User Operating Systems
Emacs, Compilation, and Makefile
emacs Preliminaries
 Emacs is a text editor
 Strength of emacs: command keys
 Meta Key
 Under Linux or Windows/putty: the Alt key
 Under MacOs it should be the Command key.
 In the emacs documentation, key sequences described as:




C-e – This is [Ctrl-e].
C-x C-b – This is [Ctrl-x][Ctrl-b].
C-x b – This is [Ctrl-x]b.
M-e – This is [Meta-e].
emacs Preliminaries
 Start emacs:
 emacs –nw file_name
 Same as new files and existing files
 Editing text and exectuting commands can be done at the
same time
 Quit emacs
 [Ctrl-x][Ctrl-c]
 Cancel a half-typed command or stop a running
command
 [Ctrl-g]
Inserting and Replacing Text
 The default input mode is the insert mode. To switch to the
overwrite mode, press the [Insert] key.
 To save a file, use [Ctrl-x] [Ctrl-s].
 To save a file as a different filename, use [Ctrl-x][Ctrl-w].
Navigation
 emacs uses the control keys to move in the four
directions.
 [Ctrl-b] – move left, [Ctrl-f] – move right, [Ctrl-p] –
move up, [Ctrl-n] – move down.
 To scroll full page forward, use [Ctrl-v]. To scroll full
page backward, use [Alt-v].
Navigation
 To move to the beginning of a line, use [Ctrl-a]. To move the
end of a line, use [Ctrl-e].
 To move the beginning of the word, use [Alt-f]. To move the end
of the word, use [Alt-b].
 To move to the beginning of the file, use [Alt-<]. To move to
the end of the file, use [Alt->].
Compiling C++ Programs with g++
 The g++ gnu compiler is for compiling and linking C++ programs.
gcc is the C compiler/linker.
 Compiling just one program:
g++ source_file.cpp
By default it produces an executable called a.out.
 Some options for the g++:
-o executable_name
-c
-lname
-Lpath
-Ipath
defines the name of the result
compilation only: creates an object
links a library to the executable
specifies a path into which g++ should look for
libraries
specifies a path into which g++ should look
for header files
Compiling Programs with g++
 g++ -o result source_file.cc
 Create an executable file called result
Compiling Multiple Source Files (1)
 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.
 Example: We have two source code files: file1.cpp and file2.cpp
 g++ -c file1.cpp (create file1.o)
 g++ -c file2.cpp (create file2.o)
 g++ -o result file1.o file2.o (create an executable result)
 And we need to remove intermediate object files
 rm –r f *.o (delete file1.o and file2.o)
Compiling Multiple Source Files (2)
 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.
 Although it is not needed here because standard library is
included in g++. However, for nonstandard library, we
need to include it.
How G++ Work
Previous example:
g++ -c file1.cpp (compile and create file1.o)
g++ -c file2.cpp (compile and create file2.o)
g++ -o result file1.o file2.o –lm (link and an executable result)
• -lm is not needed here.
How to save time
 Use makefile
 A makefile is a file (script) containing :
 Project structure (files, dependencies)
 Instructions for files creation
 Note that the Makefile mechanism is not limited to
C programs and Linux/Unix systems
Makefiles
 Makefiles are command files that we can use for easy
compilation of programs.
 A makefile is invoked with the command make.
 This command looks for a file with the name Makefile
(makefile) in the current directory and executes some
commands based on its content.
 If the makefile is called anything but Makefile (makefile), or if
it's not in the working directory, use the -f option with its
name:
make –f Makefile1
make –f ../C151/Makefile
Line-Oriented
 Makefile is a line-oriented file.
 If a line is too long and you would like it to span across
several lines for clarity and convenience, you may do so, but
you will have to escape the end of line by “\” at the end of
each such a line.
14
Basic Makefile Format
# Comments
VAR=value(s) # Actually, it is a macro
target: list of dependencies
<tab> command1 to achieve target
<tab> command2
Example of a Makefile for previous
programs
result: file1.o file2.o
g++ –o result file1.o file2.o
file1.o: file1.cpp
g++ –c file1.cpp
file2.o: file2.cpp
g++ –c file2.cpp
Project structure
 Project structure specifies module dependencies
 Example :
 Program contains 3 files
 main.cpp, sum.cpp, sum.h
 sum.h is included in both .cpp files
 Executable should be the file result
result (exe)
sum.o
main.o
main.cpp
sum.h
sum.cpp
sum.h
makefile
result: main.o sum.o
g++ –o result main.o sum.o
main.o: main.c sum.h
g++ –c main.cpp
sum.o: sum.c sum.h
g++ –c sum.cpp
Rule syntax
main.o: main.cpp sum.h
g++ –c main.cpp
Rule
tab
dependency
action
Dependencies
 The list of dependencies can be:
 Filenames
 Other target names
 Variables
 Separated by a space.
 May be empty; means “build always”.
 Before the target is built:
 it’s checked whether it is up-to-date (in case of files) by comparing time
stamp of the target of each dependency; if the target file does not exist,
it’s automatically considered “old”.
 If there are dependencies that are “newer” then the target, then the target
is rebuild; else untouched.
20
Make operation - example
File
result
main.o
sum.o
main.cpp
sum.cpp
sum.h
Last Modified
10:03
09:56
09:35
10:45
09:14
08:39
Make operation - example
 Operations performed:
g++ –c main.cpp
g++ –o result main.o sum.o
 main.o should be recompiled (main.cpp is
newer).
 Consequently, main.o is newer than result and
therefore result should be recreated (by relinking).
Variables(Macros) and wild card
Examples of variables:
 HOME = /home/courses/cop4530/spring02
 CPP = $(HOME)/cpp
Using Variables and wild card
CC = g++
OBJECTS = main.o sum.o
result: $(OBJECTS)
$(CC) –o $@ $(OBJECTS)
%.o : %.cpp sum.h
$(CC) –c *.cpp
 $@: file name of the target
 The “%” is used to indicate a wild card.
 This is not a good solution. Why?
Other Makefile Options
 make
 Usually only create one executable (the first
target)
 Make all
 Will look at target all and might create more than one
executables
 Target all can be placed anywhere
 Make run
 Will run the executable created
 make clean
 Will remove intermediate files
Make all example 1
all: result1 result 2
result1: file1.o file2.o
g++ –o result file1.o file2.o
result2: file1.o file2.o
g++ –o result file1.o file2.o
file1.o: file1.cpp
g++ –c file1.cpp
file2.o: file2.cpp
g++ –c file2.cpp
Command:
make all
make ( can generate same result, why?)
Make all example 2
result1: file1.o file2.o
g++ –o result file1.o file2.o
result2: file1.o file2.o
g++ –o result file1.o file2.o
all: result1 result 2
file1.o: file1.cpp
g++ –c file1.cpp
file2.o: file2.cpp
g++ –c file2.cpp
Command:
make all
make (result will be different, why?)
Remove intermediate files
result: file1.o file2.o
g++ –o result file1.o file2.o
file1.o: file1.cpp
g++ –c file1.cpp
file2.o: file2.cpp
g++ –c file2.cpp
clean:
rm –rf *.o
Command:
make clean
Remove intermediate files: Using
variables
OBJS = main.o sum.o
result: main.o sum.o
gcc –o result main.o sum.o
main.o: main.c sum.h
gcc –c main.c
sum.o: sum.c sum.h
gcc –c sum.c
clean:
rm –rf $(OBJS)
Command:
make clean
Run the program
OBJS = main.o sum.o
result: main.o sum.o
gcc –o result main.o sum.o
main.o: main.c sum.h
gcc –c main.c
sum.o: sum.c sum.h
gcc –c sum.c
clean:
rm –rf $(OBJS)
run:
result
Command (after command make):
make run
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 that can be found in /usr/lib/planets/
Example (Makefile)
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
Example (using variables)
objects = wheels.o rover.o terrain.o main.o
libs = -lmars
libdir = -L/usr/lib/planets
rover: $(objects)
g++ $(objects) $(libs) $(libdir) -o rover
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 $(objects)
Reading Assignment
 Textbook: Chapter 7
 http://www.eng.hawaii.edu/Tutor/Make/index.html