Linux编程基础之编写一个简单的 Makefile 文件


前言

操作系统: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”的文件)及其生成的可执行文件
在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

huazi-J

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值