Linux Makefile的使用

来源:互联网 发布:mac识别不了u盘启动盘 编辑:程序博客网 时间:2024/06/05 20:30

1. 在home目录下新建make文件夹

2. 在make文件夹中新建以下程序文件:


main.c

#include"mytool1.h"#include"mytool2.h" intmain(void){mytool1_Print("Hello");mytool2_print("Hello");}

mytool1.c

/*mytool1.c*/#include"mytool1.h"#include<stdio.h> voidmytool1_Print(char * print_str){printf("This is mytool1 print %s\n",print_str);}


mytool1.h

/*mytool1.h*/#ifndef_MYTOOL1_H_#define_MYTOOL1_H_ voidmytool1_Print(char * printf_str); #endif

mytool2.c

/*mytool2.c*/#include"mytool2.h"#include<stdio.h> voidmytool2_print(char * print_str){printf("This is mytool2 print %s\n",print_str);}

mytool2.h

/*mytool2.h*/#ifndef_MYTOOL2_H_#define_MYTOOL2_H_ voidmytool2_print(char * printf_str); #endif

3. 手动编译上述文件,并运行查看结果:

gcc main.c mytool1.c mytool2.c –o PrintHello

运行结果为:


4. 根据代码分析其依赖关系,编写makefile文件:

CC=gcc -WallPrintHello:main.o mytool1.o mytool2.o    ${CC} main.o mytool1.o mytool2.o -oPrintHellomain.o:main.c mytool1.h mytool2.h    ${CC} -c main.c -o main.omytool1.o:mytool1.c mytool1.h    ${CC} -c mytool1.c -o mytool1.omytool2.o:mytool2.c mytool2.h    ${CC} -c mytool2.c -o mytool2.oclean:    rm *.o

 

上述${CC}是什么意思?clean是什么作用?

${CC}等价于gcc –Wall

—wall显示警告信息

Clean是删除所有的.o文件

5. 删除目录下的PrintHello,利用make重新生成PrintHello


6. 如何清理当前目录下所有的目标文件?

在clean中通过rm *.o删除所有的.o文件


原创粉丝点击