SlideShare a Scribd company logo
Make Tutorial
Le Yan
User Services
High Performance Computing @ LSU/LONI
2/14/2012
LONI Fortran Programming Workshop, LSU
Feb 13-16, 2012
Outline
• What is make
• How to use make
– How to write a makefile
– How to use the “make” command
2/14/2012
LONI Fortran Programming Workshop, LSU
Feb 13-16, 2012
What is Make
• A tool that
– Controls the generation of executable and other non-source files (libraries
etc.)
– Simplifies (a lot) the management of a program that has multiple source files
• Have many variants
– GNU make (we will focus on it today)
– BSD make
– …
• Other utilities that do similar things
– Cmake
– Zmake
– …
2/14/2012
LONI Fortran Programming Workshop, LSU
Feb 13-16, 2012
What is Make
• A tool that
– Controls the generation of executable and other non-source files (libraries
etc.)
– Simplifies (a lot) the management of a program that has multiple source files
• Have many variants
– GNU make (we will focus on it today)
– BSD make
– …
• Other utilities that do similar things
– Cmake
– Zmake
– …
2/14/2012
LONI Fortran Programming Workshop, LSU
Feb 13-16, 2012
Why having multiple source files
• It is very important to keep different modules
of functionalities in different source files,
especially for a large program
– Easier to edit and understand
– Easier version control
– Easier to share code with others
– Allow to write a program with different languages
2/14/2012
LONI Fortran Programming Workshop, LSU
Feb 13-16, 2012
From source files to executable
• Two-step process
– The compiler generates the object files from the
source files
– The linker generates the executable from the
object files
• Most compilers do both steps by default
– Use “-c” to suppress linking
2/14/2012
LONI Fortran Programming Workshop, LSU
Feb 13-16, 2012
Compiling multiple source files
• Compiling single source file is straightforward
– <compiler> <flags> <source file>
• Compiling multiple source files
– Need to analyze file dependencies to decide the
order of compilation
– Can be done with one command as well
• <compiler> <flags> <source file 1> <source file 2>…
2/14/2012
LONI Fortran Programming Workshop, LSU
Feb 13-16, 2012
A “Hello world” example (1)
2/14/2012
LONI Fortran Programming Workshop, LSU Feb
13-16, 2012
Source file Purpose
Common.f90 Declares a character variable to store the message
Hello.f90 Prints the message to screen
Adjust.f90 Modifies the message and prints it to screen
Main.f90 Calls functions in hello.f90 and adjust.f90
main.f90 adjust.f90 hello.f90
Common.mod
Common.f90
main.o adjust.o hello.o
a.out
A “Hello world” example (2)
2/14/2012
LONI Fortran Programming Workshop, LSU Feb
13-16, 2012
[lyan1@eric2 make]$ ls
adjust.f90 common.f90 hello.f90 main.f90
[lyan1@eric2 make]$ ifort common.f90 hello.f90
adjust.f90 main.f90
[lyan1@eric2 make]$ ./a.out
Hello, world!
Hello, world!
main.f90 adjust.f90 hello.f90
Common.mod
Common.f90
main.o adjust.o hello.o
a.out
Command line compilation
• Command line compilation works, but it is
– Cumbersome
• Does not work very well when one has a source tree with
many source files in many sub-directories
– Not flexible
• What if different source files need to be compiled using
different flags?
• Use Make instead!
2/14/2012
LONI Fortran Programming Workshop, LSU
Feb 13-16, 2012
How Make works
• Two parts
– The Makefile
• A text file that describes the dependency
– The “make” command
• Compile the program using the dependency provided
by the Makefile
2/14/2012
LONI Fortran Programming Workshop, LSU
Feb 13-16, 2012
[lyan1@eric2 make]$ ls
adjust.f90 common.f90 hello.f90 main.f90
Makefile
[lyan1@eric2 make]$ make
ifort common.f90 hello.f90 adjust.f90 main.f90
[lyan1@eric2 make]$ ls
adjust.f90 a.out common.f90 common.mod hello.f90
main.f90 Makefile
A Makefile with only one rule
2/14/2012
LONI Fortran Programming Workshop, LSU Feb
13-16, 2012
[lyan1@eric2 make]$ cat Makefile
all:
ifort common.f90 hello.f90 adjust.f90 main.f90
Target Action: shell commands that will be executed
Explicit rule
A mandatory tab
Exercise 1
• Copy all files under
/home/lyan1/traininglab/make to your own
user space
• Check the Makefile and use it to build the
executable
2/14/2012
LONI Fortran Programming Workshop, LSU
Feb 13-16, 2012
Makefile components
• Explicit rules
– Purpose: create a target or re-create a target when
any of prerequisites changes
– Syntax:
• Implicit rules
• Variable definition
• Directives
2/14/2012
LONI Fortran Programming Workshop, LSU
Feb 13-16, 2012
target: prerequisites
(tab) action
Explicit rules (1)
• Multiple rules can exist in the same Makefile
– The “make” command builds the first target by default
– To build other targets, one needs to specify the target name
• make <target name>
• A single rule can have multiple targets separated by space
• An action (or recipe) can consist of multiple commands
– They can be on multiple lines, or on the same line separated by
semicolons
– Wildcards can be used
– By default all executed commands will be printed to screen
• Can be suppressed by adding “@” before the commands
2/14/2012
LONI Fortran Programming Workshop, LSU
Feb 13-16, 2012
Explicit rules (2)
• How file dependencies are handled
– Targets and prerequisites are often file names
– A target is considered out-of-date if
• It does not exist, or
• It is older than any of the prerequisites
2/14/2012
LONI Fortran Programming Workshop, LSU
Feb 13-16, 2012
A Makefile with many rules
2/14/2012
LONI Fortran Programming Workshop, LSU Feb
13-16, 2012
main.f90 adjust.f90 hello.f90
Common.mod
Common.f90
main.o adjust.o hello.o
a.out
all: main.o adjust.o hello.o
ifort main.o adjust.o hello.o
main.o: main.f90
ifort –c main.f90
adjust.o: adjust.f90 common.mod
ifort –c adjust.f90
hello.o: hello.f90 common.mod
ifort –c hello.f90
common.mod: common.f90
ifort –c common.f90
Exercise 2
• Write a Makefile using the template provided on
the previous slide and “make”
• Run “make” again and see what happens
• Modify the message (common.f90) and “make”
again
• Add a new rule “clean” which deletes all but the
source and makefiles (the executable, object files
and common.mod), and try “make clean”
2/14/2012
LONI Fortran Programming Workshop, LSU
Feb 13-16, 2012
Variables in Makefile (1)
• These kinds of
duplication are
error-prone
• One can solve
this problem by
using variables
2/14/2012
LONI Fortran Programming Workshop, LSU
Feb 13-16, 2012
all: main.o adjust.o hello.o
ifort main.o adjust.o hello.o
main.o: main.f90
ifort –c main.f90
adjust.o: adjust.f90 common.mod
ifort –c adjust.f90
hello.o: hello.f90 common.mod
ifort –c hello.f90
common.mod: common.f90
ifort –c common.f90
Variables in Makefile (2)
• Similar to shell variables
– Define once as a string and reuse later
2/14/2012
LONI Fortran Programming Workshop, LSU
Feb 13-16, 2012
all: main.o adjust.o hello.o
ifort main.o adjust.o hello.o
main.o: main.f90
ifort –c main.f90
FC=ifort
OBJ=main.o adjust.o hello.o
all: $(OBJ)
$(FC) $(OBJ)
main.o: main.f90
$(FC) –c main.f90
Without variables
With variables
Automatic variables
• The values of automatic variables change every time a
rule is executed
• Automatic variables only have values within a rule
• Most frequently used ones
– $@: The name of the current target
– $^: The names of all the prerequisites
– $?: The names of all the prerequisites that are newer than
the target
– $<: The name of the first prerequisite
2/14/2012
LONI Fortran Programming Workshop, LSU
Feb 13-16, 2012
Implicit rules (1)
• Tells Make system how to build a certain type of targets
– GNU make has a few built-in implicit rules
• Syntax is similar to an ordinary rule, except that “%” is used
in the target
– “%” stands for the same thing in the prerequisites as it does in
the target
– There can also be unvarying prerequisites
– Automatic variables can be used here as well
2/14/2012
LONI Fortran Programming Workshop, LSU
Feb 13-16, 2012
%.o: %.c
(tab) action
Implicit rules (2)
• In this example, any .o target has a corresponding .c file
as an implied prerequisite
• If a target needs additional prerequisites, write a action-
less rule with those prerequisites
2/14/2012
LONI Fortran Programming Workshop, LSU
Feb 13-16, 2012
CC=icc
CFLAGS=-O3
%.o : %.c
@$(CC) $(CFLAGS) -c –o $@ $<
data.o: data.h
Exercise 3
• Rewrite the Makefile from Exercise 2
– Define an implicit rule so that no more than 3
explicit rules are necessary (excluding “clean”)
– Use variables so that no file name appears in the
action section of any rule
2/14/2012
LONI Fortran Programming Workshop, LSU
Feb 13-16, 2012
Directives
• Make directives are similar to the C preprocessor
directives
– E.g. include, define, conditionals
• Include directive
– Read the contents of other Makefiles before
proceeding within the current one
– Often used to read
• Top level and common definitions when there are multiple
sub-directories and makefiles
2/14/2012
LONI Fortran Programming Workshop, LSU
Feb 13-16, 2012
Command line options of make (1)
• -f <file name>
– Specify the name of the file to be used as the makefile
– Default is GNUmakefile, makefile and Makefile (in that
order)
– Multiple makefiles may be useful for compilation on
multiple platforms
• -s
– Turn on silent mode (as if all commands start with an “@”)
2/14/2012
LONI Fortran Programming Workshop, LSU
Feb 13-16, 2012
Command line options of make (2)
• -j <number of jobs>
– Build multiple targets in parallel
• -i
– Ignore all errors
– A warning message will be printed out for each error
• -k
– Continue as much as possible after an error.
2/14/2012
LONI Fortran Programming Workshop, LSU
Feb 13-16, 2012
Exercise 4
• Take a look at a real life makefile
– /home/lyan1/traininglab/valgrind/Makefile
– Makefile for a memory profiler Valgrind
2/14/2012
LONI Fortran Programming Workshop, LSU
Feb 13-16, 2012
Questions?
2/14/2012
LONI Fortran Programming Workshop, LSU Feb
13-16, 2012

