34
Emacs, Compilation, and Makefile C151 Multi-User Operating Systems

Emacs, Compilation, and Makefile C151 Multi-User Operating Systems

Embed Size (px)

DESCRIPTION

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]

Citation preview

Page 1: Emacs, Compilation, and Makefile C151 Multi-User Operating Systems

Emacs, Compilation, and Makefile

C151 Multi-User Operating Systems

Page 2: Emacs, Compilation, and Makefile C151 Multi-User Operating Systems

emacs PreliminariesEmacs is a text editorStrength of emacs: command keysMeta Key

Under Linux or Windows/putty: the Alt keyUnder 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].

Page 3: Emacs, Compilation, and Makefile C151 Multi-User Operating Systems

emacs PreliminariesStart emacs:

emacs –nw file_nameSame 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]

Page 4: Emacs, Compilation, and Makefile C151 Multi-User Operating Systems

Inserting and Replacing TextThe 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].

Page 5: Emacs, Compilation, and Makefile C151 Multi-User Operating Systems

Navigationemacs 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].

Page 6: Emacs, Compilation, and Makefile C151 Multi-User Operating Systems

NavigationTo 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->].

Page 7: Emacs, Compilation, and Makefile C151 Multi-User Operating Systems

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.cppBy 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

Page 8: Emacs, Compilation, and Makefile C151 Multi-User Operating Systems

Compiling Programs with g++g++ -o result source_file.cc

Create an executable file called result

Page 9: Emacs, Compilation, and Makefile C151 Multi-User Operating Systems

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.cppg++ -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)

Page 10: Emacs, Compilation, and Makefile C151 Multi-User Operating Systems

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.

Page 11: Emacs, Compilation, and Makefile C151 Multi-User Operating Systems

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.

Page 12: Emacs, Compilation, and Makefile C151 Multi-User Operating Systems

How to save timeUse makefileA makefile is a file (script) containing :Project structure (files, dependencies)Instructions for files creationNote that the Makefile mechanism is not

limited to C programs and Linux/Unix systems

Page 13: Emacs, Compilation, and Makefile C151 Multi-User Operating Systems

MakefilesMakefiles are command files that we can use

for easy compilation of programs.A makefile is invoked with the command makemake. 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-f option with its name:make –f Makefile1make –f Makefile1make –f ../C151/Makefilemake –f ../C151/Makefile

Page 14: Emacs, Compilation, and Makefile C151 Multi-User Operating Systems

14

Line-OrientedMakefile 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.

Page 15: Emacs, Compilation, and Makefile C151 Multi-User Operating Systems

Basic Makefile Format

# Comments

VAR=value(s) # Actually, it is a macro

target: list of dependencies<tab> command1 to achieve target<tab> command2

Page 16: Emacs, Compilation, and Makefile C151 Multi-User Operating Systems

Example of a Makefile for previous programs

result: file1.o file2.og++ –o result file1.o file2.o

file1.o: file1.cppg++ –c file1.cpp

file2.o: file2.cppg++ –c file2.cpp

Page 17: Emacs, Compilation, and Makefile C151 Multi-User Operating Systems

Project structureProject structure specifies module

dependenciesExample :

Program contains 3 filesmain.cpp, sum.cpp, sum.hsum.h is included in both .cpp filesExecutable should be the file result

result (exe)

sum.omain.o

sum.cppsum.h sum.hmain.cpp

Page 18: Emacs, Compilation, and Makefile C151 Multi-User Operating Systems

makefileresult: main.o sum.o

g++ –o result main.o sum.o

main.o: main.c sum.hg++ –c main.cpp

sum.o: sum.c sum.hg++ –c sum.cpp

Page 19: Emacs, Compilation, and Makefile C151 Multi-User Operating Systems

Rule syntax

main.o: main.cpp sum.h g++ –c main.cpp

tab

dependency action

Rule

Page 20: Emacs, Compilation, and Makefile C151 Multi-User Operating Systems

20

DependenciesThe list of dependencies can be:

FilenamesOther target namesVariables

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.

Page 21: Emacs, Compilation, and Makefile C151 Multi-User Operating Systems

Make operation - exampleFile Last Modifiedresult 10:03 main.o 09:56sum.o 09:35main.cpp 10:45sum.cpp 09:14sum.h 08:39

