Makefile Examples
ตัวอย่างไฟล์ Makefile อย่างง่ายแบบที่ 1
all: hello
hello: main.o factorial.o hello.o
g++ main.o factorial.o hello.o -o hello
main.o: main.cpp
g++ -c main.cpp
factorial.o: factorial.cpp
g++ -c factorial.cpp
hello.o: hello.cpp
g++ -c hello.cpp
clean:
rm -rf *o helloตัวอย่างไฟล์ Makefile อย่างง่ายแบบที่ 2
ใช้ macro เพื่อสร้างตัวแปร CC และ CFLAGS
# the compiler to use
CC=g++
# options passed to the compiler
CFLAGS=-c -Wall
all: hello
hello: main.o factorial.o hello.o
$(CC) main.o factorial.o hello.o -o hello
main.o: main.cpp
$(CC) $(CFLAGS) main.cpp
factorial.o: factorial.cpp
$(CC) $(CFLAGS) factorial.cpp
hello.o: hello.cpp
$(CC) $(CFLAGS) hello.cpp
clean:
rm -rf *o helloตัวอย่างไฟล์ Makefile อย่างง่ายแบบที่ 3
สร้างตัวแปร SOURCES, OBJECTS และ EXECUTABLE เพื่อรวมไฟล์โปรแกรมเข้าด้วยกัน
ตัวอย่างไฟล์ Makefile อย่างง่ายแบบที่ 4
ในกรณีที่มีการอ้างอิง (include) ไฟล์ header (.h) เพิ่มหรือไลบรารีภายนอกเพิ่มเติม จะต้องใส่ option -I และ -l เพิ่มเข้าไป
ตัวอย่างโปรแกรม
สร้างไฟล์โปรแกรมจำนวน 3 ไฟล์ ได้แก่
hello.cpp
function.h
main.cpp
หลังจากนั้นให้สร้างไฟล์ Makefile ดังนี้
หลังจากนั้นให้ทำการรันด้วยคำสั่งดังนี้
Last updated
Was this helpful?