More Related Content

Similar to LONI_MakeTutorial_Spring2012.pdf

se2030-11 Intro to version control(1).ppt
se2030-11 Intro to version control(1).pptse2030-11 Intro to version control(1).ppt
se2030-11 Intro to version control(1).ppt
Ezratgab
 
Introduction tojavaandxml lc-slides01-fp2005-ver 1.0
Introduction tojavaandxml lc-slides01-fp2005-ver 1.0Introduction tojavaandxml lc-slides01-fp2005-ver 1.0
Introduction tojavaandxml lc-slides01-fp2005-ver 1.0
Sanjay Yadav
 
Linux training
Linux trainingLinux training
Linux training
Parker Fong
 
Squeezing more out of OpenOffice.org
Squeezing more out of OpenOffice.orgSqueezing more out of OpenOffice.org
Squeezing more out of OpenOffice.org
Alexandro Colorado
 
Happy porting x86 application to android
Happy porting x86 application to androidHappy porting x86 application to android
Happy porting x86 application to android
Owen Hsu
 
How to write shared libraries!
How to write shared libraries!How to write shared libraries!
How to write shared libraries!
Stanley Ho
 
Hadoop
HadoopHadoop
Hadoop
Raghu Juluri
 
510Lec01-Overview.pptx
510Lec01-Overview.pptx510Lec01-Overview.pptx
510Lec01-Overview.pptx
KrosumLabs1
 
