======================================================
makefile的编写:
makefile的命名:全小写或者第一个字母大写(Makefile)
makefile的规则(三要素):
目标,依赖,命令
app:main.c add.c sub.c mul.c
gcc main.c add.c sub.c mul.c -o app
make
makefile的编写:
makefile的命名:全小写或者第一个字母大写(Makefile)
makefile的规则(三要素):
目标,依赖,命令
app:main.c add.c sub.c mul.c
gcc main.c add.c sub.c mul.c -o app
make
======================================================
分开编译:(向下查找规则)
app:main.o add.o sub.o mul.o
gcc main.o add.o sub.o mul.o -o app
main.o:main.c
gcc -c main.c
add.o:add.c
gcc -c add.c
mul.o:mul.c
gcc -c mul.o
sub.o:sub.c
gcc -c sub.c
make
======================================================
app:main.o add.o sub.o mul.o
gcc main.o add.o sub.o mul.o -o app
main.o:main.c
gcc -c main.c
add.o:add.c
gcc -c add.c
mul.o:mul.c
gcc -c mul.o
sub.o:sub.c
gcc -c sub.c
make
======================================================
内部更新机制,通过文件时间就行更新
======================================================
======================================================
//上面很多冗余,需要修改规则
//变量,赋值($取值) :3,4s/app/$(target)
//模式规则
//app:main.o sub.o mul.o--->%.o:%.c
//gcc main.o sub.o mul.o -o app
//makefile中的自动变量
//$< $@
//$< 规则中的第一个依赖
//$< 规则中的目标
//$^规则中的所有依赖
//只能在规则中的命令中使用
//makefile中自己维护的变量
//CC
//变量,赋值($取值) :3,4s/app/$(target)
//模式规则
//app:main.o sub.o mul.o--->%.o:%.c
//gcc main.o sub.o mul.o -o app
//makefile中的自动变量
//$< $@
//$< 规则中的第一个依赖
//$< 规则中的目标
//$^规则中的所有依赖
//只能在规则中的命令中使用
//makefile中自己维护的变量
//CC
obj=main.o add.o sub.o mul.o
target=app
$(target):$(obj)
gcc $(obj) -o $(target)
target=app
$(target):$(obj)
gcc $(obj) -o $(target)
%.o:%.c
gcc -c $< -o $@
obj=main.o add.o sub.o mul.o
target=app
CC = gcc
CPPFLAGS = - I
$(target):$(obj)
$(CC) $(obj) -o $(target)
gcc -c $< -o $@
obj=main.o add.o sub.o mul.o
target=app
CC = gcc
CPPFLAGS = - I
$(target):$(obj)
$(CC) $(obj) -o $(target)
%.o:%.c
$(CC) -c $< -o $@
make
$(CC) -c $< -o $@
make
======================================================
#makefile中的函数
#所有makefile中的系统函数都是有返回值的
#makefile中的函数
#所有makefile中的系统函数都是有返回值的
#obj=main.o add.o sub.o mul.o
target=app
src=$(wildcard ./*.c)
obj=$(patsubst ./%.c, ./%.o, $(src))
CC = gcc
CPPFLAGS = - I
$(target):$(obj)
$(CC) $(obj) -o $(target)
target=app
src=$(wildcard ./*.c)
obj=$(patsubst ./%.c, ./%.o, $(src))
CC = gcc
CPPFLAGS = - I
$(target):$(obj)
$(CC) $(obj) -o $(target)
%.o:%.c
$(CC) -c $< -o $@
$(CC) -c $< -o $@
.PHONY:clean
clean:
rm $(obj) $(target) -f
clean:
rm $(obj) $(target) -f
hello:
echo "hello makefile"
echo "hello makefile"
make clean
======================================================
======================================================
转自:http://www.bubuko.com/infodetail-3036749.html