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 aren’t computer whizzes .

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 . Red Hat) • Central elements of Linux (kernel.What is a Linux Distribution • Collection of software prepared by a company or organization (e.g.

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

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.How to acquire Linux (2) • Faster. or on one where you can afford to damage Windows • Get help from an experienced friend .

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! .How to acquire Linux (3) • A middle-ground solution = CYGWIN • Installs within Windows.

Now for some examples… • Examples of why Linux is useful for Electrical and Computer Engineering students .

and installed by default • One of the oldest and most stable compilers • Very powerful. with multitude of options • Called: gcc .C Compiler • Free.

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

outputFileName is the “executable” program .c -o outputFileName • Run the compiled program by typing: ./outputFileName .Compiling programs (2) • gcc codeFile1.

Compiling programs (3) • To compile multiple files into one project: gcc codeFile1.Adding multiple filenames is similar to adding files to a “Project” in a graphical development environment .c -o outputFileName .c codeFile2.

c -o outputFileName – Provides extra warnings.c -o outputFileName – Allows program to be debugged with a debugger such as gdb • All warnings: -Wall gcc –Wall codeFile1. useful for tracking down some bugs .Compiling programs (4) • Some useful options: • Debug symbols: -g gcc –g codeFile1.

but if you decide to learn one. 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. unbelievably powerful • Huge debates on which is better. VIM is generally considered best for programming . but once good at using.Editors • Linux has many powerful text editors.

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 .

but includes many extra features • Allows an entire project to be intelligently built with one command on the command line .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.

Makefiles (2) • What do we mean by “intelligent” building of a program? – For small programs. we don’t care about compilation time – compiling and building an assignment doesn’t 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 .

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 .g.Dependencies • Sometimes one file depends on another file – e.

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

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

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

type “make” • Result is an executable named hello .A Simple Makefile “Rule” Dependency line – hello hello: hello.c –o hello Command to turn hello.c depends on hello.c gcc hello.c into hello • Save this text as name “Makefile” in the same directory as the source code • To build the project.

c.c gcc hello.c since the last time that the hello executable was created . and the file creation time is newer than hello. what should “make” do? – Does nothing. since there have been no changes to hello.c –o hello • If hello file exists.A Simple Makefile (2) hello: hello.

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 .

h gcc –c interface.c MyProject: main.c interface.c –o interface.o interface.Multiple Targets main.c Final executable file (MyProject.o –o MyProject main.exe) interface.o gcc main.o: main.o .o interface.h gcc –c main.o interface.c –o main.o interface.o main.h interface.c interface.o: interface.

c interface.c –o interface.o gcc main.h gcc –c main.o: interface.c –o main.o interface.c interface.o interface.o –o MyProject main.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: main.h gcc –c interface.o interface.o .

o –o MyProject main.c –o interface.o: main.o interface.o: interface.h gcc –c interface.o interface. for example: make main.c interface.o gcc main.o .h gcc –c main.o MyProject: main.c interface.c –o main.Multiple Targets (3) • Can make non-master targets by typing.o interface.

o interface.o gcc main.c interface. Make will discern that both main.o need to be recompiled.o interface.o –o MyProject main.o and interface.o and interface.o: interface.o interface.Multiple Targets (4) • If interface.o MyProject: main.o . followed by a build of MyProject from the new main.c –o main.h gcc –c interface.o: main.c interface.h is changed and saved.c –o interface.h gcc –c main.

/*.o To run make with this target. type: make clean .The “clean” target • Used to remove object files that take up hard disk space • In the Makefile: clean: rm –rf .

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 . good idea to use variables to make later changes easy – For example.Variables • In a large makefile.

c –o main.o ${COMPILER} main.Variables (2) • Our earlier example using a variable: COMPILER = gcc MyProject: main.c interface.c interface.o: interface.o .o interface.o –o MyProject main.c –o interface.o interface.h ${COMPILER} –c interface.o: main.o interface.h ${COMPILER} –c main.

Only the beginning… • We won’t cover more advanced topics. but makefiles can do much more. and to do other non-compilation tasks – Configure a program’s compilation based on the libraries currently installed on the system – And much more… . such as: – Automatically figure out what a file’s dependencies are (in cooperation with gcc) – Have targets to install the program.

html – http://www.htm • A good book published by O’Reilly named: – GNU Make • If you have Linux installed. such as: – http://www. there are many examples on your system since almost every Linux program has a Makefile .eng.Resources • There are resources on the