ݺߣ

ݺߣShare a Scribd company logo
Introduction to Makefile Waqqas Jabbar
Compiling with gcc Compilation and linking in a single step gcc file1.c file2.c -o exe Compilation and linking in separate steps gcc -c file1.c gcc -c file2.c gcc file1.o file2.o -o exe
make command Makefile is the input to make command. Automate rebuilding software by determining the minimum set of commands that need to be called to do this Install program files Clean up object files Reads a file named Makefile in the directory from which it is executed
Makefile syntax Rules Target: Dependencies (tab) Command (tab) Command Target A file that is generated by a program Executable or object files The name of an action to carry out Dependencies Files that are used as inputs to create a Target Space separated list May include other targets
Makefile syntax Command Shell command If no target is specified during execution of make then the first target is used
Make algorithm Build dependencies Determine if dependency is file or action target If file by that name exist -> File Target Otherwise -> Action Target File Target Do nothing Action Target Build target dependencies
Make algorithm Check if at least one of the dependencies is newer than the target Execute Commands in that rule, if newer Definition Newer File Target: File Dependency: Check modification times Action Dependency:Check commands (in dependency rule) is executed or not Action Target:  File Dependency: Always newer  Action Dependency:Check commands (in dependency rule) is executed or not
Makefile variable Define Variable1 = value Usage in Commands $(Variable1) Usage in variables Variable2 = $(Variable1) Macros Variable evaluated when command is executed
Abstract Rules Defines how to build a file `*.s2' from a file `*.s1', where s1 and s2 are suffixes .s1.s2:  (tab) Command (tab) Command
Punctuation $<' are the dependencies that changed causing the target to need to be rebuilt  `$@' is the target  `$^' are all the dependencies for the current rule
include Include other Makefiles Variables can be used
Executing shell command to assign variables $(shell command) SVN_VERSION=$(shell if test -d .svn ; then svnversion . | sed -e 's/\([1-9]*\)\([MS]\)\?:\?\([1-9]*\)[MS]\?/\1/' ;  else echo unknown ; fi) VERINFO=-DSVN_VERSION=&quot;$(SVN_VERSION)&quot; ARCH=$(shell uname -p|sed s/_/' '/|awk '{print $$2}')
Loops in command You can write loops as in shell script @for dir in $(SUBDIRS); do \ $(MAKE) -w -C $$dir $(TARGET) clean || exit 1 ; \ done
Wildcard and patsubst SRCS := $(wildcard *.cc) $(wildcard *.c) $(wildcard *.cpp) OBJS := $(patsubst %.cc, %.o,$(wildcard *.cc)) $(patsubst %.c, %.o,$(wildcard *.c)) $(patsubst %.cpp, %.o,$(wildcard *.cpp))
Environment variable checking ifeq, else,endif ifeq ($(LIB_TYPE), static) LIBEXT = a COMPILE_LIB = $(AR) -ruv $(TOP)/lib/$(LIBNAME).$(LIBEXT) $(OBJS) else LIBEXT = so COMPILE_LIB = $(CC) -fPIC -shared -Wl,-soname,$(LIBNAME).$(LIBEXT) -o $(TOP)/lib/$(LIBNAME).$(LIBEXT) $(OBJS) $(LIBS) endif
Thank you

More Related Content

What's hot (20)

Linux commands
Linux commandsLinux commands
Linux commands
Balakumaran Arunachalam
?
Unix Linux Commands Presentation 2013
Unix Linux Commands Presentation 2013Unix Linux Commands Presentation 2013
Unix Linux Commands Presentation 2013
Wave Digitech
?
Intro to Linux Shell Scripting
Intro to Linux Shell ScriptingIntro to Linux Shell Scripting
Intro to Linux Shell Scripting
vceder
?
Tutoriel GITTutoriel GIT
Tutoriel GIT
Francois ANDRE
?
Tp n 6 linuxTp n 6 linux
Tp n 6 linux
Amir Souissi
?
How A Compiler Works: GNU Toolchain
How A Compiler Works: GNU ToolchainHow A Compiler Works: GNU Toolchain
How A Compiler Works: GNU Toolchain
National Cheng Kung University
?
Linux systems - Linux Commands and Shell Scripting
Linux systems - Linux Commands and Shell ScriptingLinux systems - Linux Commands and Shell Scripting
Linux systems - Linux Commands and Shell Scripting
Emertxe Information Technologies Pvt Ltd
?
Git - Sistema Descentralizado de Controle de Vers?esGit - Sistema Descentralizado de Controle de Vers?es
Git - Sistema Descentralizado de Controle de Vers?es
Leandro Cavalcante
?
Toolchain
ToolchainToolchain
Toolchain
Anil Kumar Pugalia
?
ʮ jservWIϵyOӋ
ʮ  jservWIϵyOӋʮ  jservWIϵyOӋ
ʮ jservWIϵyOӋ
R\ R\
?
CMake - Introduction and best practices
CMake - Introduction and best practicesCMake - Introduction and best practices
CMake - Introduction and best practices
Daniel Pfeifer
?
Shell scripting
Shell scriptingShell scripting
Shell scripting
Manav Prasad
?
Linux command ppt
Linux command pptLinux command ppt
Linux command ppt
kalyanineve
?
Kernel Recipes 2017 - 20 years of Linux Virtual Memory - Andrea Arcangeli
Kernel Recipes 2017 - 20 years of Linux Virtual Memory - Andrea ArcangeliKernel Recipes 2017 - 20 years of Linux Virtual Memory - Andrea Arcangeli
Kernel Recipes 2017 - 20 years of Linux Virtual Memory - Andrea Arcangeli
Anne Nicolas
?
CMake best practices
CMake best practicesCMake best practices
CMake best practices
Henry Schreiner
?
git - eine praktische Einfhrunggit - eine praktische Einfhrung
git - eine praktische Einfhrung
Marcel Eichner
?
UM2019 Extended BPF: A New Type of Software
UM2019 Extended BPF: A New Type of SoftwareUM2019 Extended BPF: A New Type of Software
UM2019 Extended BPF: A New Type of Software
Brendan Gregg
?
Basics of shell programming
Basics of shell programmingBasics of shell programming
Basics of shell programming
Chandan Kumar Rana
?
Basic commands of linux
Basic commands of linuxBasic commands of linux
Basic commands of linux
shravan saini
?
A Practical Introduction to git
A Practical Introduction to gitA Practical Introduction to git
A Practical Introduction to git
Emanuele Olivetti
?
Unix Linux Commands Presentation 2013
Unix Linux Commands Presentation 2013Unix Linux Commands Presentation 2013
Unix Linux Commands Presentation 2013
Wave Digitech
?
Intro to Linux Shell Scripting
Intro to Linux Shell ScriptingIntro to Linux Shell Scripting
Intro to Linux Shell Scripting
vceder
?
Tutoriel GITTutoriel GIT
Tutoriel GIT
Francois ANDRE
?
Tp n 6 linuxTp n 6 linux
Tp n 6 linux
Amir Souissi
?
Git - Sistema Descentralizado de Controle de Vers?esGit - Sistema Descentralizado de Controle de Vers?es
Git - Sistema Descentralizado de Controle de Vers?es
Leandro Cavalcante
?
ʮ jservWIϵyOӋ
ʮ  jservWIϵyOӋʮ  jservWIϵyOӋ
ʮ jservWIϵyOӋ
R\ R\
?
CMake - Introduction and best practices
CMake - Introduction and best practicesCMake - Introduction and best practices
CMake - Introduction and best practices
Daniel Pfeifer
?
Kernel Recipes 2017 - 20 years of Linux Virtual Memory - Andrea Arcangeli
Kernel Recipes 2017 - 20 years of Linux Virtual Memory - Andrea ArcangeliKernel Recipes 2017 - 20 years of Linux Virtual Memory - Andrea Arcangeli
Kernel Recipes 2017 - 20 years of Linux Virtual Memory - Andrea Arcangeli
Anne Nicolas
?
git - eine praktische Einfhrunggit - eine praktische Einfhrung
git - eine praktische Einfhrung
Marcel Eichner
?
UM2019 Extended BPF: A New Type of Software
UM2019 Extended BPF: A New Type of SoftwareUM2019 Extended BPF: A New Type of Software
UM2019 Extended BPF: A New Type of Software
Brendan Gregg
?
Basic commands of linux
Basic commands of linuxBasic commands of linux
Basic commands of linux
shravan saini
?
A Practical Introduction to git
A Practical Introduction to gitA Practical Introduction to git
A Practical Introduction to git
Emanuele Olivetti
?

Similar to Introduction To Makefile (20)

LOSS_C11- Programming Linux 20221006.pdf
LOSS_C11- Programming Linux 20221006.pdfLOSS_C11- Programming Linux 20221006.pdf
LOSS_C11- Programming Linux 20221006.pdf
Thninh2
?
Basic Make
Basic MakeBasic Make
Basic Make
Alec Clews
?
Linux intro 5 extra: makefiles
Linux intro 5 extra: makefilesLinux intro 5 extra: makefiles
Linux intro 5 extra: makefiles
Giovanni Marco Dall'Olio
?
Linux intro 4 awk + makefile
Linux intro 4  awk + makefileLinux intro 4  awk + makefile
Linux intro 4 awk + makefile
Giovanni Marco Dall'Olio
?
Advanced linux chapter ix-shell script
Advanced linux chapter ix-shell scriptAdvanced linux chapter ix-shell script
Advanced linux chapter ix-shell script
Eliezer Moraes
?
Autoconf&Automake
Autoconf&AutomakeAutoconf&Automake
Autoconf&Automake
niurui
?
Course 102: Lecture 12: Basic Text Handling
Course 102: Lecture 12: Basic Text Handling Course 102: Lecture 12: Basic Text Handling
Course 102: Lecture 12: Basic Text Handling
Ahmed El-Arabawy
?
Makefiles Bioinfo
Makefiles BioinfoMakefiles Bioinfo
Makefiles Bioinfo
Giovanni Marco Dall'Olio
?
GradleFX
GradleFXGradleFX
GradleFX
Christophe Herreman
?
Basic shell programs assignment 1_solution_manual
Basic shell programs assignment 1_solution_manualBasic shell programs assignment 1_solution_manual
Basic shell programs assignment 1_solution_manual
Kuntal Bhowmick
?
(2) c sharp introduction_basics_part_i
(2) c sharp introduction_basics_part_i(2) c sharp introduction_basics_part_i
(2) c sharp introduction_basics_part_i
Nico Ludwig
?
(1) cpp introducing the_cpp_programming_language
(1) cpp introducing the_cpp_programming_language(1) cpp introducing the_cpp_programming_language
(1) cpp introducing the_cpp_programming_language
Nico Ludwig
?
Spsl by sasidhar 3 unit
Spsl by sasidhar  3 unitSpsl by sasidhar  3 unit
Spsl by sasidhar 3 unit
Sasidhar Kothuru
?
Course 102: Lecture 8: Composite Commands
Course 102: Lecture 8: Composite Commands Course 102: Lecture 8: Composite Commands
Course 102: Lecture 8: Composite Commands
Ahmed El-Arabawy
?
Basics of shell programming
Basics of shell programmingBasics of shell programming
Basics of shell programming
Chandan Kumar Rana
?
Using Unix
Using UnixUsing Unix
Using Unix
Dr.Ravi
?
(2) c sharp introduction_basics_part_i
(2) c sharp introduction_basics_part_i(2) c sharp introduction_basics_part_i
(2) c sharp introduction_basics_part_i
Nico Ludwig
?
Chip Designer's Code - Linux Terminal Part 3 - File Handling
Chip Designer's Code - Linux Terminal Part 3 - File HandlingChip Designer's Code - Linux Terminal Part 3 - File Handling
Chip Designer's Code - Linux Terminal Part 3 - File Handling
Amr Adel
?
The Galaxy toolshed
The Galaxy toolshedThe Galaxy toolshed
The Galaxy toolshed
Joachim Jacob
?
Lic?o 13 functions
Lic?o 13 functionsLic?o 13 functions
Lic?o 13 functions
Accio Oliveira
?
LOSS_C11- Programming Linux 20221006.pdf
LOSS_C11- Programming Linux 20221006.pdfLOSS_C11- Programming Linux 20221006.pdf
LOSS_C11- Programming Linux 20221006.pdf
Thninh2
?
Advanced linux chapter ix-shell script
Advanced linux chapter ix-shell scriptAdvanced linux chapter ix-shell script
Advanced linux chapter ix-shell script
Eliezer Moraes
?
Autoconf&Automake
Autoconf&AutomakeAutoconf&Automake
Autoconf&Automake
niurui
?
Course 102: Lecture 12: Basic Text Handling
Course 102: Lecture 12: Basic Text Handling Course 102: Lecture 12: Basic Text Handling
Course 102: Lecture 12: Basic Text Handling
Ahmed El-Arabawy
?
Basic shell programs assignment 1_solution_manual
Basic shell programs assignment 1_solution_manualBasic shell programs assignment 1_solution_manual
Basic shell programs assignment 1_solution_manual
Kuntal Bhowmick
?
(2) c sharp introduction_basics_part_i
(2) c sharp introduction_basics_part_i(2) c sharp introduction_basics_part_i
(2) c sharp introduction_basics_part_i
Nico Ludwig
?
(1) cpp introducing the_cpp_programming_language
(1) cpp introducing the_cpp_programming_language(1) cpp introducing the_cpp_programming_language
(1) cpp introducing the_cpp_programming_language
Nico Ludwig
?
Course 102: Lecture 8: Composite Commands
Course 102: Lecture 8: Composite Commands Course 102: Lecture 8: Composite Commands
Course 102: Lecture 8: Composite Commands
Ahmed El-Arabawy
?
(2) c sharp introduction_basics_part_i
(2) c sharp introduction_basics_part_i(2) c sharp introduction_basics_part_i
(2) c sharp introduction_basics_part_i
Nico Ludwig
?
Chip Designer's Code - Linux Terminal Part 3 - File Handling
Chip Designer's Code - Linux Terminal Part 3 - File HandlingChip Designer's Code - Linux Terminal Part 3 - File Handling
Chip Designer's Code - Linux Terminal Part 3 - File Handling
Amr Adel
?

More from Waqqas Jabbar (14)

Introduction to react native @ TIC NUST
Introduction to react native @ TIC NUSTIntroduction to react native @ TIC NUST
Introduction to react native @ TIC NUST
Waqqas Jabbar
?
Jump into React-Native (Class 6)
Jump into React-Native  (Class 6)Jump into React-Native  (Class 6)
Jump into React-Native (Class 6)
Waqqas Jabbar
?
Jump into React-Native (Class 5)
Jump into React-Native  (Class 5)Jump into React-Native  (Class 5)
Jump into React-Native (Class 5)
Waqqas Jabbar
?
Jump into React-Native (Class 4)
Jump into React-Native  (Class 4)Jump into React-Native  (Class 4)
Jump into React-Native (Class 4)
Waqqas Jabbar
?
Jump into React Native (Class 3)
Jump into React Native  (Class 3)Jump into React Native  (Class 3)
Jump into React Native (Class 3)
Waqqas Jabbar
?
Jump into React-Native (Class 2/6)
Jump into React-Native  (Class 2/6)Jump into React-Native  (Class 2/6)
Jump into React-Native (Class 2/6)
Waqqas Jabbar
?
Jump into React-Native (Class 1)
Jump into React-Native (Class 1)Jump into React-Native (Class 1)
Jump into React-Native (Class 1)
Waqqas Jabbar
?
Introduction to React Native
Introduction to React NativeIntroduction to React Native
Introduction to React Native
Waqqas Jabbar
?
Introduction to React Native
Introduction to React NativeIntroduction to React Native
Introduction to React Native
Waqqas Jabbar
?
Redux Saga - Under the hood
Redux Saga - Under the hoodRedux Saga - Under the hood
Redux Saga - Under the hood
Waqqas Jabbar
?
Crud application using react native, redux and redux sagas
Crud application using react native, redux and redux sagasCrud application using react native, redux and redux sagas
Crud application using react native, redux and redux sagas
Waqqas Jabbar
?
Introduction To SVN
Introduction To SVNIntroduction To SVN
Introduction To SVN
Waqqas Jabbar
?
Makefile Generation From Autotools
Makefile Generation From AutotoolsMakefile Generation From Autotools
Makefile Generation From Autotools
Waqqas Jabbar
?
Introduction To SVN
Introduction To SVNIntroduction To SVN
Introduction To SVN
Waqqas Jabbar
?
Introduction to react native @ TIC NUST
Introduction to react native @ TIC NUSTIntroduction to react native @ TIC NUST
Introduction to react native @ TIC NUST
Waqqas Jabbar
?
Jump into React-Native (Class 6)
Jump into React-Native  (Class 6)Jump into React-Native  (Class 6)
Jump into React-Native (Class 6)
Waqqas Jabbar
?
Jump into React-Native (Class 5)
Jump into React-Native  (Class 5)Jump into React-Native  (Class 5)
Jump into React-Native (Class 5)
Waqqas Jabbar
?
Jump into React-Native (Class 4)
Jump into React-Native  (Class 4)Jump into React-Native  (Class 4)
Jump into React-Native (Class 4)
Waqqas Jabbar
?
Jump into React Native (Class 3)
Jump into React Native  (Class 3)Jump into React Native  (Class 3)
Jump into React Native (Class 3)
Waqqas Jabbar
?
Jump into React-Native (Class 2/6)
Jump into React-Native  (Class 2/6)Jump into React-Native  (Class 2/6)
Jump into React-Native (Class 2/6)
Waqqas Jabbar
?
Jump into React-Native (Class 1)
Jump into React-Native (Class 1)Jump into React-Native (Class 1)
Jump into React-Native (Class 1)
Waqqas Jabbar
?
Introduction to React Native
Introduction to React NativeIntroduction to React Native
Introduction to React Native
Waqqas Jabbar
?
Introduction to React Native
Introduction to React NativeIntroduction to React Native
Introduction to React Native
Waqqas Jabbar
?
Redux Saga - Under the hood
Redux Saga - Under the hoodRedux Saga - Under the hood
Redux Saga - Under the hood
Waqqas Jabbar
?
Crud application using react native, redux and redux sagas
Crud application using react native, redux and redux sagasCrud application using react native, redux and redux sagas
Crud application using react native, redux and redux sagas
Waqqas Jabbar
?
Makefile Generation From Autotools
Makefile Generation From AutotoolsMakefile Generation From Autotools
Makefile Generation From Autotools
Waqqas Jabbar
?

Introduction To Makefile

  • 1. Introduction to Makefile Waqqas Jabbar
  • 2. Compiling with gcc Compilation and linking in a single step gcc file1.c file2.c -o exe Compilation and linking in separate steps gcc -c file1.c gcc -c file2.c gcc file1.o file2.o -o exe
  • 3. make command Makefile is the input to make command. Automate rebuilding software by determining the minimum set of commands that need to be called to do this Install program files Clean up object files Reads a file named Makefile in the directory from which it is executed
  • 4. Makefile syntax Rules Target: Dependencies (tab) Command (tab) Command Target A file that is generated by a program Executable or object files The name of an action to carry out Dependencies Files that are used as inputs to create a Target Space separated list May include other targets
  • 5. Makefile syntax Command Shell command If no target is specified during execution of make then the first target is used
  • 6. Make algorithm Build dependencies Determine if dependency is file or action target If file by that name exist -> File Target Otherwise -> Action Target File Target Do nothing Action Target Build target dependencies
  • 7. Make algorithm Check if at least one of the dependencies is newer than the target Execute Commands in that rule, if newer Definition Newer File Target: File Dependency: Check modification times Action Dependency:Check commands (in dependency rule) is executed or not Action Target: File Dependency: Always newer Action Dependency:Check commands (in dependency rule) is executed or not
  • 8. Makefile variable Define Variable1 = value Usage in Commands $(Variable1) Usage in variables Variable2 = $(Variable1) Macros Variable evaluated when command is executed
  • 9. Abstract Rules Defines how to build a file `*.s2' from a file `*.s1', where s1 and s2 are suffixes .s1.s2: (tab) Command (tab) Command
  • 10. Punctuation $<' are the dependencies that changed causing the target to need to be rebuilt `$@' is the target `$^' are all the dependencies for the current rule
  • 11. include Include other Makefiles Variables can be used
  • 12. Executing shell command to assign variables $(shell command) SVN_VERSION=$(shell if test -d .svn ; then svnversion . | sed -e 's/\([1-9]*\)\([MS]\)\?:\?\([1-9]*\)[MS]\?/\1/' ; else echo unknown ; fi) VERINFO=-DSVN_VERSION=&quot;$(SVN_VERSION)&quot; ARCH=$(shell uname -p|sed s/_/' '/|awk '{print $$2}')
  • 13. Loops in command You can write loops as in shell script @for dir in $(SUBDIRS); do \ $(MAKE) -w -C $$dir $(TARGET) clean || exit 1 ; \ done
  • 14. Wildcard and patsubst SRCS := $(wildcard *.cc) $(wildcard *.c) $(wildcard *.cpp) OBJS := $(patsubst %.cc, %.o,$(wildcard *.cc)) $(patsubst %.c, %.o,$(wildcard *.c)) $(patsubst %.cpp, %.o,$(wildcard *.cpp))
  • 15. Environment variable checking ifeq, else,endif ifeq ($(LIB_TYPE), static) LIBEXT = a COMPILE_LIB = $(AR) -ruv $(TOP)/lib/$(LIBNAME).$(LIBEXT) $(OBJS) else LIBEXT = so COMPILE_LIB = $(CC) -fPIC -shared -Wl,-soname,$(LIBNAME).$(LIBEXT) -o $(TOP)/lib/$(LIBNAME).$(LIBEXT) $(OBJS) $(LIBS) endif