Surviving OS X as a Windows Admin
Surviving OS X as a Windows AdminSurviving OS X as a Windows Admin
Surviving OS X as a Windows Admin
Dell World
 
Robocopy
RobocopyRobocopy
Robocopy
Francisco Rios
 
Basic Linux day 1
Basic Linux day 1Basic Linux day 1
Basic Linux day 1
Saikumar Daram
 
Ekon24 mORMot 2
Ekon24 mORMot 2Ekon24 mORMot 2
Ekon24 mORMot 2
Arnaud Bouchez
 
Embedded Systems: Lecture 13: Introduction to GNU Toolchain (Build Tools)
Embedded Systems: Lecture 13: Introduction to GNU Toolchain (Build Tools)Embedded Systems: Lecture 13: Introduction to GNU Toolchain (Build Tools)
Embedded Systems: Lecture 13: Introduction to GNU Toolchain (Build Tools)
Ahmed El-Arabawy
 
101 4.6 create and change hard and symbolic links v2
101 4.6 create and change hard and symbolic links v2101 4.6 create and change hard and symbolic links v2
101 4.6 create and change hard and symbolic links v2
Acácio Oliveira
 
Programming the Semantic Web
Programming the Semantic WebProgramming the Semantic Web
Programming the Semantic Web
Luigi De Russis
 
