You are on page 1of 35

Introduction to Linux and Makefiles

Software Engineering 3K04/3M04


October 17, 2005
Prepared by M. Kinsner

What is Linux?
An operating system, in many ways like
Windows and MacOS Free can download and installed from the Internet Countless software packages for free, and most very easy to install/uninstall

Should everybody use Linux?


Certainly not
Linux better for some people, and some tasks

Linux great for:


People that enjoy playing with computers Programming Many Engineering tasks

Should everybody use Linux? (2)


Linux NOT good for:
Those who have trouble using Windows People who only want to type assignments Playing games Companies whose staff arent computer whizzes

What is a Linux Distribution


Collection of software prepared by a
company or organization (e.g. Red Hat) Central elements of Linux (kernel, standard programs) are almost the same between distributions A distribution (collection of programs) will usually have a specific purpose in mind:
Desktop computer Network router/firewall

How to acquire Linux


Easiest way:
CD-based distribution (e.g. Knoppix, Mandrake Move) download CD from Web Runs from CD and uses only RAM memory, not the hard disk Take the CD out of computer, restart, and no trace of Linux

How to acquire Linux (2)


Faster, but more dangerous approach:
Install Linux to your hard disk Much faster to use than running from CD Easy to damage an existing Windows install during the Linux install process Recommend: Install on an old computer, or on one where you
can afford to damage Windows Get help from an experienced friend

How to acquire Linux (3)


A middle-ground solution = CYGWIN Installs within Windows, and allows you to
use a Linux shell and many Linux utilities including the compilers from within Windows Not really Linux, but a collection of Linux programs built to work under Windows very useful!

Now for some examples


Examples of why Linux is useful for

Electrical and Computer Engineering students

C Compiler
Free, and installed by default One of the oldest and most stable

compilers Very powerful, with multitude of options

Called: gcc

Compiling programs
At first glance, harder to use than
conventional graphical compilers There are graphical frontends, but the easiest way to get started is with the conventional command line Later we will explore Makefiles, which greatly simplify the compilation of programs

Compiling programs (2)


gcc codeFile1.c -o outputFileName

Run the compiled program by typing:


./outputFileName - outputFileName is the executable program

Compiling programs (3)


To compile multiple files into one project:
gcc codeFile1.c codeFile2.c -o outputFileName

- Adding multiple filenames is similar to adding files to a Project in a graphical development environment

Compiling programs (4)


Some useful options: Debug symbols: -g
gcc g codeFile1.c -o outputFileName Allows program to be debugged with a debugger such as gdb

All warnings: -Wall

gcc Wall codeFile1.c -o outputFileName Provides extra warnings, useful for tracking down some bugs

Editors

Linux has many powerful text editors,

loved by programmers around the world


Graphical editors are available Most powerful are VIM and EMACS (nongraphical) Both have steep learning curves, and are nothing

like Windows editors Take days of frustration to learn how to do simple tasks, but once good at using, unbelievably powerful Huge debates on which is better, but if you decide to learn one, VIM is generally considered best for programming

Other advantages for power users


Most existing programming languages are
available under Linux Many very powerful scripting languages available such as TCL/TK Utilities make tasks that would be hard in other operating systems very easy Command line shells very powerful at the hands of an experienced user

Want more information?


IEEE Student Branch Linux Crash Course
You get 3 hours of hands-on introduction Take home a CD-based Linux distribution

Countless resources on the Web for

getting started with Linux many of the free tutorials are quite good

Makefiles
A Makefile is the configuration file used by
a standard program called Make Make is like a project manager in a graphical development environment, but includes many extra features Allows an entire project to be intelligently built with one command on the command line

Makefiles (2)
What do we mean by intelligent building
of a program?
For small programs, we dont care about compilation time compiling and building an assignment doesnt take long When building a large project, such as an operating system, the Make utility helps by recompiling only the files that have changed since the last compilation

Dependencies
Sometimes one file depends on another
file
e.g. a C file depends on its header files If a header file changes, the C files that #include that header file should be recompiled to take into account the changes to the header

Dependencies (2)
Final executable file (MyProject.exe) main.o main.c interface.h interface.o interface.c

Dependencies (3)
Final executable file (MyProject.exe) main.o main.c interface.h interface.o interface.c

Dependencies (4)
Final executable file (MyProject.exe) main.o main.c interface.h interface.o interface.c

A Simple Makefile Rule


Dependency line hello hello: hello.c depends on hello.c gcc hello.c o hello Command to turn hello.c into hello

Save this text as name Makefile in the


same directory as the source code

To build the project, type make Result is an executable named hello

A Simple Makefile (2)


hello: hello.c gcc hello.c o hello

If hello file exists, and the file creation

time is newer than hello.c, what should make do?


Does nothing, since there have been no changes to hello.c since the last time that the hello executable was created

Generic form of a rule


target: prerequisite1 prerequisite2 command to make target
Target is the output file Prerequisites are the files that are needed by target (and that can cause target to be recompiled if they change) Command is the actual command to turn the prerequisites into the target

Multiple Targets
main.o main.c

Final executable file (MyProject.exe) interface.o interface.h interface.c

MyProject: main.o interface.o


gcc main.o interface.o o MyProject main.o: main.c interface.h

gcc c main.c o main.o


interface.o: interface.c interface.h gcc c interface.c o interface.o

Multiple Targets (2)


How does make know which target is the
primary one?
First target listed in the file is the master
MyProject: main.o interface.o
gcc main.o interface.o o MyProject main.o: main.c interface.h

gcc c main.c o main.o


interface.o: interface.c interface.h gcc c interface.c o interface.o

Multiple Targets (3)


Can make non-master targets by typing,
for example: make main.o
MyProject: main.o interface.o
gcc main.o interface.o o MyProject main.o: main.c interface.h

gcc c main.c o main.o


interface.o: interface.c interface.h gcc c interface.c o interface.o

Multiple Targets (4)


If interface.h is changed and saved, Make
will discern that both main.o and interface.o need to be recompiled, followed by a build of MyProject from the new main.o and interface.o
MyProject: main.o interface.o gcc main.o interface.o o MyProject

main.o: main.c interface.h


gcc c main.c o main.o interface.o: interface.c interface.h gcc c interface.c o interface.o

The clean target


Used to remove object files that take up hard
disk space

In the Makefile:
clean: rm rf ./*.o To run make with this target, type: make clean

Variables
In a large makefile, good idea to use
variables to make later changes easy
For example, rather than typing gcc in the command part of every rule, create a variable at the top of the Makefile: COMPILER = gcc

Commands can then say:


${COMPILER} sourceFile.c o executableFile

Variables (2)
Our earlier example using a variable:
COMPILER = gcc MyProject: main.o interface.o

${COMPILER} main.o interface.o o MyProject


main.o: main.c interface.h ${COMPILER} c main.c o main.o interface.o: interface.c interface.h ${COMPILER} c interface.c o interface.o

Only the beginning


We wont cover more advanced topics, but
makefiles can do much more, such as:
Automatically figure out what a files dependencies are (in cooperation with gcc) Have targets to install the program, and to do other non-compilation tasks Configure a programs compilation based on the libraries currently installed on the system And much more

Resources
There are resources on the Web, such as:
http://www.eng.hawaii.edu/Tutor/Make/index.html http://www.opussoftware.com/tutorial/TutMakefile.htm

A good book published by OReilly named:


GNU Make

If you have Linux installed, there are many

examples on your system since almost every Linux program has a Makefile

You might also like