More Related Content Similar to GNU Make, Autotools, CMake 簡介 (20) GNU Make, Autotools, CMake 簡介3. ┠ᶆ
● ⡆GNU Make, Autotools, CMake
○ AutotoolsCMake㒊௷ᴟᗘ⡆
7. 㜝GNU Make
● man make
○ GNU make utility to maintain groups of programs
○ ???
13. ᑙ᷐㔞⦅㆞ⓗၥ㢟
● ᡃ⬟⬟ᡴ୍⾜ᣦ௧ᑵᖳᡃ⮬ື⦅㆞?
● ᡃ⬟⬟ྈ⦅㆞᭦ື㐣ⓗ᷐?
○ ໟྵᨵ*.h᷐ᑞ᠕*.c㒔ྍ௨⮬ື㔜⦅
● ྍྍ௨᭷ἄ᭷㟰άⓗ⦅㆞⤌ែ?
○ タᐃdebug mode㑏release mode
○ タᐃ⦅㆞ჾ㑅㡯
○ Compile timeᣦᐃᕧ㞟
■ -DMY_VAR=1
○ ...
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. Hello Makefile
target prerequisites
recipe ୍ᐃせ<tab>㛤㢌!
௨ୗἣ᭳ᇳ⾜recipe
● ᪂ቔ䠖target᷐Ꮡᅾ
○ ㏻ᖖtarget୍ಶ᷐䠈ణ㏺ᚲせᲄ௳
● ᭦᪂䠖prerequisites᷐᭦ື㛫ẚtarget᷐
㑏せ᪂
20. ㆰᩝ
● タᐃ
○ VAR = VAL
○ VAR := VAL
○ VAL ?= VAL
○ VAR += VAL
○ ᡃ᠐ⓗ
● タᐃᶵ
○ ᷐䠈㏻ᖖᑵMakefile內
○ make ௧ⓗཨᩝ
○ ⎔ቃㆰᩝ
● ྲྀ值
○ $(VAR)
21. VAR = VAL
VAR := VAL
VAL ?= VAL
VAR += VAL
23. ㆰᩝタᐃ: 㐃ືᆺ =
VAR1 ➨୍ḟタᐃ
VAR2 ⓗ值VAR1┦ྠ
༳ฟVAR2ⓗ內ᐜ
VAR1 ➨ḟタᐃ
༳ฟVAR2ⓗ內ᐜ
31. ᑠ⤖
タᐃ ⌮ゎ᪉ᘧ
VAR = VAL 㐃ືᙧ
VAR := VAL ❧༶⏕ᩀᙧ
VAL ?= VAL 㡸タᙧ
VAR += VAL ຍ☞ᙧ
ᡃ᠐ⓗ ᠐ⓗ▱㐨ᛠ㯟ゎ㔚
32. 內ᘓㆰᩝ (⠇㗴)
ྡ✃ ព⩏
$@ targetྡ✃
$^ ᡤ᭷ⓗprerequisitesྡ✃
$< ➨୍ಶprerequisiteྡ✃
⏝㏵அ୍䠖
target: dep1.c inc.h test.h
<tab> gcc -o $@ $<
$? ẚtarget㑏᪂ⓗprerequisitesྡ✃
37. 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 /))
41. ✽Ꮿ┳ⓗ䠈ྍ௨┳฿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) ---
42. ᑠ⤖
● GNU make: ༠ຓ⦅㆞ⓗೃỴᐃ
○ 㑣㒊௷せ㔜⦅
○ ᣦᐃ㔜᪂⦅㆞ⓗືస
● ᡤ௨ᑃMakefile䠈せᥥ㏙
○ 產⏕᷐ཎጞ᷐ⓗ㜝⫃ᛶ
○ ␜㏺ல᷐᭦ື㛫㜝ಀ᭷ㆰⓗೃ䠈ヱ
ொ㯟?
49. ศᯒ
● recipe ⓗᇳ⾜target௨ཬprerequisites᷐
㛫㈨イ᭷㜝
● cleanⓗ┠ⓗ୪せ產⏕clean᷐䠈ஓᑵ
說targetᇳ⾜recipetargetྰⅭ᷐↓㜝
53. ᘏఙ㢟ᮦ/ᅇᐙຌㄢ
● ዴఱ⮬ື㐍ධྠ┠㗴Make?
● ዴఱ⮬ື產⏕C source᷐Header᷐rule?
○ hello.c includes f1.h, f2.h䠈ඳኳᚋཪຍධf3.h䚹ᡭືᨵ
ᚇ⣼䚹
● ᭷ἄ᭷㎨ἲᢕᡤ᭷ⓗタᐃᨺᅾ᷐內⤥
Makefile include?
56. 」⩦䠖GNU Makeᑠ⤖
● GNU make: ༠ຓ⦅㆞ⓗೃỴᐃ
○ 㑣㒊௷せ㔜⦅
○ ᣦᐃ㔜᪂⦅㆞ⓗືస
● ᡤ௨ᑃMakefile䠈せᥥ㏙
○ 產⏕᷐ཎጞ᷐ⓗ㜝⫃ᛶ
○ ␜㏺ல᷐᭦ື㛫㜝ಀ᭷ㆰⓗೃ䠈ヱ
ொ㯟?
67. configure.ac
# せồ∧ᮏ
AC_PREREQ([2.68])
# ዓ௳㈨イ
AC_INIT([Test_Autotools], [0], [test])
# ⤥Automake㈨イ䠈foreign⾲♧⏝GNUᶆ‽
# ஓᑵ㟂せchangelog, AUTHORS➼᷐
AM_INIT_AUTOMAKE([foreign -Wall -Werror])
68. configure.ac
# config᷐
AC_CONFIG_HEADERS([config.h])
# ᮏḟdemo⏝ static library
AC_PROG_RANLIB
# Makefile ㊰ᚏ
AC_CONFIG_FILES([Makefile src/Makefile
libs/Makefile])
71. libs/Makefile.am
# ᣦᐃinclude㊰ᚏ
AM_CFLAGS = -I../include
# 產⏕liba.a libb.b
# lib_௦⾲Ᏻ⿶せᨺᅾ$(prefix)/lib୰
# 㡸タprefix=/usr/local
lib_LIBRARIES = liba.a libb.a
72. 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
75. ● configure
● make
● make install
● make dist # ⮬ືᖳ఼ᡴໟtarball
85. ὶ⛬
● ᑃCMakeLists.txt
● ⏝⪅ᇳ⾜cmake
○ 產⏕ヱᖹྎᑞ᠕ⓗ⦅㆞⎔ቃ᷐ዴMakefile➼
● ⏝⪅ᇳ⾜ᖹྎୖⓗ⦅㆞᪉ἲ
○ ዴmake
● ⏝⪅ᇳ⾜cmake installᏳ⿶㌾㧓䚹
88. CMakeLists.txt
# タᐃㆰᩝ
set(SRC_DIR src)
set(LIB_DIR libs)
set(INC_DIR include)
# Compile flags
set(CMAKE_C_FLAGS "-Wall -Werror")
89. CMakeLists.txt
# ᣦ௧include┠㗴
include_directories(${INC_DIR})
# ࿌ッCMakeせཤୗิⓗ┠㗴⦅㆞
add_subdirectory(${SRC_DIR})
add_subdirectory(${LIB_DIR})
add_subdirectory(${INC_DIR})
92. src/CMakeLists.txt
# タᐃㆰᩝ
set(test_SRCS test.c)
# 產⏕ᇳ⾜᷐
add_executable(${PROJECT_NAME}
${test_SRCS})
# ᣦ௧linkภᘧᗜ
target_link_libraries(${PROJECT_NAME} a b)
104. ཨ⪃㈨ᩱ
● 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
105. ཨ⪃㈨ᩱ
● CMake Wiki
○ http://www.cmake.org/Wiki/CMake
● CMake-tutorial (᥎)
○ https://github.com/TheErk/CMake-tutorial