Dana Vrajitoru
C151 Multi-User Operating Systems

Compiling on Linux

Links

Environment Variables

Compiling Programs with g++

Compiling Multiple Source Files

Input and Output Redirection

Makefiles

Makefile Entries

Special Entries

Writing a Makefile

Example

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