Download - Linux Makefile
7/29/2019 Linux Makefile
http://slidepdf.com/reader/full/linux-makefile 1/35
Introduction to Linux and
Makefiles
Software Engineering 3K04/3M04
October 17, 2005Prepared by M. Kinsner
7/29/2019 Linux Makefile
http://slidepdf.com/reader/full/linux-makefile 2/35
What is Linux?
• An operating system, in many ways likeWindows and MacOS
• Free – can download and installed fromthe Internet
• Countless software packages for free, and
most very easy to install/uninstall
7/29/2019 Linux Makefile
http://slidepdf.com/reader/full/linux-makefile 3/35
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
7/29/2019 Linux Makefile
http://slidepdf.com/reader/full/linux-makefile 4/35
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
7/29/2019 Linux Makefile
http://slidepdf.com/reader/full/linux-makefile 5/35
What is a Linux Distribution
• Collection of software prepared by acompany or organization (e.g. Red Hat)
• Central elements of Linux (kernel,standard programs) are almost the samebetween distributions
• A distribution (collection of programs) willusually have a specific purpose in mind:
– Desktop computer
– Network router/firewall
7/29/2019 Linux Makefile
http://slidepdf.com/reader/full/linux-makefile 6/35
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 notrace of Linux
7/29/2019 Linux Makefile
http://slidepdf.com/reader/full/linux-makefile 7/35
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
7/29/2019 Linux Makefile
http://slidepdf.com/reader/full/linux-makefile 8/35
How to acquire Linux (3)
• A middle-ground solution = CYGWIN
• Installs within Windows, and allows you to
use a Linux shell and many Linux utilitiesincluding the compilers from withinWindows
• Not really Linux, but a collection of Linuxprograms built to work under Windows – very useful!
7/29/2019 Linux Makefile
http://slidepdf.com/reader/full/linux-makefile 9/35
Now for some examples…
• Examples of why Linux is useful for
Electrical and Computer Engineeringstudents
7/29/2019 Linux Makefile
http://slidepdf.com/reader/full/linux-makefile 10/35
C Compiler
• Free, and installed by default
• One of the oldest and most stable
compilers• Very powerful, with multitude of options
• Called: gcc
7/29/2019 Linux Makefile
http://slidepdf.com/reader/full/linux-makefile 11/35
Compiling programs
• At first glance, harder to use thanconventional graphical compilers
• There are graphical frontends, but theeasiest way to get started is with theconventional command line
• Later we will explore Makefiles, whichgreatly simplify the compilation of programs
7/29/2019 Linux Makefile
http://slidepdf.com/reader/full/linux-makefile 12/35
Compiling programs (2)
• gcc codeFile1.c -o outputFileName
• Run the compiled program by typing:
./outputFileName
- outputFileName is the “executable” program
7/29/2019 Linux Makefile
http://slidepdf.com/reader/full/linux-makefile 13/35
Compiling programs (3)
• To compile multiple files into one project:
gcc codeFile1.c codeFile2.c -o outputFileName
- Adding multiple filenames is similar to addingfiles to a “Project” in a graphical developmentenvironment
7/29/2019 Linux Makefile
http://slidepdf.com/reader/full/linux-makefile 14/35
Compiling programs (4)
• Some useful options:
• Debug symbols: -g
gcc –g codeFile1.c -o outputFileName
– Allows program to be debugged with adebugger such as gdb
• All warnings: -Wallgcc –Wall codeFile1.c -o outputFileName
– Provides extra warnings, useful for trackingdown some bugs
7/29/2019 Linux Makefile
http://slidepdf.com/reader/full/linux-makefile 15/35
Editors
• Linux has many powerful text editors,loved by programmers around the world – Graphical editors are available
– Most powerful are VIM and EMACS (non-graphical)• 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, unbelievablypowerful
• Huge debates on which is better, but if you decideto learn one, VIM is generally considered best for
programming
7/29/2019 Linux Makefile
http://slidepdf.com/reader/full/linux-makefile 16/35
Other advantages for power users
• Most existing programming languages areavailable under Linux
• Many very powerful scripting languagesavailable such as TCL/TK
• Utilities make tasks that would be hard inother operating systems very easy
• Command line shells very powerful at thehands of an experienced user
7/29/2019 Linux Makefile
http://slidepdf.com/reader/full/linux-makefile 17/35
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 forgetting started with Linux – many of thefree tutorials are quite good
7/29/2019 Linux Makefile
http://slidepdf.com/reader/full/linux-makefile 18/35
Makefiles
• A Makefile is the configuration file used bya standard program called “Make”
• Make is like a project manager in agraphical development environment, butincludes many extra features
• Allows an entire project to be intelligently built with one command on the commandline
7/29/2019 Linux Makefile
http://slidepdf.com/reader/full/linux-makefile 19/35
Makefiles (2)
• What do we mean by “intelligent” buildingof a program?
– For small programs, we don’t care aboutcompilation time – compiling and building anassignment doesn’t take long
– When building a large project, such as an
operating system, the Make utility helps byrecompiling only the files that have changedsince the last compilation
7/29/2019 Linux Makefile
http://slidepdf.com/reader/full/linux-makefile 20/35
Dependencies
• Sometimes one file depends on anotherfile
– e.g. a C file depends on its header files
– If a header file changes, the C files that
#include that header file should berecompiled to take into account the changesto the header
7/29/2019 Linux Makefile
http://slidepdf.com/reader/full/linux-makefile 21/35
Dependencies (2)
Final executable file(MyProject.exe)
main.o interface.o
main.c interface.h interface.c
7/29/2019 Linux Makefile
http://slidepdf.com/reader/full/linux-makefile 22/35
Dependencies (3)
Final executable file(MyProject.exe)
main.o interface.o
main.c interface.h interface.c
7/29/2019 Linux Makefile
http://slidepdf.com/reader/full/linux-makefile 23/35
Dependencies (4)
Final executable file(MyProject.exe)
main.o interface.o
main.c interface.h interface.c
7/29/2019 Linux Makefile
http://slidepdf.com/reader/full/linux-makefile 24/35
A Simple Makefile “Rule”
hello: hello.c
gcc hello.c –o hello
• Save this text as name “Makefile” in thesame directory as the source code
• To build the project, type “make”
• Result is an executable named hello
Dependency line – hellodepends on hello.c
Command to turn hello.c into hello
7/29/2019 Linux Makefile
http://slidepdf.com/reader/full/linux-makefile 25/35
A Simple Makefile (2)
hello: hello.c
gcc hello.c –o hello
• If hello file exists, and the file creationtime 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 thehello executable was created
7/29/2019 Linux Makefile
http://slidepdf.com/reader/full/linux-makefile 26/35
Generic form of a rule
target: prerequisite1 prerequisite2 …
command to make target
– Target is the output file
– Prerequisites are the files that are needed bytarget (and that can cause target to be
recompiled if they change) – Command is the actual command to turn the
prerequisites into the target
7/29/2019 Linux Makefile
http://slidepdf.com/reader/full/linux-makefile 27/35
Multiple TargetsFinal executable file
(MyProject.exe)
main.o interface.o
main.c 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
7/29/2019 Linux Makefile
http://slidepdf.com/reader/full/linux-makefile 28/35
Multiple Targets (2)
MyProject: main.o interface.o
gcc main.o interface.o –o MyProject
main.o: main.c interface.hgcc –c main.c –o main.o
interface.o: interface.c interface.h
gcc –c interface.c –o interface.o
• How does make know which target is theprimary one?
– First target listed in the file is the master
7/29/2019 Linux Makefile
http://slidepdf.com/reader/full/linux-makefile 29/35
Multiple Targets (3)
MyProject: main.o interface.o
gcc main.o interface.o –o MyProject
main.o: main.c interface.hgcc –c main.c –o main.o
interface.o: interface.c interface.h
gcc –c interface.c –o interface.o
• Can make non-master targets by typing,for example:
make main.o
7/29/2019 Linux Makefile
http://slidepdf.com/reader/full/linux-makefile 30/35
Multiple Targets (4)
MyProject: main.o interface.o
gcc main.o interface.o –o MyProjectmain.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
• If interface.h is changed and saved, Makewill discern that both main.o andinterface.o need to be recompiled,followed by a build of MyProject from thenew main.o and interface.o
7/29/2019 Linux Makefile
http://slidepdf.com/reader/full/linux-makefile 31/35
The “clean” target
• Used to remove object files that take up harddisk space
• In the Makefile:
clean:
rm –rf ./*.o
To run make with this target, type:
make clean
7/29/2019 Linux Makefile
http://slidepdf.com/reader/full/linux-makefile 32/35
Variables
• In a large makefile, good idea to usevariables to make later changes easy
– For example, rather than typing “gcc” in thecommand part of every rule, create a variableat the top of the Makefile:
COMPILER = gcc
• Commands can then say:
${COMPILER} sourceFile.c –o executableFile
7/29/2019 Linux Makefile
http://slidepdf.com/reader/full/linux-makefile 33/35
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
7/29/2019 Linux Makefile
http://slidepdf.com/reader/full/linux-makefile 34/35
Only the beginning…
• We won’t cover more advanced topics, butmakefiles can do much more, such as:
– Automatically figure out what a file’sdependencies are (in cooperation with gcc)
– Have targets to install the program, and to doother non-compilation tasks
– Configure a program’s compilation based onthe libraries currently installed on the system
– And much more…
7/29/2019 Linux Makefile
http://slidepdf.com/reader/full/linux-makefile 35/35
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 O’Reilly named:
– GNU Make
• If you have Linux installed, there are manyexamples on your system since almost everyLinux program has a Makefile