Linux程序员必须学会使用GNUmake来构建和管理自己的软件工程。GNU 的make能够使整个软件工程的编译、链接只需要一个命令就可以完成。 Makefile文件描述了整个工程的编译,连接等规则
targets
:
prerequisites
(Tab)command
目标
依赖
(Tab) 命令
例如:main.o : main.c
gcc –c main.c
Makefile中把那些
没有任何依赖只有执行
动作的目标称为“伪目标”(phony targets)。
在makefile中,存在
系统默认的
自动化变量
$^:代表所有的依赖文件
$@:代表目标
$<:代表第一个依赖文件
Makefile中“#”字符后的内容被视作注释。
例子:
fun.c
#include "fun.h"
int max_fun(int x,int y)
{
if(x>=y)
return x;
else
return y;
}
fun.h
#include <stdio.h>
extern int max_fun(int x, int y);
main.c
#include "fun.h"
int main(void)
{
int a,b;
printf("Please enter the number a and b\n");
scanf("%d%d",&a,&b);
int max=0;
max=max_fun(a,b);
printf("The max number is %d\n",max);
return 0;
}
makefile
OBJS=main.o fun.o
CC=gcc
CFLAGS=-c
main:$(OBJS)
$(CC) $^ -o $@
%.o:%.c
$(CC) $(CFLAGS) $< -o $@
clean:
rm -f main *.o
结果
[root@localhost usemakefile]# make
gcc main.o fun.o -o main
[root@localhost usemakefile]# ./main
Please enter the number a and b
12
44
The max number is 44