整体目录结构:
//hello.c
#include"myhead.h"
int main(void)
{
printf("hello world\n");
return 0;
}
include文件夹下放着
myhead.h文件,myhead.h文件的内容如下
#include<stdio.h>
makefile文件的内容如下:
hello:hello.o
gcc hello.o -o hello
hello.o:hello.c
gcc -c -I include hello.c -o hello.o
.PHONY:clean
clean:
rm *.o hello
编写不同目录下文件时的makefile文件时利用到了:
指令:-I dir指定被包含的makefile所在目录
//执行效果如下
二:单独把变量放在一个.mk文件中时的编写方式
#AB=-c -Wall
include config.mk
hello:hello.o
gcc hello.o -o hello
hello.o:hello.c
gcc $(AB) -I include hello.c -o hello.o
.PHONY:clean
clean:
rm *.o hello
#代表注释
config.mk的内容为:AB=-c -Wall
//执行效果如下
注:这种情况适合用在变量较多的情况下