makefile 的意义
依赖的定义
targets:prerequisites;command1
‘\t‘ command2
makefile 中的元素含义
targets
prerequisities
command
注意事项
targets 可以包含多个目标
prerequisites 可以包含多个依赖
[Tab] 键:\t
续行符:\
makefile 中可以在命令前加上 @
,使得命令无回显
依赖示例
all:test
echo "make all"
test:
echo "make test"
依赖规则
示例:利用 main.c 和 func.c 源文件生成 hello.out 可执行文件
源代码文件
//func.c
#include <stdio.h>
void foo(){
printf("print void foo()");
}
//main.c
#include <stdio.h>
extern void foo();
int main()
{
foo();
return 0;
}
makefile 文件
hello.out:main.o func.o
gcc -o hello.out main.o func.o
main.o:main.c
gcc -o main.o -c main.c
func.o:func.c
gcc -o func.o -c func.c
执行 make,输出:
gcc -o func.o -c func.c
gcc -o main.o -c main.c
gcc -o hello.out func.o main.o
修改 func.c 文件 => 当依赖在时间上比目标更新,执行对应命令
//func.c
#include <stdio.h>
void foo(){
printf("print void foo()\n");
}
再次执行 make,输出:
gcc -o func.o -c func.c
gcc -o hello.out func.o main.o
原文:https://www.cnblogs.com/bky-hbq/p/13197748.html