福州网站排名推广,宜宾网站建设公司,网站开发外包报价单,如何进行网站调试一.make中的路径搜索
问题#xff1a;在实际的工程项目中#xff0c;所有的源文件和头文件都放在同一个文件夹中吗#xff1f; 实验1 #xff1a; VPATH 引子
mhrubuntu:~/work/makefile1/17$ ll
total 28
drwxrwxr-x 4 mhr mhr 4096 Apr 22 00:46 ./
drwxrwxr-x 7 mhr m…一.make中的路径搜索
问题在实际的工程项目中所有的源文件和头文件都放在同一个文件夹中吗 实验1 VPATH 引子
mhrubuntu:~/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/
mhrubuntu:~/work/makefile1/17$ inc/func.h#include stdio.h
#include func.hvoid foo()
{printf(void foo() : %s\n, This file is from inc ...);
}src/func.c#include stdio.h
#include func.hvoid foo()
{printf(void foo() : %s\n, HELLO);
}src/main.c#include stdio.h
#include func.hint main()
{foo();return 0;
} makefileOBJS : func.o main.ohello.out : $(OBJS)gcc -o $ $^echo Target File $$(OBJS) : %.o : %.c func.hgcc -o $ -c $^mhrubuntu:~/work/makefile1/17$
mhrubuntu:~/work/makefile1/17$ make
make: *** No rule to make target func.c, needed by func.o. Stop.
mhrubuntu:~/work/makefile1/17$ 当前makefile: 要创建的可执行程序 hello.out 依赖于 func.o main.o 两个目标文件当前目录下搜索不到这两个文件这两个目标文件是通过 规则中的模式替换
$(OBJS) : %.o : %.c func.hgcc -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.hgcc -o $ -c $mhrubuntu:~/work/makefile1/17$
mhrubuntu:~/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
mhrubuntu:~/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.hvoid 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)mhrubuntu:~/work/makefile1/17$ ./hello.out
void foo() : This file is from inc ...
mhrubuntu:~/work/makefile1/17$ VPATH 变量告诉make解释器当在当前目录找不到需要的文件的时候 首先到 inc文件夹中找如果找不到 再去src文件夹中去找。如果在inc文件夹中找到了需要的文件那么就不会继续去src文件夹中寻找了。但是inc 中的文件是我们手误放进去的所以不是我们想要的结果那么这种情况怎么办呢 即说明 .h 头文件在 inc文件夹中去找 .c文件到 src文件夹中去找
实验5vpath(小写) 初探
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.hgcc $(CFLAGS) -o $ -c $mhrubuntu:~/work/makefile1/17$
mhrubuntu:~/work/makefile1/17$ ./hello.out
void foo() : Hello D.T.
mhrubuntu:~/work/makefile1/17$