Introduction to linux at Introductory Bioinformatics Workshop
Introduction to linux at Introductory Bioinformatics WorkshopIntroduction to linux at Introductory Bioinformatics Workshop
Introduction to linux at Introductory Bioinformatics Workshop
Setor Amuzu
 
4.6 create and change hard and symbolic links v2
4.6 create and change hard and symbolic links v24.6 create and change hard and symbolic links v2
4.6 create and change hard and symbolic links v2
Acácio Oliveira
 
The Beauty and the Beast
The Beauty and the BeastThe Beauty and the Beast
The Beauty and the Beast
Bastian Feder
 
101 2.3 manage shared libraries
101 2.3 manage shared libraries101 2.3 manage shared libraries
101 2.3 manage shared libraries
Acácio Oliveira
 
C- language Lecture 8
C- language Lecture 8C- language Lecture 8
C- language Lecture 8
Hatem Abd El-Salam
 

Similar to LONI_MakeTutorial_Spring2012.pdf (20)

se2030-11 Intro to version control(1).ppt
se2030-11 Intro to version control(1).pptse2030-11 Intro to version control(1).ppt
se2030-11 Intro to version control(1).ppt
 
Introduction tojavaandxml lc-slides01-fp2005-ver 1.0
Introduction tojavaandxml lc-slides01-fp2005-ver 1.0Introduction tojavaandxml lc-slides01-fp2005-ver 1.0
Introduction tojavaandxml lc-slides01-fp2005-ver 1.0
 
Linux training
Linux trainingLinux training
Linux training
 
Squeezing more out of OpenOffice.org
Squeezing more out of OpenOffice.orgSqueezing more out of OpenOffice.org
Squeezing more out of OpenOffice.org
 
Happy porting x86 application to android
Happy porting x86 application to androidHappy porting x86 application to android
Happy porting x86 application to android
 
How to write shared libraries!
How to write shared libraries!How to write shared libraries!
How to write shared libraries!
 
Hadoop
HadoopHadoop
Hadoop
 
510Lec01-Overview.pptx
510Lec01-Overview.pptx510Lec01-Overview.pptx
510Lec01-Overview.pptx
 
Surviving OS X as a Windows Admin
Surviving OS X as a Windows AdminSurviving OS X as a Windows Admin
Surviving OS X as a Windows Admin
 
Robocopy
RobocopyRobocopy
Robocopy
 
Basic Linux day 1
Basic Linux day 1Basic Linux day 1
Basic Linux day 1
 
Ekon24 mORMot 2
Ekon24 mORMot 2Ekon24 mORMot 2
Ekon24 mORMot 2
 
Embedded Systems: Lecture 13: Introduction to GNU Toolchain (Build Tools)
Embedded Systems: Lecture 13: Introduction to GNU Toolchain (Build Tools)Embedded Systems: Lecture 13: Introduction to GNU Toolchain (Build Tools)
Embedded Systems: Lecture 13: Introduction to GNU Toolchain (Build Tools)
 
101 4.6 create and change hard and symbolic links v2
101 4.6 create and change hard and symbolic links v2101 4.6 create and change hard and symbolic links v2
101 4.6 create and change hard and symbolic links v2
 
Programming the Semantic Web
Programming the Semantic WebProgramming the Semantic Web
Programming the Semantic Web
 
Introduction to linux at Introductory Bioinformatics Workshop
Introduction to linux at Introductory Bioinformatics WorkshopIntroduction to linux at Introductory Bioinformatics Workshop
Introduction to linux at Introductory Bioinformatics Workshop
 