Page 22: Emacs, Compilation, and Makefile C151 Multi-User Operating Systems

Make operation - exampleOperations performed:

g++ –c main.cppg++ –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 re-linking).

Page 23: Emacs, Compilation, and Makefile C151 Multi-User Operating Systems

Variables(Macros) and wild cardExamples of variables:HOME = /home/courses/cop4530/spring02CPP = $(HOME)/cpp

Page 24: Emacs, Compilation, and Makefile C151 Multi-User Operating Systems

Using Variables and wild cardCC = g++OBJECTS = main.o sum.o

result: $(OBJECTS)$(CC) –o $@ $(OBJECTS)

%.o : %.cpp sum.h          $(CC) –c *.cpp

$@: file name of the targetThe “%” is used to indicate a wild card.This is not a good solution. Why?

Page 25: Emacs, Compilation, and Makefile C151 Multi-User Operating Systems

Other Makefile Optionsmake

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 anywhereMake run Will run the executable created

make cleanWill remove intermediate files

Page 26: Emacs, Compilation, and Makefile C151 Multi-User Operating Systems

Make all example 1all: result1 result 2result1: file1.o file2.o

g++ –o result file1.o file2.oresult2: file1.o file2.o

g++ –o result file1.o file2.ofile1.o: file1.cpp

g++ –c file1.cpp file2.o: file2.cpp

g++ –c file2.cpp

Command:make allmake ( can generate same result, why?)

Page 27: Emacs, Compilation, and Makefile C151 Multi-User Operating Systems

Make all example 2result1: file1.o file2.o

g++ –o result file1.o file2.oresult2: file1.o file2.o

g++ –o result file1.o file2.oall: result1 result 2file1.o: file1.cpp

g++ –c file1.cpp file2.o: file2.cpp

g++ –c file2.cpp

Command:make allmake (result will be different, why?)

Page 28: Emacs, Compilation, and Makefile C151 Multi-User Operating Systems

Remove intermediate filesresult: file1.o file2.o

g++ –o result file1.o file2.ofile1.o: file1.cpp

g++ –c file1.cpp file2.o: file2.cpp

g++ –c file2.cppclean:

rm –rf *.o

Command:make clean

Page 29: Emacs, Compilation, and Makefile C151 Multi-User Operating Systems

Remove intermediate files: Using variables

OBJS = main.o sum.oresult: main.o sum.o

gcc –o result main.o sum.omain.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

Page 30: Emacs, Compilation, and Makefile C151 Multi-User Operating Systems

Run the programOBJS = main.o sum.oresult: main.o sum.o

gcc –o result main.o sum.omain.o: main.c sum.h

gcc –c main.c sum.o: sum.c sum.h

gcc –c sum.c clean:

rm –rf $(OBJS)run:

resultCommand (after command make):make run

Page 31: Emacs, Compilation, and Makefile C151 Multi-User Operating Systems

ExampleSuppose that we want to compile a

program called rover and that it is composed of the following 3 files:wheels.cc including wheels.hrover.cc including wheels.h and rover.hterrain.cc including terrain.hmain.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/

Page 32: Emacs, Compilation, and Makefile C151 Multi-User Operating Systems

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.hg++ -c wheels.cc

rover.o: rover.cc wheels.h rover.hg++ -c rover.cc

terrain.o: terrain.cc terrain.hg++ -c terrain.cc

main.o: main.cc rover.h terrain.hg++ -c main.cc

clean: rm rover *.o

Page 33: Emacs, Compilation, and Makefile C151 Multi-User Operating Systems

Example (using variables)objects = wheels.o rover.o terrain.o main.olibs = -lmarslibdir = -L/usr/lib/planetsrover: $(objects)

g++ $(objects) $(libs) $(libdir) -o roverrover.o: rover.cc wheels.h rover.h

g++ -c rover.ccterrain.o: terrain.cc terrain.h

g++ -c terrain.ccmain.o: main.cc rover.h terrain.h

g++ -c main.ccclean:

rm rover $(objects)

Page 34: Emacs, Compilation, and Makefile C151 Multi-User Operating Systems

Reading AssignmentTextbook: Chapter 7http://www.eng.hawaii.edu/Tutor/Make/inde

x.html