当前位置: 首页 > news >正文

Makefile编译原理 make 中的路径搜索_1

一.make中的路径搜索

问题:在实际的工程项目中,所有的源文件和头文件都放在同一个文件夹中吗?

实验1 : VPATH 引子

mhr@ubuntu:~/work/makefile1/17$ ll
total 28
drwxrwxr-x 4 mhr mhr 4096 Apr 22 00:46 ./
drwxrwxr-x 7 mhr mhr 4096 Apr 22 00:32 ../
drwxrwxrwx 2 mhr mhr 4096 Jan 23  2018 inc/
-rw-rw-r-- 1 mhr mhr  133 Apr 22 00:46 makefile
drwxrwxrwx 2 mhr mhr 4096 Jan 23  2018 src/
mhr@ubuntu:~/work/makefile1/17$ inc/func.h#include <stdio.h>
#include "func.h"void foo()
{printf("void foo() : %s\n", "This file is from inc ...");
}src/func.c#include <stdio.h>
#include "func.h"void foo()
{printf("void foo() : %s\n", HELLO);
}src/main.c#include <stdio.h>
#include "func.h"int main()
{foo();return 0;
}	makefileOBJS := func.o main.ohello.out : $(OBJS)@gcc -o $@ $^@echo "Target File ==> $@"$(OBJS) : %.o : %.c func.h@gcc -o $@ -c $^mhr@ubuntu:~/work/makefile1/17$ 
mhr@ubuntu:~/work/makefile1/17$ make
make: *** No rule to make target 'func.c', needed by 'func.o'.  Stop.
mhr@ubuntu:~/work/makefile1/17$ 

当前makefile: 要创建的可执行程序 hello.out 依赖于 func.o main.o 两个目标文件(当前目录下搜索不到这两个文件),这两个目标文件是通过 规则中的模式替换:

$(OBJS) : %.o : %.c func.h@gcc -o $@ -c $^

解决办法: 

make对VPATH值的处理方式

- 当前文件夹找不到需要的文件时,VPATH就会被使用

- make会在VPATH指定的文件夹中依次搜索文件

- 当多个文件夹存在同名文件时,选择第一次搜索到的文件

实验2 VPATH 初体验, VPATH 只能决定 make 的搜索路径,无法决定命令的搜索路径

OBJS := func.o main.oVPATH := inc srchello.out : $(OBJS)@gcc -o $@ $^@echo "Target File ==> $@"$(OBJS) : %.o : %.c func.h@gcc -o $@ -c $<mhr@ubuntu:~/work/makefile1/17$ 
mhr@ubuntu:~/work/makefile1/17$ make
src/main.c:2:18: fatal error: func.h: No such file or directory#include "func.h"^
compilation terminated.
makefile:11: recipe for target 'main.o' failed
make: *** [main.o] Error 1
mhr@ubuntu:~/work/makefile1/17$ 

显然,这次没有提示: make: *** No rule to make target ‘func.c’, needed by ‘func.o’. Stop.的错误,说明 maka找到了 需要的源文件,即

$(OBJS) : %.o : %.c func.h
找到了 需要的源文件,但是该规则的命令却执行失败。

原因:VPATH 只能决定 make 的搜路径,无法决定命令的搜索路径,他对命令没有任何作用,上面得到的编译错误是因为 gcc 在编译.c文件的时候找不到对应的头文件。

实验3:指定编译命令gcc 需要的文件的搜索路径

OBJS := func.o main.oINC := inc
SRC := src
VPATH := $(INC) $(SRC)
CFLAGS := -I $(INC)hello.out : $(OBJS)@gcc -o $@ $^@echo "Target File ==> $@"$(OBJS) : %.o : %.c func.h  // 为 make 提供搜索路径 :VPATH := $(INC) $(SRC)@gcc $(CFLAGS) -o $@ -c $<  //为 gcc 命令 提供搜索路径,搜索 func.h:-I $(INC)

$(OBJS) : %.o : %.c func.h // 为 make 解释器提供搜索路径 :VPATH := $(INC) $(SRC) ,这样make解释器就得到了需要的文件,并根据规则中的模式替换,生成了最后的规则。

