写了三个文件,main.c hello.c hello.h

main.c调用hello.h中的函数
hello.h的声明函数 保存在hello.c中
所以需要编译hello.o <-- hello.h hello.c
在编译main.o <-- main.c

然后在利用main.o 和 hello.o编译成目标文件hello
执行程序./hello
#版本1****************************************************

#Makefile
#最终文件hello, 执行./hello
hello : main.o hello.o
#编译hello.o
hello.o : hello.h hello.c
#编译main.o
main.o : main.c

#删除文件
clean:
rm -rf hello.o main.o

#版本2********************************************************

#注意:在命令前@,表示执行静默运行该命令
#最终文件hello, 执行./hello
#定义声明
#$(shell clear)
//定义全局变量 调用格式: $(objects)
objects=main.o hello.o
//使用shell命令 格式: $(shell pwd)
pwd=$(shell pwd)
hello:$(objects)
hello.o:hello.h hello.c
main.o:main.c hello.h

.PHONY:clean
clean:
#rm hello
#-rm $(objects) hello Makefile.log
-rm -rf *.o
-rm Makefile.log

pwd:
@echo $(pwd)

注意:在Makefile中不能使用中文,最好下面也不要有注释,如下

#最终文件hello, 执行./hello
#定义声明
#$(shell clear)
objects=main.o hello.o
pwd=$(shell pwd)

hello:$(objects)
hello.o:hello.h hello.c
main.o:main.c hello.h


.PHONY:clean
clean:
#rm hello
#-rm $(objects) hello Makefile.log
-rm -rf *.o
-rm Makefile.log

pwd:
@echo $(pwd)

注意:命令要在编译命令以后↓

#最终文件hello, 执行./hello
#定义声明
#
objects=main.o hello.o
HELLO_=hello.h hello.c
pwd=$(shell pwd)

# start
hello:$(objects)
hello.o:$(HELLO_)
main.o:main.c

.PHONY:clean
cleanall:
-rm -rf *.o
-rm log hello

clean_objects:
-rm -rf *.o

pwd:
@echo $(pwd)
pwd > log

shell文件调用makefile文件

#!/bin/bash

make
make clean_objects

最后执行shell文件调用
./shell.sh