makefile编写不同目录下的文件的方法

整体目录结构:
在这里插入图片描述
//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

//执行效果如下
在这里插入图片描述
注:这种情况适合用在变量较多的情况下

发布了151 篇原创文章 · 获赞 81 · 访问量 6622

猜你喜欢

转载自blog.csdn.net/qq_38158479/article/details/104310439
今日推荐