Your SlideShare is downloading. ×
0
GNU Make, Autotools, 
CMake ⡆௓ 
Wen Liao
Disclaimer 
ᢞᙳ∦㈨ᩱⅭస⪅ᩚ⌮㈨ᩱཬಶேពぢ䠈ἄ᭷⥂ 
㐣ᄫㅽ☜ㄆ䠈ㄳㆫ⪅⮬⾜᩵㓃
┠ᶆ 
● ⡆௓GNU Make, Autotools, ࿴CMake 
○ Autotools࿴CMake㒊௷ᴟᗘ⡆௓
 ヨ⎔ቃ
Outline 
● GNU Make 
● Autotools 
● CMake 
● ཨ⪃㈨ᩱ 
● Q & A
஦ඛ‽ഛ 
● Ⅽ஢ᝍⓗ⏕άᖾ⚟⨾⁹䠈ㄳ☜ㄆ⦅㍴ჾᨭ᥼㢷 
♧✵ⓑᏐඖ࿴<tab>Ꮠඖ! 
geany: 
<tab> trailing space 
vim:
㜝᪊GNU Make 
● man make 
○ GNU make utility to maintain groups of programs 
○ ???
┤᥋┳౛Ꮚ 
ᛠ㯟⦅㆞?
⡆ႝ䠈gcc hello.c
ຍ☞䠈㏺ಶᛠ㯟⦅㆞?
GG! 㑏せຍinclude Path
㑣㯟ୖⓒಶ᷐᱌ᛠ㯟㎨䠛ᡴ฿Ṛ䠛
ᑙ᱌᷐᱌㔞኱⦅㆞ⓗၥ㢟 
● ᡃ⬟୙⬟ᡴ୍⾜ᣦ௧ᑵᖳᡃ⮬ື⦅㆞? 
● ᡃ⬟୙⬟ྈ⦅㆞᭦ື㐣ⓗ᷐᱌? 
○ ໟྵᨵ஢*.h᷐᱌ᑞ᠕*.c㒔ྍ௨⮬ື㔜⦅ 
● ྍ୙ྍ௨᭷ἄ᭷㟰άⓗ⦅㆞⤌ែ? 
○ タᐃdebug mode㑏᫝release mo...
Yes You Can!
㜝᪊GNU Make 
● man make 
○ The purpose of the make utility is to 
determine automatically which pieces of a 
large program ...
Hello Makefile 
target prerequisites 
recipe ୍ᐃせ<tab>㛤㢌! 
௨ୗ᝟ἣ᭳ᇳ⾜recipe 
● ᪂ቔ䠖target᷐᱌୙Ꮡᅾ 
○ ㏻ᖖtarget᫝୍ಶ᷐᱌䠈ణ᫝㏺୙᫝ᚲせᲄ௳ 
● ᭦᪂...
ㄡ說Makefile୍ᐃせ⦅㆞᷐᱌? 
target prerequisites 
୍㛤ጞྈ᭷Makefile䠈ୗmakeฟ⌧㘒ㄗ說↓ἲ產⏕dep_file 
產⏕dep_fileᚋ᭳ᇳ⾜@echo Test 
產⏕test_fileᑵ୙ᇳ⾜@...
ᶞ≪target 
● ➨୍ಶtarget✃Ⅽdefault target䠈ஓ᫝makeⓗ㐍ධ㯶䚹 
● @⾲♧୙せᢕᣦ௧༳ฟ䠈ྍ௨⦎⩦ᢕ௚ᣡᤶ┳┳䚹 
● $@ᒎ㛤ᚋ᫝targetⓗྡ✃
ㄳẚᑞୖ㡫䠈やᐹ㡰ᗎ䟿
ㆰᩝ 
● タᐃ 
○ VAR = VAL 
○ VAR := VAL 
○ VAL ?= VAL 
○ VAR += VAL 
○ ඼௚ᡃ୙᠐ⓗ 
● タᐃ᫬ᶵ 
○ ᷐᱌䠈㏻ᖖᑵ᫝Makefile內 
○ make ࿨௧ⓗཨᩝ 
○ ⎔ቃㆰ...
VAR = VAL 
VAR := VAL 
VAL ?= VAL 
VAR += VAL
୕ᑠ䠛
ㆰᩝタᐃ: 㐃ືᆺ = 
VAR1 ➨୍ḟタᐃ 
VAR2 ⓗ值࿴VAR1┦ྠ 
༳ฟVAR2ⓗ內ᐜ 
VAR1 ➨஧ḟタᐃ 
෌༳ฟVAR2ⓗ內ᐜ
ඳḟVAR2ⓗ內ᐜ䠈᭳㞉VAR1ᨵㆰ
ㆰᩝタᐃ䠖❧้⏕ᩀᆺ := 
ྈᨵ㏺⾜䠈ᢕ = ᨵᡂ :=
ㆰᩝタᐃ: 㡸タᆺ ?=
ㆰᩝタᐃ䠖ຍ☞ᆺ +=
ᑠ⤖ 
タᐃ ⌮ゎ᪉ᘧ 
VAR = VAL 㐃ືᙧ 
VAR := VAL ❧༶⏕ᩀᙧ 
VAL ?= VAL 㡸タᙧ 
VAR += VAL ຍ☞ᙧ 
඼௚ᡃ୙᠐ⓗ ୙᠐ⓗ୙▱㐨ᛠ㯟ゎ㔚
內ᘓㆰᩝ (⠇㗴) 
ྡ✃ ព⩏ 
$@ targetྡ✃ 
$^ ᡤ᭷ⓗprerequisitesྡ✃ 
$< ➨୍ಶprerequisiteྡ✃ 
⏝㏵அ୍䠖 
target: dep1.c inc.h test.h 
<tab> gcc ...
⠊౛ 
タᐃせ產⏕ⓗᇳ⾜᷐ 
タᐃせ⦅㆞ⓗ⛬ᘧ᷐ 
⦅㆞ཨᩝ 
ᒎ㛤ㆰᩝ䠈౫ᒎ㛤ⓗ 
ㆰᩝ⦅㆞᷐᱌ 
Ύ㝖產⏕ⓗ᷐᱌つ๎
┠㗴ୗ᭷ཎጞ᷐࿴Makefile 
⦅㆞᭷ᒎ㛤ㆰᩝ୪ᣦᐃᑞ 
᠕᷐᱌┠㗴ୗ᭷ཎጞ᷐࿴ 
Makefile 
ᇳ⾜᷐ᕬ⥂產⏕ 
Ύ㝖ᇳ⾜᷐୪㔜᪂⦅㆞
ᲄ௳ุ᪇⠊౛ 
LOGNAME᫝⎔ቃㆰᩝ!
LOGNAME᫝⎔ቃㆰᩝ! 
ே⫗タᐃLOGNAME內ᐜ
function⠇㗴 
● ㄒἲ 
○ $(ภᩝྡ✃ ཨᩝ) 
ศ㢮 ภᩝྡ✃ 說᫂ ⠊౛ (ㄳ㈞฿Makefileᐿ !) 
イᜥ 
$(waring イᜥ) 㢷♧㆙࿌イᜥ௨ཬᑞ᠕ⓗ⾜⹰ $(warning Your gcc version ...
㐃⧰ᢏ 
● OBJS=$(patsubst %.c,%.o,$(shell ls *.c))
ᖖぢ㘒ㄗ䠖ẗಶrecipe ᇳ⾜≪ែ୙ᘏ⧰ 
㐀ᡂⓗᝒ๻ 
⠊౛䠖ᘓ❧୍ಶ┠㗴䠈㐍ධヱ┠㗴๓ᚋ 
༳ฟ┠๓ᕤస┠㗴☜ㄆษ᥮┠㗴ᡂຌ
㛤⊼䠈ἄ᭷ษ᥮฿test┠㗴
✽Ꮿ┳ⓗ䠈ྍ௨┳฿make⹦⌮recipeⓗ 
᪉ᘧ᫝產⏕᪂ⓗprocess䠈ᇳ⾜recipe䠈 
↛ᚋ⤖᮰ヱprocess 
$ strace -f make 
vfork(Process 8949 attached (waiting for...
ᑠ⤖ 
● GNU make: ༠ຓ⦅㆞ⓗ᫬ೃỴᐃ 
○ 㑣㒊௷せ㔜⦅ 
○ ᣦᐃ㔜᪂⦅㆞ⓗືస 
● ᡤ௨ᑃMakefile䠈୺せᥥ㏙ 
○ 產⏕᷐᱌࿴ཎጞ᷐ⓗ㜝⫃ᛶ 
○ ␜㏺ல᷐᱌᭦ື᫬㛫㜝ಀ᭷ㆰ໬ⓗ᫬ೃ䠈ヱ 
೴ொ㯟஦?
⿵඘1: ἄ᭷Makefileⓗmake 
┠๓┠㗴ἄ᭷Makefile 
↷ᶏmakeქქ 
↷ᶏmake++
Implicit Rules 
୕ᑠ䠛
make -p 
CPP㏺㑔ྍ୙᫝C++䠈᭷ 
⯆㊃man cpp 
㑏グᚓㆰᩝႫ䠛
make -p (᥋㜝䠅 
make᭷內ᘓ㡸タⓗ⹦⌮つ๎䠈ㄳ࿴ୖ㡫ㆰᩝ௨ཬ๓㠃ⓗㄒ 
ἲᑞ↷
⿵඘2: .PHONY 
⠊౛Makefile䠈ໟྵ產⏕binary࿴Ύ㝖binary
⋵┳┳ 
Ύ㝖binary 
產⏕᪂᷐䠈ྡ✃Ⅽclean 
᷐᱌cleanᏑᅾ䠈↓ἲΎ㝖binary 
ᚲ㡲฽㝖᷐᱌cleanᡯ⬟ᇳ⾜target
ศᯒ 
● recipe ⓗᇳ⾜࿴target௨ཬprerequisites᷐᱌ 
᫬㛫㈨イ᭷㜝 
● cleanⓗ┠ⓗ୪୙᫝せ產⏕clean᷐᱌䠈ஓᑵ᫝ 
說targetᇳ⾜recipe࿴target᫝ྰⅭ᷐᱌↓㜝
.PHONY 
● ࿴᷐᱌↓㜝ⓗtarget 
● ⏝ἲ 
.PHONY: ௨✵ⓑ㝸㛤ⓗTARGETྡ✃ 
● ⠊౛ 
.PHONY: clean install
ಟṇ 
.PHONYᅾ㏺㑔
┳┳ᩀᯝ
ᘏఙ㢟ᮦ/ᅇᐙຌㄢ 
● ዴఱ⮬ື㐍ධ୙ྠ┠㗴Make? 
● ዴఱ⮬ື產⏕C source᷐࿴Header᷐᱌rule? 
○ hello.c includes f1.h, f2.h䠈ඳኳᚋཪຍධf3.h䚹ᡭືᨵ 
ᚇ⣼䚹 
● ᭷ἄ᭷㎨ἲᢕ...
๦ୗᑵ᫝⣽⠇䠈ㄳ⮬⾜ 
┳᭩䠈つ๸タィ⮬ᕫⓗ 
ᐿ⩦ㄢ䚹
Outline 
● GNU Make 
● Autotools 
● CMake 
● ཨ⪃㈨ᩱ 
● Q & A
」⩦䠖GNU Makeᑠ⤖ 
● GNU make: ༠ຓ⦅㆞ⓗ᫬ೃỴᐃ 
○ 㑣㒊௷せ㔜⦅ 
○ ᣦᐃ㔜᪂⦅㆞ⓗືస 
● ᡤ௨ᑃMakefile䠈୺せᥥ㏙ 
○ 產⏕᷐᱌࿴ཎጞ᷐ⓗ㜝⫃ᛶ 
○ ␜㏺ல᷐᱌᭦ື᫬㛫㜝ಀ᭷ㆰ໬ⓗ᫬ೃ䠈ヱ 
೴ொ...
㑣㯟᭳ொ㯟㑏せ᭷autotools 
● ㊬ᖹྎⓗၥ㢟 
○ memset v.s. bzero 
○ ㊰ᚏ䚸᷐᱌୙ྠ 
○ system call୙ྠ 
● ྠᖹྎ 
○ ภᘧᗜ∧ᮏ୙ྠ䠈prototypeྍ⬟୙ྠ 
● ┦౫ᛶၥ㢟
autotools 
● autotoolⓗ┠ⓗᑵ᫝產⏕ᖹྎୖྍ௨⦅㆞ⓗ⎔ 
ቃ 
● Ⅽ஢㐩฿㏺ᶏⓗ┠ⓗ䠈⣔⤫㟂せ೴฿ୗ㠃ⓗຌ 
⬟ 
○ ᷙ查ᖹྎ⎔ቃ 
○ 產⏕Makefile
ὶ⛬ 
http://en.wikipedia.org/wiki/GNU_build_system#mediaviewer/File:Autoconf-automake-process.svg
http://en.wikipedia.org/wiki/GNU_build_system#mediaviewer/File:Autoconf-automake-process.svg
http://en.wikipedia.org/wiki/GNU_build_system#mediaviewer/File:Autoconf-automake-process.svg
ᚇ」㞯䠛ཱྀ㢌ㅮᑵ᫝ 
1. ⮬ᖿᡈ㊝ᕤලᚋ產⏕᭦ືconfigure. 
ac䠈࿴ᑃMakefile.am 
2. ㊝ᕤල產⏕configure, Makefile.in࿴ 
config.in 
3. ㊝configure䠈產⏕Makefil...
㑏୙᠐䠛౗┳⠊౛ 
఼せᑃⓗᮾす
autoreconfᖳ఼࿧ྉ┦㜝ᕤ 
ලዴaclocal, autoconf, 
automake➼
產⏕ฟ౗ⓗ᷐᱌
產⏕ฟ౗ⓗ᷐᱌
configure.ac 
# せồ∧ᮏ 
AC_PREREQ([2.68]) 
# ዓ௳㈨イ 
AC_INIT([Test_Autotools], [0], [test]) 
# ⤥Automake㈨イ䠈foreign⾲♧୙⏝GNUᶆ‽ 
#...
configure.ac 
# config᷐᱌ 
AC_CONFIG_HEADERS([config.h]) 
# ᮏḟdemo౑⏝ static library 
AC_PROG_RANLIB 
# Makefile ㊰ᚏ 
AC_CONF...
configure.ac 
# ᦏᑜCC ⦅㆞ჾ 
AC_PROG_CC 
# ⤖᮰config䠈㛤ጞ產⏕┦㜝᷐᱌ 
AC_OUTPUT
Makfile.am 
SUBDIRS = libs src
libs/Makefile.am 
# ᣦᐃinclude㊰ᚏ 
AM_CFLAGS = -I../include 
# 產⏕liba.a ࿴libb.b 
# lib_௦⾲Ᏻ⿶᫬せᨺᅾ$(prefix)/lib୰ 
# 㡸タprefix=/u...
libs/Makefile.am 
# 產⏕liba.aⓗ┦౫᷐᱌ 
liba_a_SOURCES = liba.c liba.h 
# 產⏕libb.aⓗ┦౫᷐᱌ 
libb_a_SOURCES = libb.c libb.h 
# Ᏻ⿶฿$...
src/Makefile.am 
# ᣦᐃlinkီலlibrary 
LDADD = ../libs/liba.a ../libs/libb.a 
# ᣦᐃinclude㊰ᚏ 
AM_CFLAGS = -I../include
src/Makefile.am 
# Ᏻ⿶฿$(prefix)/bin 
bin_PROGRAMS = test 
# 產⏕ⓗ᷐᱌┦౫ⓗ᷐᱌ 
test_SOURCES = test.c
● configure 
● make 
● make install 
● make dist # ⮬ືᖳ఼ᡴໟtarball
ᑠ⤖ 
● ౑⏝autotools㟂せ⮬ᕫᑃ 
○ configure.ac 
○ Makefile.am 
● ୖ㠃ⓗ⠊౛ᴟᗘ⡆␎
ᘏఙ㢟ᮦ/ᅇᐙຌㄢ 
● ዴఱ產⏕shared library? 
● ᫝ྰ᭷᭦⫄᫂ⓗlibrary產⏕᪉ᘧ䠛 
○ hint: libtools
๦ୗᑵ᫝⣽⠇䠈ㄳ⮬⾜ 
┳᭩䠈つ๸タィ⮬ᕫⓗ 
ᐿ⩦ㄢ䚹
Outline 
● GNU Make 
● Autotools 
● CMake 
● ཨ⪃㈨ᩱ 
● Q & A
㜝᪊CMake 
CMake᫝1999ᖺ᥎ฟⓗ㛤※⮬⏤㌾㧓ィ␓䠈┠ⓗ 
᫝ᥦ౪୙ྠᖹྎஅ㛫ඹྠⓗ⦅㆞⎔ቃ䚹 
≉ᛶ: 
● ᨭ᥼୙ྠᖹྎ 
● ྍ௨ᑘBuild࿴ཎᮏ⛬ᘧ☞ศ㛤 
○ out-place build 
○ in-place bu...
ὶ⛬ 
● ᧝ᑃCMakeLists.txt 
● ౑⏝⪅ᇳ⾜cmake 
○ 產⏕ヱᖹྎᑞ᠕ⓗ⦅㆞⎔ቃ᷐᱌ዴMakefile➼ 
● ౑⏝⪅ᇳ⾜ᖹྎୖⓗ⦅㆞᪉ἲ 
○ ዴmake 
● ౑⏝⪅ᇳ⾜cmake installᏳ⿶㌾㧓䚹
⠊౛ 
఼せᑃⓗᮾす
CMakeLists.txt 
# ∧ᮏ㟂ồ 
cmake_minimum_required(VERSION 2.8) 
# ఼ⓗProjectྡ✃ 
project(testcmake)
CMakeLists.txt 
# タᐃㆰᩝ 
set(SRC_DIR src) 
set(LIB_DIR libs) 
set(INC_DIR include) 
# Compile flags 
set(CMAKE_C_FLAGS "-Wa...
CMakeLists.txt 
# ᣦ௧include┠㗴 
include_directories(${INC_DIR}) 
# ࿌ッCMakeせཤୗิⓗ┠㗴⦅㆞ 
add_subdirectory(${SRC_DIR}) 
add_subd...
libs/CMakeLists.txt 
# タᐃㆰᩝ䠈ᣦᐃlibrary┦౫᪊ီಶ᷐᱌ 
set(liba_SRCS liba.c) 
set(libb_SRCS libb.c) 
# ᣦᐃ⦅㆞ᆺᘧⅭshared library 
add_l...
libs/CMakeLists.txt 
# Ᏻ⿶᱁ᘧ 
# install(TARGETS ภᘧᗜྡ✃ LIBRARY 
# DESTINATION Ᏻ⿶┠㗴㊰ᚏ) 
install(TARGETS a b LIBRARY 
DESTINAT...
src/CMakeLists.txt 
# タᐃㆰᩝ 
set(test_SRCS test.c) 
# 產⏕ᇳ⾜᷐ 
add_executable(${PROJECT_NAME} 
${test_SRCS}) 
# ᣦ௧linkภᘧᗜ 
ta...
src/CMakeLists.txt 
# Ᏻ⿶᱁ᘧ 
# install(TARGETS ᇳ⾜᷐ྡ✃ 
# DESTINATION Ᏻ⿶┠㗴㊰ᚏ) 
install(TARGETS ${PROJECT_NAME} 
DESTINATION b...
include/CMakeLists.txt 
# install(FILES Header᷐ྡ✃ 
# DESTINATION Ᏻ⿶┠㗴㊰ᚏ) 
install(FILES liba.h libb.h 
DESTINATION include...
out-place build
ᑠ⤖ 
● ఼せᑃCMakeLists.txt䠈ᣦᐃせ㐍ධီல┠㗴 
⦅㆞䠈ᡈ᫝┦㜝ⓗ᷐᱌௨ཬ㡸ᮇ⦅㆞࿴Ᏻ⿶ⓗ 
ᆺែ 
● ୖ㠃ⓗ⠊౛ᴟᗘ⡆␎
ᘏఙ㢟ᮦ/ᅇᐙຌㄢ 
● ヲ⣽ㄒἲ 
○ ㆰᩝタᐃ 
○ ᲄ௳タᐃ 
○ ᕧ㞟࿴ภᩝ 
● ᡴໟ᪉ᘧ
๦ୗᑵ᫝⣽⠇䠈ㄳ⮬⾜ 
┳᭩䠈つ๸タィ⮬ᕫⓗ 
ᐿ⩦ㄢ䚹
Outline 
● GNU Make 
● Autotools 
● CMake 
● ཨ⪃㈨ᩱ 
● Q & A
ཨ⪃㈨ᩱ 
● GNU Makeᡭ෉ 
○ http://www.gnu.org/software/make/manual/make.html 
● GNU Make ᛌ㏿ཨ⪃ 
○ http://www.gnu.org/software/ma...
ཨ⪃㈨ᩱ 
● CMake Wiki 
○ http://www.cmake.org/Wiki/CMake 
● CMake-tutorial (኱᥎) 
○ https://github.com/TheErk/CMake-tutorial
Outline 
● GNU Make 
● Autotools 
● CMake 
● ཨ⪃㈨ᩱ 
● Q & A
GNU Make, Autotools, CMake 簡介
GNU Make, Autotools, CMake 簡介
GNU Make, Autotools, CMake 簡介
GNU Make, Autotools, CMake 簡介
GNU Make, Autotools, CMake 簡介
GNU Make, Autotools, CMake 簡介
GNU Make, Autotools, CMake 簡介
GNU Make, Autotools, CMake 簡介
GNU Make, Autotools, CMake 簡介
GNU Make, Autotools, CMake 簡介
GNU Make, Autotools, CMake 簡介
Upcoming SlideShare
Loading in...5
×

GNU Make, Autotools, CMake 簡介

9,726

Published on

2014/Aug/20 新增implicit rules和.PHONY說明
2014/Jul/19 Fix minor error
2014/Jul/12 Fix minor error

Published in: Technology
0 Comments
95 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
9,726
On Slideshare
0
From Embeds
0
Number of Embeds
19
Actions
Shares
0
Downloads
303
Comments
0
Likes
95
Embeds 0
No embeds

No notes for slide

Transcript of "GNU Make, Autotools, CMake 簡介"

  1. 1. GNU Make, Autotools, CMake ⡆௓ Wen Liao
  2. 2. Disclaimer ᢞᙳ∦㈨ᩱⅭస⪅ᩚ⌮㈨ᩱཬಶேពぢ䠈ἄ᭷⥂ 㐣ᄫㅽ☜ㄆ䠈ㄳㆫ⪅⮬⾜᩵㓃
  3. 3. ┠ᶆ ● ⡆௓GNU Make, Autotools, ࿴CMake ○ Autotools࿴CMake㒊௷ᴟᗘ⡆௓
  4. 4.  ヨ⎔ቃ
  5. 5. Outline ● GNU Make ● Autotools ● CMake ● ཨ⪃㈨ᩱ ● Q & A
  6. 6. ஦ඛ‽ഛ ● Ⅽ஢ᝍⓗ⏕άᖾ⚟⨾⁹䠈ㄳ☜ㄆ⦅㍴ჾᨭ᥼㢷 ♧✵ⓑᏐඖ࿴<tab>Ꮠඖ! geany: <tab> trailing space vim:
  7. 7. 㜝᪊GNU Make ● man make ○ GNU make utility to maintain groups of programs ○ ???
  8. 8. ┤᥋┳౛Ꮚ ᛠ㯟⦅㆞?
  9. 9. ⡆ႝ䠈gcc hello.c
  10. 10. ຍ☞䠈㏺ಶᛠ㯟⦅㆞?
  11. 11. GG! 㑏せຍinclude Path
  12. 12. 㑣㯟ୖⓒಶ᷐᱌ᛠ㯟㎨䠛ᡴ฿Ṛ䠛
  13. 13. ᑙ᱌᷐᱌㔞኱⦅㆞ⓗၥ㢟 ● ᡃ⬟୙⬟ᡴ୍⾜ᣦ௧ᑵᖳᡃ⮬ື⦅㆞? ● ᡃ⬟୙⬟ྈ⦅㆞᭦ື㐣ⓗ᷐᱌? ○ ໟྵᨵ஢*.h᷐᱌ᑞ᠕*.c㒔ྍ௨⮬ື㔜⦅ ● ྍ୙ྍ௨᭷ἄ᭷㟰άⓗ⦅㆞⤌ែ? ○ タᐃdebug mode㑏᫝release mode ○ タᐃ⦅㆞ჾ㑅㡯 ○ Compile timeᣦᐃᕧ㞟 ■ -DMY_VAR=1 ○ ...
  14. 14. Yes You Can!
  15. 15. 㜝᪊GNU Make ● man make ○ The purpose of the make utility is to determine automatically which pieces of a large program need to be recompiled, and issue the commands to recompile them. ○ ⓑヰ: ༠ຓ⦅㆞ⓗ᫬ೃỴᐃ ■ 㑣㒊௷せ㔜⦅ ■ ᣦᐃ㔜᪂⦅㆞ⓗືస
  16. 16. Hello Makefile target prerequisites recipe ୍ᐃせ<tab>㛤㢌! ௨ୗ᝟ἣ᭳ᇳ⾜recipe ● ᪂ቔ䠖target᷐᱌୙Ꮡᅾ ○ ㏻ᖖtarget᫝୍ಶ᷐᱌䠈ణ᫝㏺୙᫝ᚲせᲄ௳ ● ᭦᪂䠖prerequisites᷐᱌᭦ື᫬㛫ẚtarget᷐ ᱌㑏せ᪂
  17. 17. ㄡ說Makefile୍ᐃせ⦅㆞᷐᱌? target prerequisites ୍㛤ጞྈ᭷Makefile䠈ୗmakeฟ⌧㘒ㄗ說↓ἲ產⏕dep_file 產⏕dep_fileᚋ᭳ᇳ⾜@echo Test 產⏕test_fileᑵ୙ᇳ⾜@echo Test ᭦᪂dep_file ᭦ື᫬㛫ᚋ᭳ᇳ⾜@echo Test recipe
  18. 18. ᶞ≪target ● ➨୍ಶtarget✃Ⅽdefault target䠈ஓ᫝makeⓗ㐍ධ㯶䚹 ● @⾲♧୙せᢕᣦ௧༳ฟ䠈ྍ௨⦎⩦ᢕ௚ᣡᤶ┳┳䚹 ● $@ᒎ㛤ᚋ᫝targetⓗྡ✃
  19. 19. ㄳẚᑞୖ㡫䠈やᐹ㡰ᗎ䟿
  20. 20. ㆰᩝ ● タᐃ ○ VAR = VAL ○ VAR := VAL ○ VAL ?= VAL ○ VAR += VAL ○ ඼௚ᡃ୙᠐ⓗ ● タᐃ᫬ᶵ ○ ᷐᱌䠈㏻ᖖᑵ᫝Makefile內 ○ make ࿨௧ⓗཨᩝ ○ ⎔ቃㆰᩝ ● ྲྀ值 ○ $(VAR)
  21. 21. VAR = VAL VAR := VAL VAL ?= VAL VAR += VAL
  22. 22. ୕ᑠ䠛
  23. 23. ㆰᩝタᐃ: 㐃ືᆺ = VAR1 ➨୍ḟタᐃ VAR2 ⓗ值࿴VAR1┦ྠ ༳ฟVAR2ⓗ內ᐜ VAR1 ➨஧ḟタᐃ ෌༳ฟVAR2ⓗ內ᐜ
  24. 24. ඳḟVAR2ⓗ內ᐜ䠈᭳㞉VAR1ᨵㆰ
  25. 25. ㆰᩝタᐃ䠖❧้⏕ᩀᆺ := ྈᨵ㏺⾜䠈ᢕ = ᨵᡂ :=
  26. 26. ㆰᩝタᐃ: 㡸タᆺ ?=
  27. 27. ㆰᩝタᐃ䠖ຍ☞ᆺ +=
  28. 28. ᑠ⤖ タᐃ ⌮ゎ᪉ᘧ VAR = VAL 㐃ືᙧ VAR := VAL ❧༶⏕ᩀᙧ VAL ?= VAL 㡸タᙧ VAR += VAL ຍ☞ᙧ ඼௚ᡃ୙᠐ⓗ ୙᠐ⓗ୙▱㐨ᛠ㯟ゎ㔚
  29. 29. 內ᘓㆰᩝ (⠇㗴) ྡ✃ ព⩏ $@ targetྡ✃ $^ ᡤ᭷ⓗprerequisitesྡ✃ $< ➨୍ಶprerequisiteྡ✃ ⏝㏵அ୍䠖 target: dep1.c inc.h test.h <tab> gcc -o $@ $< $? ẚtarget㑏᪂ⓗprerequisitesྡ✃
  30. 30. ⠊౛ タᐃせ產⏕ⓗᇳ⾜᷐ タᐃせ⦅㆞ⓗ⛬ᘧ᷐ ⦅㆞ཨᩝ ᒎ㛤ㆰᩝ䠈౫ᒎ㛤ⓗ ㆰᩝ⦅㆞᷐᱌ Ύ㝖產⏕ⓗ᷐᱌つ๎
  31. 31. ┠㗴ୗ᭷ཎጞ᷐࿴Makefile ⦅㆞᭷ᒎ㛤ㆰᩝ୪ᣦᐃᑞ ᠕᷐᱌┠㗴ୗ᭷ཎጞ᷐࿴ Makefile ᇳ⾜᷐ᕬ⥂產⏕ Ύ㝖ᇳ⾜᷐୪㔜᪂⦅㆞
  32. 32. ᲄ௳ุ᪇⠊౛ LOGNAME᫝⎔ቃㆰᩝ!
  33. 33. LOGNAME᫝⎔ቃㆰᩝ! ே⫗タᐃLOGNAME內ᐜ
  34. 34. function⠇㗴 ● ㄒἲ ○ $(ภᩝྡ✃ ཨᩝ) ศ㢮 ภᩝྡ✃ 說᫂ ⠊౛ (ㄳ㈞฿Makefileᐿ !) イᜥ $(waring イᜥ) 㢷♧㆙࿌イᜥ௨ཬᑞ᠕ⓗ⾜⹰ $(warning Your gcc version is too old) $(error イᜥ) 㢷♧㘒ㄗイᜥ䚸ᑞ᠕ⓗ⾜⹰ᚋ⤖᮰ make conf=my_file $(error file $(conf) not found) Ꮠ୵ ⹦⌮ $(subst from,to,⹦⌮ᩥᏐ) Ꮠ୵᭰᥮,ᚋ㠃✵ⓑⅭཨᩝⓗ୍㒊௷ $(warning $(subst .c,.o,test.c hello.c)) $(patsubst pattern,᭰᥮ᩥᏐ,⹦⌮ᩥᏐ) patternᏐ୵᭰᥮,ᚋ㠃✵ⓑⅭཨᩝⓗ୍ 㒊௷䚹%௦⾲௵ព㛗ᗘⓗ௵ពᏐඖ䚹 $(warning $(patsubst t%.c,a%.o,test.c hello.c)) ඼௚ $(shell ࿨௧) ᇳ⾜࿨௧䠈ᅇബᩥᏐ⤖ᯝ $(warning $(shell ls /))
  35. 35. 㐃⧰ᢏ ● OBJS=$(patsubst %.c,%.o,$(shell ls *.c))
  36. 36. ᖖぢ㘒ㄗ䠖ẗಶrecipe ᇳ⾜≪ែ୙ᘏ⧰ 㐀ᡂⓗᝒ๻ ⠊౛䠖ᘓ❧୍ಶ┠㗴䠈㐍ධヱ┠㗴๓ᚋ ༳ฟ┠๓ᕤస┠㗴☜ㄆษ᥮┠㗴ᡂຌ
  37. 37. 㛤⊼䠈ἄ᭷ษ᥮฿test┠㗴
  38. 38. ✽Ꮿ┳ⓗ䠈ྍ௨┳฿make⹦⌮recipeⓗ ᪉ᘧ᫝產⏕᪂ⓗprocess䠈ᇳ⾜recipe䠈 ↛ᚋ⤖᮰ヱprocess $ strace -f make vfork(Process 8949 attached (waiting for parent) Process 8949 resumed (parent 8947 ready) ) = 8949 [pid 8949] execve("/bin/mkdir", ["mkdir", "-p", "test"], [/* 42 vars */]) = 0 <... wait4 resumed> [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], 0, NULL) = 8949 --- SIGCHLD (Child exited) @ 0 (0) ---
  39. 39. ᑠ⤖ ● GNU make: ༠ຓ⦅㆞ⓗ᫬ೃỴᐃ ○ 㑣㒊௷せ㔜⦅ ○ ᣦᐃ㔜᪂⦅㆞ⓗືస ● ᡤ௨ᑃMakefile䠈୺せᥥ㏙ ○ 產⏕᷐᱌࿴ཎጞ᷐ⓗ㜝⫃ᛶ ○ ␜㏺ல᷐᱌᭦ື᫬㛫㜝ಀ᭷ㆰ໬ⓗ᫬ೃ䠈ヱ ೴ொ㯟஦?
  40. 40. ⿵඘1: ἄ᭷Makefileⓗmake ┠๓┠㗴ἄ᭷Makefile ↷ᶏmakeქქ ↷ᶏmake++
  41. 41. Implicit Rules ୕ᑠ䠛
  42. 42. make -p CPP㏺㑔ྍ୙᫝C++䠈᭷ ⯆㊃man cpp 㑏グᚓㆰᩝႫ䠛
  43. 43. make -p (᥋㜝䠅 make᭷內ᘓ㡸タⓗ⹦⌮つ๎䠈ㄳ࿴ୖ㡫ㆰᩝ௨ཬ๓㠃ⓗㄒ ἲᑞ↷
  44. 44. ⿵඘2: .PHONY ⠊౛Makefile䠈ໟྵ產⏕binary࿴Ύ㝖binary
  45. 45. ⋵┳┳ Ύ㝖binary 產⏕᪂᷐䠈ྡ✃Ⅽclean ᷐᱌cleanᏑᅾ䠈↓ἲΎ㝖binary ᚲ㡲฽㝖᷐᱌cleanᡯ⬟ᇳ⾜target
  46. 46. ศᯒ ● recipe ⓗᇳ⾜࿴target௨ཬprerequisites᷐᱌ ᫬㛫㈨イ᭷㜝 ● cleanⓗ┠ⓗ୪୙᫝せ產⏕clean᷐᱌䠈ஓᑵ᫝ 說targetᇳ⾜recipe࿴target᫝ྰⅭ᷐᱌↓㜝
  47. 47. .PHONY ● ࿴᷐᱌↓㜝ⓗtarget ● ⏝ἲ .PHONY: ௨✵ⓑ㝸㛤ⓗTARGETྡ✃ ● ⠊౛ .PHONY: clean install
  48. 48. ಟṇ .PHONYᅾ㏺㑔
  49. 49. ┳┳ᩀᯝ
  50. 50. ᘏఙ㢟ᮦ/ᅇᐙຌㄢ ● ዴఱ⮬ື㐍ධ୙ྠ┠㗴Make? ● ዴఱ⮬ື產⏕C source᷐࿴Header᷐᱌rule? ○ hello.c includes f1.h, f2.h䠈ඳኳᚋཪຍධf3.h䚹ᡭືᨵ ᚇ⣼䚹 ● ᭷ἄ᭷㎨ἲᢕᡤ᭷ⓗタᐃᨺᅾ᷐᱌內⤥ Makefile include?
  51. 51. ๦ୗᑵ᫝⣽⠇䠈ㄳ⮬⾜ ┳᭩䠈つ๸タィ⮬ᕫⓗ ᐿ⩦ㄢ䚹
  52. 52. Outline ● GNU Make ● Autotools ● CMake ● ཨ⪃㈨ᩱ ● Q & A
  53. 53. 」⩦䠖GNU Makeᑠ⤖ ● GNU make: ༠ຓ⦅㆞ⓗ᫬ೃỴᐃ ○ 㑣㒊௷せ㔜⦅ ○ ᣦᐃ㔜᪂⦅㆞ⓗືస ● ᡤ௨ᑃMakefile䠈୺せᥥ㏙ ○ 產⏕᷐᱌࿴ཎጞ᷐ⓗ㜝⫃ᛶ ○ ␜㏺ல᷐᱌᭦ື᫬㛫㜝ಀ᭷ㆰ໬ⓗ᫬ೃ䠈ヱ ೴ொ㯟஦?
  54. 54. 㑣㯟᭳ொ㯟㑏せ᭷autotools ● ㊬ᖹྎⓗၥ㢟 ○ memset v.s. bzero ○ ㊰ᚏ䚸᷐᱌୙ྠ ○ system call୙ྠ ● ྠᖹྎ ○ ภᘧᗜ∧ᮏ୙ྠ䠈prototypeྍ⬟୙ྠ ● ┦౫ᛶၥ㢟
  55. 55. autotools ● autotoolⓗ┠ⓗᑵ᫝產⏕ᖹྎୖྍ௨⦅㆞ⓗ⎔ ቃ ● Ⅽ஢㐩฿㏺ᶏⓗ┠ⓗ䠈⣔⤫㟂せ೴฿ୗ㠃ⓗຌ ⬟ ○ ᷙ查ᖹྎ⎔ቃ ○ 產⏕Makefile
  56. 56. ὶ⛬ http://en.wikipedia.org/wiki/GNU_build_system#mediaviewer/File:Autoconf-automake-process.svg
  57. 57. http://en.wikipedia.org/wiki/GNU_build_system#mediaviewer/File:Autoconf-automake-process.svg
  58. 58. http://en.wikipedia.org/wiki/GNU_build_system#mediaviewer/File:Autoconf-automake-process.svg
  59. 59. ᚇ」㞯䠛ཱྀ㢌ㅮᑵ᫝ 1. ⮬ᖿᡈ㊝ᕤලᚋ產⏕᭦ືconfigure. ac䠈࿴ᑃMakefile.am 2. ㊝ᕤල產⏕configure, Makefile.in࿴ config.in 3. ㊝configure䠈產⏕Makefile࿴config.h 4. make; make install
  60. 60. 㑏୙᠐䠛౗┳⠊౛ ఼せᑃⓗᮾす
  61. 61. autoreconfᖳ఼࿧ྉ┦㜝ᕤ ලዴaclocal, autoconf, automake➼
  62. 62. 產⏕ฟ౗ⓗ᷐᱌
  63. 63. 產⏕ฟ౗ⓗ᷐᱌
  64. 64. configure.ac # せồ∧ᮏ AC_PREREQ([2.68]) # ዓ௳㈨イ AC_INIT([Test_Autotools], [0], [test]) # ⤥Automake㈨イ䠈foreign⾲♧୙⏝GNUᶆ‽ # ஓᑵ᫝୙㟂せchangelog, AUTHORS➼᷐᱌ AM_INIT_AUTOMAKE([foreign -Wall -Werror])
  65. 65. configure.ac # config᷐᱌ AC_CONFIG_HEADERS([config.h]) # ᮏḟdemo౑⏝ static library AC_PROG_RANLIB # Makefile ㊰ᚏ AC_CONFIG_FILES([Makefile src/Makefile libs/Makefile])
  66. 66. configure.ac # ᦏᑜCC ⦅㆞ჾ AC_PROG_CC # ⤖᮰config䠈㛤ጞ產⏕┦㜝᷐᱌ AC_OUTPUT
  67. 67. Makfile.am SUBDIRS = libs src
  68. 68. libs/Makefile.am # ᣦᐃinclude㊰ᚏ AM_CFLAGS = -I../include # 產⏕liba.a ࿴libb.b # lib_௦⾲Ᏻ⿶᫬せᨺᅾ$(prefix)/lib୰ # 㡸タprefix=/usr/local lib_LIBRARIES = liba.a libb.a
  69. 69. libs/Makefile.am # 產⏕liba.aⓗ┦౫᷐᱌ liba_a_SOURCES = liba.c liba.h # 產⏕libb.aⓗ┦౫᷐᱌ libb_a_SOURCES = libb.c libb.h # Ᏻ⿶฿$(prefix)/includeⓗ᷐᱌ include_HEADERS = ../include/liba.h .. /include/libb.h
  70. 70. src/Makefile.am # ᣦᐃlinkီலlibrary LDADD = ../libs/liba.a ../libs/libb.a # ᣦᐃinclude㊰ᚏ AM_CFLAGS = -I../include
  71. 71. src/Makefile.am # Ᏻ⿶฿$(prefix)/bin bin_PROGRAMS = test # 產⏕ⓗ᷐᱌┦౫ⓗ᷐᱌ test_SOURCES = test.c
  72. 72. ● configure ● make ● make install ● make dist # ⮬ືᖳ఼ᡴໟtarball
  73. 73. ᑠ⤖ ● ౑⏝autotools㟂せ⮬ᕫᑃ ○ configure.ac ○ Makefile.am ● ୖ㠃ⓗ⠊౛ᴟᗘ⡆␎
  74. 74. ᘏఙ㢟ᮦ/ᅇᐙຌㄢ ● ዴఱ產⏕shared library? ● ᫝ྰ᭷᭦⫄᫂ⓗlibrary產⏕᪉ᘧ䠛 ○ hint: libtools
  75. 75. ๦ୗᑵ᫝⣽⠇䠈ㄳ⮬⾜ ┳᭩䠈つ๸タィ⮬ᕫⓗ ᐿ⩦ㄢ䚹
  76. 76. Outline ● GNU Make ● Autotools ● CMake ● ཨ⪃㈨ᩱ ● Q & A
  77. 77. 㜝᪊CMake CMake᫝1999ᖺ᥎ฟⓗ㛤※⮬⏤㌾㧓ィ␓䠈┠ⓗ ᫝ᥦ౪୙ྠᖹྎஅ㛫ඹྠⓗ⦅㆞⎔ቃ䚹 ≉ᛶ: ● ᨭ᥼୙ྠᖹྎ ● ྍ௨ᑘBuild࿴ཎᮏ⛬ᘧ☞ศ㛤 ○ out-place build ○ in-place build ● ᨭ᥼cacheຍᛌ⦅㆞㏿ᗘ
  78. 78. ὶ⛬ ● ᧝ᑃCMakeLists.txt ● ౑⏝⪅ᇳ⾜cmake ○ 產⏕ヱᖹྎᑞ᠕ⓗ⦅㆞⎔ቃ᷐᱌ዴMakefile➼ ● ౑⏝⪅ᇳ⾜ᖹྎୖⓗ⦅㆞᪉ἲ ○ ዴmake ● ౑⏝⪅ᇳ⾜cmake installᏳ⿶㌾㧓䚹
  79. 79. ⠊౛ ఼せᑃⓗᮾす
  80. 80. CMakeLists.txt # ∧ᮏ㟂ồ cmake_minimum_required(VERSION 2.8) # ఼ⓗProjectྡ✃ project(testcmake)
  81. 81. CMakeLists.txt # タᐃㆰᩝ set(SRC_DIR src) set(LIB_DIR libs) set(INC_DIR include) # Compile flags set(CMAKE_C_FLAGS "-Wall -Werror")
  82. 82. CMakeLists.txt # ᣦ௧include┠㗴 include_directories(${INC_DIR}) # ࿌ッCMakeせཤୗิⓗ┠㗴⦅㆞ add_subdirectory(${SRC_DIR}) add_subdirectory(${LIB_DIR}) add_subdirectory(${INC_DIR})
  83. 83. libs/CMakeLists.txt # タᐃㆰᩝ䠈ᣦᐃlibrary┦౫᪊ီಶ᷐᱌ set(liba_SRCS liba.c) set(libb_SRCS libb.c) # ᣦᐃ⦅㆞ᆺᘧⅭshared library add_library(a SHARED ${liba_SRCS}) add_library(b SHARED ${libb_SRCS})
  84. 84. libs/CMakeLists.txt # Ᏻ⿶᱁ᘧ # install(TARGETS ภᘧᗜྡ✃ LIBRARY # DESTINATION Ᏻ⿶┠㗴㊰ᚏ) install(TARGETS a b LIBRARY DESTINATION lib)
  85. 85. src/CMakeLists.txt # タᐃㆰᩝ set(test_SRCS test.c) # 產⏕ᇳ⾜᷐ add_executable(${PROJECT_NAME} ${test_SRCS}) # ᣦ௧linkภᘧᗜ target_link_libraries(${PROJECT_NAME} a b)
  86. 86. src/CMakeLists.txt # Ᏻ⿶᱁ᘧ # install(TARGETS ᇳ⾜᷐ྡ✃ # DESTINATION Ᏻ⿶┠㗴㊰ᚏ) install(TARGETS ${PROJECT_NAME} DESTINATION bin)
  87. 87. include/CMakeLists.txt # install(FILES Header᷐ྡ✃ # DESTINATION Ᏻ⿶┠㗴㊰ᚏ) install(FILES liba.h libb.h DESTINATION include)
  88. 88. out-place build
  89. 89. ᑠ⤖ ● ఼せᑃCMakeLists.txt䠈ᣦᐃせ㐍ධီல┠㗴 ⦅㆞䠈ᡈ᫝┦㜝ⓗ᷐᱌௨ཬ㡸ᮇ⦅㆞࿴Ᏻ⿶ⓗ ᆺែ ● ୖ㠃ⓗ⠊౛ᴟᗘ⡆␎
  90. 90. ᘏఙ㢟ᮦ/ᅇᐙຌㄢ ● ヲ⣽ㄒἲ ○ ㆰᩝタᐃ ○ ᲄ௳タᐃ ○ ᕧ㞟࿴ภᩝ ● ᡴໟ᪉ᘧ
  91. 91. ๦ୗᑵ᫝⣽⠇䠈ㄳ⮬⾜ ┳᭩䠈つ๸タィ⮬ᕫⓗ ᐿ⩦ㄢ䚹
  92. 92. Outline ● GNU Make ● Autotools ● CMake ● ཨ⪃㈨ᩱ ● Q & A
  93. 93. ཨ⪃㈨ᩱ ● GNU Makeᡭ෉ ○ http://www.gnu.org/software/make/manual/make.html ● GNU Make ᛌ㏿ཨ⪃ ○ http://www.gnu.org/software/make/manual/make. html#Quick-Reference ● GNU Automakeᡭ෉ ○ http://www.gnu. org/software/automake/manual/automake.html ● Alexandre Duret Lutz: Autotools Tutorial (኱ ᥎) ○ https://www.lrde.epita.fr/~adl/autotools.html
  94. 94. ཨ⪃㈨ᩱ ● CMake Wiki ○ http://www.cmake.org/Wiki/CMake ● CMake-tutorial (኱᥎) ○ https://github.com/TheErk/CMake-tutorial
  95. 95. Outline ● GNU Make ● Autotools ● CMake ● ཨ⪃㈨ᩱ ● Q & A
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×