实验4:当多个文件夹存在同名文件时,选择第一次搜索到的文件

假如 inc 头文件文件夹中由于手误也存入了一个 func.c 那么会发生什么

inc/ func.h func.c
这里的func.c 内容如下:

#include <stdio.h>
#include "func.h"void foo()
{printf("void foo() : %s\n", "This file is from inc ...");
}makefile:OBJS := func.o main.oINC := inc
SRC := src
VPATH := $(INC) $(SRC)
CFLAGS := -I $(INC)hello.out : $(OBJS)@gcc -o $@ $^@echo "Target File ==> $@"$(OBJS) : %.o : %.c func.h  // 为 make 提供搜索路径 :VPATH := $(INC) $(SRC)@gcc $(CFLAGS) -o $@ -c $<  //为 gcc 命令 提供搜索路径,搜索 func.h:-I $(INC)mhr@ubuntu:~/work/makefile1/17$ ./hello.out 
void foo() : This file is from inc ...
mhr@ubuntu:~/work/makefile1/17$ 

VPATH 变量告诉make解释器,当在当前目录找不到需要的文件的时候 首先到 inc文件夹中找,如果找不到 再去src文件夹中去找。如果在inc文件夹中找到了需要的文件,那么就不会继续去src文件夹中寻找了。但是inc 中的文件是我们手误放进去的,所以不是我们想要的结果,那么这种情况怎么办呢?

即说明 .h 头文件在 inc文件夹中去找; .c文件到 src文件夹中去找

实验5:vpath(小写) 初探

OBJS := func.o main.o
INC := inc
SRC := src
CFLAGS := -I $(INC)vpath %.h $(INC)
vpath %.c $(SRC)hello.out : $(OBJS)@gcc -o $@ $^@echo "Target File ==> $@"$(OBJS) : %.o : %.c func.h@gcc $(CFLAGS) -o $@ -c $<mhr@ubuntu:~/work/makefile1/17$ 
mhr@ubuntu:~/work/makefile1/17$ ./hello.out 
void foo() : Hello D.T.
mhr@ubuntu:~/work/makefile1/17$ 

http://www.lryc.cn/news/298445.html

相关文章:

  • 蓝桥杯每日一题------背包问题(一)
  • 面试 JavaScript 框架八股文十问十答第八期
  • 【机器学习】单变量线性回归
  • 《计算思维导论》笔记:10.4 关系模型-关系运算
  • QT+OSG/osgEarth编译之八十四:osgdb_osg+Qt编译(一套代码、一套框架,跨平台编译,版本:OSG-3.6.5插件库osgdb_osg)
  • 【Redis快速入门】初识Redis、Redis安装、图形化界面
  • Linux(Ubuntu) 环境搭建:Nginx
  • 快速手动完成 VS 编写脚本自动化:如何选取最高效的工作方式?
  • FAST角点检测算法
  • Python中使用opencv-python进行人脸检测
  • 牛客网 DP3跳台阶扩展问题
  • ARM汇编[1] 打印格式化字符串(printf
  • Java 集合、迭代器
  • 在 Docker 中启动 ROS2 里的 rivz2 和 rqt 出现错误的解决方法
  • 使用securecrt+xming通过x11访问ubuntu可视化程序
  • 红队打靶练习:HEALTHCARE: 1
  • Java IO:概念和分类总结
  • 【Linux】基本命令(下)
  • 腾讯云游戏联机服务器配置价格表,4核16G/8核32G/4核32G/16核64G
  • 面试经典150题——长度最小的子数组
  • 业务流程
  • ChatGPT Plus如何升级?信用卡付款失败怎么办?如何使用信用卡升级 ChatGPT Plus?
  • Spring 如何配置 bean (XML 方式)
  • 揭秘外观模式:简化复杂系统的关键设计策略
  • Nginx 命令(Ubuntu)
  • 从github上拉取项目到pycharm中
  • python从入门到精通(十八):python爬虫的练习案列集合
  • 2.12作业
  • 树莓派4B(Raspberry Pi 4B) 使用docker搭建单机版nacos
  • C++入门学习(二十七)跳转语句—continue语句