前言
操作系统:Fedora
本文为学习笔记,编写了一个简单的 Makefile 文件
准备工作
有如下程序:main.c hello1.h hello2.h hello1.c hello2.c 其中 hello1.h 文件定义了 hello1 的原型,hello2.h 定义了 hello2 的原型,hello1.c 文件实现了 hello1 的定义,hello2.c 定义了 hello2 的定义,main.c 程序是主程序,调用了 hello1 及 hello2
(1)hello1.h 的文件内容如下
/*hello1.h*/
#ifndef _HELLO_1_H //预编译命令,防止多次包含头文件
#define _HELLO_1_H
void hello1(char *mess); //打印mess的内容
#endif
(2)hello2.h 的文件内容如下
/*hello2.h*/
#ifndef _HELLO_2_H
#define _HELLO_2_H
void hello2(char *mess);
#endif
(3)hello1.c 的文件内容如下
#include "hello1.h"
void hello1(char *mess)
{
printf("This is hello1 print %\n",mess);
}
(4)hello2.c 的文件内容如下
#include "hello2.h"
void hello2(char *mess)
{
printf("This is hello2 print %\n",mess);
}
(5)main.c 的程序内容如下
/*main.c*/
#include "hello1.h"
#include "hello2.h"
int main(int argc, char **argv)
{
hello1("hello");
hello2("world");
return 0;
}
编写Makefile 文件
(6)编写Makefile 文件。文件名为 Makefile
#Makefile
main:main.o hello1.o hello2.o
gcc -o main main.o hello1.o hello2.o
main.o:main.c hello1.h hello2.h
gcc -c main.c
hello1.o:hello1.c hello1.h
gcc -c hello1.c
hello2.o:hello2.c hello2.h
gcc -c hello2.c
clean:
rm main hello1.o hello2.o main.o
(7)将前述所有文件放入同一文件夹中,执行 make 命令,Make 工具自动从当前目录开始查找名为 Makefile 的文件,然后根据其中的指令进行编译,最后生成一个与 main 同名的可执行文件。
……(不完全显示)
(8)输入 ./main 执行可执行程序
(9)make clean 清除以前的make命令编译后所产生的object文件(即后缀为“.o”的文件)及其生成的可执行文件