一步一步写Makefile(2):shell命令,通配符*和%区别,文件路径搜索vpath VPATH,makefilevpath



#################6:Makefile中也可以插入shell命令################

variable=main.o test1.o test2.o

main:$(variable)

        cc -o main $(variable)

main.o:test1.o test2.o test1.h

 

clean:

        rm *.o main     #在Makefile中使用*通配符

print:

        cat main.c     #cat命令就是将文件内容显示到终端,在控制台也可以使用这个命令

        ls -al         #这条命令也可以自行,可知在Makefile中可以执行命令

#rm $(variable) main 正确

 

#################7:在Makefile中使用通配符* 和% ################

variable=main.o test1.o test2.o

main:$(variable)

        cc -o main $(variable)

       

#main.o:test1.o test2.o test1.h

%.o:test1.h

 

clean:

        rm *.o main     #在Makefile中使用*通配符

 

 

 

#可知:通配符%是make语法层的,如用在一条make的规则里,目标依赖命令%.o:%.c;cc %.c

#      再如:$(patsubst%.c,%.o,$(wildcard *.c))

#   通配符*是用在shell语法层的,就是在控制终端可以输入的命令,如rm *.o main

#   记忆方法,记住%,反面就是*

#################8:在Makefile中使用文件路径搜索vpath VPATH################
variable=main.o test1.o test2.o
#VPATH =./folder: ../header
vpath %.c ./folder
vpath %.h ../header
#注意这些路径都是基于当前源文件编译时的路径,如在编译main.c时它包含了test1.h头文件,而test1.h
#头文件的路径在main.c文件所在路径的上层目录下的header文件夹下面。所以是vpath %.h ../header而#不是vpath %.h ./header
main:$(variable)
        cc -o main $(variable)
        
#main.o:test1.o test2.o test1.h
%.o:test1.h
 
clean:
        rm *.o main     #在Makefile中使用*通配符
#include<stdio.h>
//#include "test1.h"
//#include "test1.h"   //多次包含也不会出错
int test2(void);
int main(void)
{
        printf("main.c\n");
        test1();
        test2();
}


 


 #vpath是过滤筛选,不同类型的文件到不同路径下查找,VPATH是统一对待,所有类型的文件都到一个制定

##路径下搜索

 

 

查看评论

相关内容

    暂无相关文章