新建一个makefile文件夹
创建hello.c
[root@localhost makefile]# vim hello.c //创建hello.c文件
#include<stdio.h>
int main()
{
print("helloword");
return 0;
}
创建print.c
[root@localhost makefile]# vim print.c //创建print.c文件
#include<stdio.h>
void print(char *s)
{
printf("%s\n",s);
}
创建Makefile
[root@localhost makefile]# vim Makefile //创建Makefile文件
Target=hello
Object=hello.o print.o
$(Target):$(Object)
gcc $(Object) -o $(Target)
#hello.o:hello.c
# gcc -c hello.c -o hello.o
#print.o:print.c
# gcc -c print.c -o print.o
#clean声明成尾目标
.PHONY : clean
clean:
rm *.o hello //清除.o类文件和hello
编译print.c和hello.c链接hello
[root@localhost makefile]# make //将hello.c与print.c编译成hello文件
cc -c -o hello.o hello.c
cc -c -o print.o print.c
[root@localhost makefile]# ls
hello hello.c hello.o Makefile print.c print.o
[root@localhost makefile]# make clean //清除.o文件和hello
rm *.o hello
[root@localhost makefile]# ls
hello.c Makefile print.c