4.6 create and change hard and symbolic links v2
4.6 create and change hard and symbolic links v24.6 create and change hard and symbolic links v2
4.6 create and change hard and symbolic links v2
 
The Beauty and the Beast
The Beauty and the BeastThe Beauty and the Beast
The Beauty and the Beast
 
101 2.3 manage shared libraries
101 2.3 manage shared libraries101 2.3 manage shared libraries
101 2.3 manage shared libraries
 
C- language Lecture 8
C- language Lecture 8C- language Lecture 8
C- language Lecture 8
 

Recently uploaded

The Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptxThe Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptx
operationspcvita
 
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
Edge AI and Vision Alliance
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Safe Software
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
saastr
 
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
Jason Yip
 
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-EfficiencyFreshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
ScyllaDB
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
saastr
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
DanBrown980551
 
What is an RPA CoE? Session 1 – CoE Vision
What is an RPA CoE?  Session 1 – CoE VisionWhat is an RPA CoE?  Session 1 – CoE Vision
What is an RPA CoE? Session 1 – CoE Vision
DianaGray10
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
ssuserfac0301
 
JavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green MasterplanJavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green Masterplan
Miro Wengner
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
Zilliz
 
AppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSFAppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSF
Ajin Abraham
 
Y-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PPY-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PP
c5vrf27qcz
 
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving
 
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge GraphGraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
Neo4j
 
Digital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Digital Banking in the Cloud: How Citizens Bank Unlocked Their MainframeDigital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Digital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Precisely
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
Jason Packer
 

Recently uploaded (20)

The Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptxThe Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptx
 
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
 
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
 
Artificial Intelligence and Electronic Warfare
Artificial Intelligence and Electronic WarfareArtificial Intelligence and Electronic Warfare
Artificial Intelligence and Electronic Warfare
 
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-EfficiencyFreshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
 
What is an RPA CoE? Session 1 – CoE Vision
What is an RPA CoE?  Session 1 – CoE VisionWhat is an RPA CoE?  Session 1 – CoE Vision
What is an RPA CoE? Session 1 – CoE Vision
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
 
JavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green MasterplanJavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green Masterplan
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
 
AppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSFAppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSF
 
Y-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PPY-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PP
 
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024
 
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge GraphGraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
 
Digital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Digital Banking in the Cloud: How Citizens Bank Unlocked Their MainframeDigital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Digital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
 

