Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 20

Lập trình C trên Linux

V. Makefile trong hệ thống Linux:


• 1/ Công cụ GNU Make
• 2/Makefile
• 3/Các quy tắc
• 4/Ví dụ
V. Makefile trong hệ thống Linux
• 1/ Công cụ GNU Make:
– Vì sao GNU make được đưa ra ?
– Giải pháp của GNU make:
V. Makefile trong hệ thống Linux
• 2/Makefile:
– Khái niệm makefile:
Makefile là một cơ sở dữ liệu văn bản chứa các
quy tắc(rules), các quy tắc này sẽ báo cho chương
trình make biết phải làm gì và làm như thế nào
– Các thành phần của rules:
• Target
• Dependency
• Command
V. Makefile trong hệ thống Linux
• 3/Các quy tắc(rules):
Cú pháp:
Target: dependency1 dependency2 …
Command
Command
……….......
V. Makefile trong hệ thống Linux
• Chú ý :
– Ký tự đầu tiên trong command phải là ký tự Tab,
không được dùng Space, nếu dùng Space thì Make
sẽ hiển thị thông báo “Missing separator” và dừng
lại.
– Khi một dòng lệnh trở nên quá dài, có thể cắt ra
bằng 1 hay nhiều dấu "\"
– Chú thích bắt đầu bằng dấu #, bắt đầu từ dấu này
tới hết dòng sẽ bị make bỏ qua
V. Makefile trong hệ thống Linux
• Ví dụ 1:
editor : editor.o screen.o keyboard.o
gcc –o editor.o screen.o keyboard.o
editor.o : editor.c editor.h keyboard.h screen.h
gcc –c editor.c
screen.o : screen.c screen.h
gcc –c screen.c
keyboard.o : keyboard.c keyboard,h
gcc –c keyboard.c
clean :
rm editor *.o
V. Makefile trong hệ thống Linux
• Một số quy tắc(rules) khác:
V. Makefile trong hệ thống Linux
• Một số quy tắc khác:
– PHONY target
V. Makefile trong hệ thống Linux
• Ví dụ 2:
editor : editor.o screen.o keyboard.o
gcc –o editor.o screen.o keyboard.o
editor.o : editor.c editor.h keyboard.h screen.h
gcc –c editor.c
screen.o : screen.c screen.h
gcc –c screen.c
keyboard.o : keyboard.c keyboard,h
gcc –c keyboard.c
PHONY : clean
clean :
rm editor *.o
V. Makefile trong hệ thống Linux
• Một số khái niệm khác:
– PHONY target
– Biến
V. Makefile trong hệ thống Linux
– Khai báo biến:
VARNAME= some_text []
– Gán giá trị cho biến (VARNAME):
$(VARNAME)
V. Makefile trong hệ thống Linux
• Ví dụ 3:
OBJS = editor.o screen.o keyboard.o
HDRS= editor.h screen.h keyboard.h
editor : $(OBJS)
gcc –o editor $(OBJS)
editor.o : editor.c $(HDRS)
gcc –c editor.c
screen.o : screen.c screen.h
gcc –c screen.c
keyboard.o : keyboard.c keyboard,h
gcc –c keyboard.c
PHONY : clean
clean :
rm editor $(OBJS)
V. Makefile trong hệ thống Linux
• Một số khái niệm khác:
– PHONY target
– Biến
– Biến tự động
V. Makefile trong hệ thống Linux
 Một số biến tự động:
Biến Mô tả 
$@ The filename of a rule’s target
$< The name of the first dependency in a rule
$^ Space-delimited list of all the dependencies in a rule
$? Space-delimited list of all the dependencies in a rule that are
newer than the target
$(@D) The directory part of a target filename, if the target is in a
subdirectory
$(@F) The filename part of a target filename, if the target is in a
subdirectory
V. Makefile trong hệ thống Linux
• Một số khái niệm khác:
– PHONY target
– Biến
– Biến tự động
– Biến xác định trước và cờ
V. Makefile trong hệ thống Linux
Một số biến xác định trước và cờ (flags)

Variable Description
 
AR Archive-maintenance programs; default value = ar
AS Program to do assembly; default value = as
CC Program for compiling C programs; default value = cc
CPP C Preprocessor program; default value = cpp
RM Program to remove files; default value = “rm -f”
ARFLAGS Flags for the archive-maintenance program; default = rv
ASFLAGS Flags for the assembler program; no default
CFLAGS Flags for the C compiler; no default
CPPFLAGS Flags for the C preprocessor; no default
LDFLAGS Flags for the linker (ld); no default
V. Makefile trong hệ thống Linux
• Một số khái niệm khác:
– PHONY target
– Biến
– Biến tự động
– Biến xác định trước và cờ
– Biến môi trường
V. Makefile trong hệ thống Linux
• Debugging Make :
– Sử dụng tùy chọn –d cho make in ra nhiều thông
tin gỡ rối ngoài các lệnh đã thực hiện
V. Makefile trong hệ thống Linux
• 4/ Ví dụ:
V. Makefile trong hệ thống Linux
• Nguồn :
https://www.gnu.org/software/make/manual/
make.html

You might also like