比如当前目录下有几个c 文件,怎么用makefile 去编译这几个文件。
第一个版本:
app:Main.c Server.c Client.c
gcc Main.c Server.c Client.c -o app
进化第二个版本:
pp:Main.o Server.o Client.o
gcc Main.o Server.o Client.o -o app
Main.o: Main.c
gcc -c Main.c
Server.o:Server.c
gcc -c Server.c
Client.o:Client.c
gcc -c Client.c
第三个版本;::
obj = Main.o Server.o Client.o
target = appDemo
$(target):$(obj)
gcc $(obj) -o $(target)
%.o:%.c
gcc -c $< -o $@
第四个版本:
obj = Main.o Server.o Client.o
target = appDemo
$(target):$(obj)
gcc $(obj) -o $(target)
%.o:%.c
gcc -c $< -o $@
第五个版本:
#obj = Main.o Server.o Client.o
# makefile 自带红函数,搜索c文件
src = $(wildcard ./*.c)
obj = $(patsubst ./%.c, ./%.o, $(src))
CC = gcc
CFLAGS = -I
target = appDemo
$(target):$(obj)
$(CC) $(obj) -o $(target)
%.o:%.c
$(CC) -c $< -o $@
.PHONEY:clean
clean:
rm $(obj) $(target) -f
start:
echo "Hello ,makefile start work!"