LONI_MakeTutorial_Spring2012.pdf

  • 1. Make Tutorial Le Yan User Services High Performance Computing @ LSU/LONI 2/14/2012 LONI Fortran Programming Workshop, LSU Feb 13-16, 2012
  • 2. Outline • What is make • How to use make – How to write a makefile – How to use the “make” command 2/14/2012 LONI Fortran Programming Workshop, LSU Feb 13-16, 2012
  • 3. What is Make • A tool that – Controls the generation of executable and other non-source files (libraries etc.) – Simplifies (a lot) the management of a program that has multiple source files • Have many variants – GNU make (we will focus on it today) – BSD make – … • Other utilities that do similar things – Cmake – Zmake – … 2/14/2012 LONI Fortran Programming Workshop, LSU Feb 13-16, 2012
  • 4. What is Make • A tool that – Controls the generation of executable and other non-source files (libraries etc.) – Simplifies (a lot) the management of a program that has multiple source files • Have many variants – GNU make (we will focus on it today) – BSD make – … • Other utilities that do similar things – Cmake – Zmake – … 2/14/2012 LONI Fortran Programming Workshop, LSU Feb 13-16, 2012
  • 5. Why having multiple source files • It is very important to keep different modules of functionalities in different source files, especially for a large program – Easier to edit and understand – Easier version control – Easier to share code with others – Allow to write a program with different languages 2/14/2012 LONI Fortran Programming Workshop, LSU Feb 13-16, 2012
  • 6. From source files to executable • Two-step process – The compiler generates the object files from the source files – The linker generates the executable from the object files • Most compilers do both steps by default – Use “-c” to suppress linking 2/14/2012 LONI Fortran Programming Workshop, LSU Feb 13-16, 2012
  • 7. Compiling multiple source files • Compiling single source file is straightforward – <compiler> <flags> <source file> • Compiling multiple source files – Need to analyze file dependencies to decide the order of compilation – Can be done with one command as well • <compiler> <flags> <source file 1> <source file 2>… 2/14/2012 LONI Fortran Programming Workshop, LSU Feb 13-16, 2012
  • 8. A “Hello world” example (1) 2/14/2012 LONI Fortran Programming Workshop, LSU Feb 13-16, 2012 Source file Purpose Common.f90 Declares a character variable to store the message Hello.f90 Prints the message to screen Adjust.f90 Modifies the message and prints it to screen Main.f90 Calls functions in hello.f90 and adjust.f90 main.f90 adjust.f90 hello.f90 Common.mod Common.f90 main.o adjust.o hello.o a.out
  • 9. A “Hello world” example (2) 2/14/2012 LONI Fortran Programming Workshop, LSU Feb 13-16, 2012 [lyan1@eric2 make]$ ls adjust.f90 common.f90 hello.f90 main.f90 [lyan1@eric2 make]$ ifort common.f90 hello.f90 adjust.f90 main.f90 [lyan1@eric2 make]$ ./a.out Hello, world! Hello, world! main.f90 adjust.f90 hello.f90 Common.mod Common.f90 main.o adjust.o hello.o a.out
  • 10. Command line compilation • Command line compilation works, but it is – Cumbersome • Does not work very well when one has a source tree with many source files in many sub-directories – Not flexible • What if different source files need to be compiled using different flags? • Use Make instead! 2/14/2012 LONI Fortran Programming Workshop, LSU Feb 13-16, 2012
  • 11. How Make works • Two parts – The Makefile • A text file that describes the dependency – The “make” command • Compile the program using the dependency provided by the Makefile 2/14/2012 LONI Fortran Programming Workshop, LSU Feb 13-16, 2012 [lyan1@eric2 make]$ ls adjust.f90 common.f90 hello.f90 main.f90 Makefile [lyan1@eric2 make]$ make ifort common.f90 hello.f90 adjust.f90 main.f90 [lyan1@eric2 make]$ ls adjust.f90 a.out common.f90 common.mod hello.f90 main.f90 Makefile
  • 12. A Makefile with only one rule 2/14/2012 LONI Fortran Programming Workshop, LSU Feb 13-16, 2012 [lyan1@eric2 make]$ cat Makefile all: ifort common.f90 hello.f90 adjust.f90 main.f90 Target Action: shell commands that will be executed Explicit rule A mandatory tab
  • 13. Exercise 1 • Copy all files under /home/lyan1/traininglab/make to your own user space • Check the Makefile and use it to build the executable 2/14/2012 LONI Fortran Programming Workshop, LSU Feb 13-16, 2012
  • 14. Makefile components • Explicit rules – Purpose: create a target or re-create a target when any of prerequisites changes – Syntax: • Implicit rules • Variable definition • Directives 2/14/2012 LONI Fortran Programming Workshop, LSU Feb 13-16, 2012 target: prerequisites (tab) action
  • 15. Explicit rules (1) • Multiple rules can exist in the same Makefile – The “make” command builds the first target by default – To build other targets, one needs to specify the target name • make <target name> • A single rule can have multiple targets separated by space • An action (or recipe) can consist of multiple commands – They can be on multiple lines, or on the same line separated by semicolons – Wildcards can be used – By default all executed commands will be printed to screen • Can be suppressed by adding “@” before the commands 2/14/2012 LONI Fortran Programming Workshop, LSU Feb 13-16, 2012
  • 16. Explicit rules (2) • How file dependencies are handled – Targets and prerequisites are often file names – A target is considered out-of-date if • It does not exist, or • It is older than any of the prerequisites 2/14/2012 LONI Fortran Programming Workshop, LSU Feb 13-16, 2012
  • 17. A Makefile with many rules 2/14/2012 LONI Fortran Programming Workshop, LSU Feb 13-16, 2012 main.f90 adjust.f90 hello.f90 Common.mod Common.f90 main.o adjust.o hello.o a.out all: main.o adjust.o hello.o ifort main.o adjust.o hello.o main.o: main.f90 ifort –c main.f90 adjust.o: adjust.f90 common.mod ifort –c adjust.f90 hello.o: hello.f90 common.mod ifort –c hello.f90 common.mod: common.f90 ifort –c common.f90
  • 18. Exercise 2 • Write a Makefile using the template provided on the previous slide and “make” • Run “make” again and see what happens • Modify the message (common.f90) and “make” again • Add a new rule “clean” which deletes all but the source and makefiles (the executable, object files and common.mod), and try “make clean” 2/14/2012 LONI Fortran Programming Workshop, LSU Feb 13-16, 2012
  • 19. Variables in Makefile (1) • These kinds of duplication are error-prone • One can solve this problem by using variables 2/14/2012 LONI Fortran Programming Workshop, LSU Feb 13-16, 2012 all: main.o adjust.o hello.o ifort main.o adjust.o hello.o main.o: main.f90 ifort –c main.f90 adjust.o: adjust.f90 common.mod ifort –c adjust.f90 hello.o: hello.f90 common.mod ifort –c hello.f90 common.mod: common.f90 ifort –c common.f90
  • 20. Variables in Makefile (2) • Similar to shell variables – Define once as a string and reuse later 2/14/2012 LONI Fortran Programming Workshop, LSU Feb 13-16, 2012 all: main.o adjust.o hello.o ifort main.o adjust.o hello.o main.o: main.f90 ifort –c main.f90 FC=ifort OBJ=main.o adjust.o hello.o all: $(OBJ) $(FC) $(OBJ) main.o: main.f90 $(FC) –c main.f90 Without variables With variables
  • 21. Automatic variables • The values of automatic variables change every time a rule is executed • Automatic variables only have values within a rule • Most frequently used ones – $@: The name of the current target – $^: The names of all the prerequisites – $?: The names of all the prerequisites that are newer than the target – $<: The name of the first prerequisite 2/14/2012 LONI Fortran Programming Workshop, LSU Feb 13-16, 2012
  • 22. Implicit rules (1) • Tells Make system how to build a certain type of targets – GNU make has a few built-in implicit rules • Syntax is similar to an ordinary rule, except that “%” is used in the target – “%” stands for the same thing in the prerequisites as it does in the target – There can also be unvarying prerequisites – Automatic variables can be used here as well 2/14/2012 LONI Fortran Programming Workshop, LSU Feb 13-16, 2012 %.o: %.c (tab) action
  • 23. Implicit rules (2) • In this example, any .o target has a corresponding .c file as an implied prerequisite • If a target needs additional prerequisites, write a action- less rule with those prerequisites 2/14/2012 LONI Fortran Programming Workshop, LSU Feb 13-16, 2012 CC=icc CFLAGS=-O3 %.o : %.c @$(CC) $(CFLAGS) -c –o $@ $< data.o: data.h
  • 24. Exercise 3 • Rewrite the Makefile from Exercise 2 – Define an implicit rule so that no more than 3 explicit rules are necessary (excluding “clean”) – Use variables so that no file name appears in the action section of any rule 2/14/2012 LONI Fortran Programming Workshop, LSU Feb 13-16, 2012
  • 25. Directives • Make directives are similar to the C preprocessor directives – E.g. include, define, conditionals • Include directive – Read the contents of other Makefiles before proceeding within the current one – Often used to read • Top level and common definitions when there are multiple sub-directories and makefiles 2/14/2012 LONI Fortran Programming Workshop, LSU Feb 13-16, 2012
  • 26. Command line options of make (1) • -f <file name> – Specify the name of the file to be used as the makefile – Default is GNUmakefile, makefile and Makefile (in that order) – Multiple makefiles may be useful for compilation on multiple platforms • -s – Turn on silent mode (as if all commands start with an “@”) 2/14/2012 LONI Fortran Programming Workshop, LSU Feb 13-16, 2012
  • 27. Command line options of make (2) • -j <number of jobs> – Build multiple targets in parallel • -i – Ignore all errors – A warning message will be printed out for each error • -k – Continue as much as possible after an error. 2/14/2012 LONI Fortran Programming Workshop, LSU Feb 13-16, 2012
  • 28. Exercise 4 • Take a look at a real life makefile – /home/lyan1/traininglab/valgrind/Makefile – Makefile for a memory profiler Valgrind 2/14/2012 LONI Fortran Programming Workshop, LSU Feb 13-16, 2012
  • 29. Questions? 2/14/2012 LONI Fortran Programming Workshop, LSU Feb 13-16, 2012