Makefile 教程

来源:互联网 发布:pubmed数据库主题词 编辑:程序博客网 时间:2024/04/27 13:34
Makefile 教程

一、先来个简单实用的入门实例

两个源文件 helloworld.c 和 test.c :
$ ls
helloworld.c  test.c

源代码:

helloworld.c

#include<stdio.h>void printHelloWorld(){printf("Hello World.\n");return;}
test.c

#include<stdio.h>extern printHelloWorld();void main(){printHelloWorld();return;}


1. 编写 Makefile 文件
在源文件目录下编写Makefile文件,内容如下:
test: test.c helloworld.c
@gcc $^ -o $@
.PHONY : clean
clean :
@rm -f test *.o

注意:第2行和第5行前要打个 [Tab] 键。

【说明】
$^:代表所有依赖文件
$@:代表目标

.PHONY:指名一个目标是伪目标

伪目标clean把编译生成的可执行文件test和.o文件删除。


保存文件,目录内容:
$ ls
helloworld.c  Makefile  test.c

2. 执行 make 编译源代码
$ make
$ ls
helloworld.c  Makefile  test  test.c
生成了可执行文件test。

3. 运行
$ ./test
Hello World.
结果正确。

4. 执行 make clean 删除编译生成的目标文件
$ ls
helloworld.c  Makefile  test  test.c
$ make clean
$ ls
helloworld.c  Makefile  test.c
删除了test。



0 0
原创粉丝点击