简单的Makefile实例

本文介绍了一个使用Makefile进行编译的实例,包含四个源文件:test.h、test.c、display.c和Max.c。通过创建Makefile文件并定义编译规则,实现了一次性编译与执行的功能。文中详细展示了Makefile的内容及如何执行编译和清理操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 makefile主要方便一次性编译与执行

实例:有四个文件:test.h,test.c,display.c,Max.c,

/*************************/

test.h:

#include <stdio.h>
#include <dlfcn.h>

int Max(int a, int b);
int display();

 

/***********************/

test.c:

#include "test.h"
int main()
{
 int ret = 0;
 ret = Max(1, 10);
 printf("Max is %d\n",ret); 
 ret = display();
 return 0;
}

/***************************/

display.c:

#include "test.h"

int display()
{
 printf("Hello world\n");
 return 0;
}

/*******************************/

Max.c:

#include "test.h"

int Max(int a, int b)
{
 return a > b ? a:b ;
}

 

创建Makefile文件:

Makefile:

make:test.o display.o Max.o

           gcc test.o display.o Max.o -o make

test.o:test.h test.c

           gcc -c test.c -o test.o

display.o:test.h display.o

           gcc -c display.c -o display.o

Max.o:test.h Max.c

           gcc -c Max.c -o Max.o

clean:

         rm -rf *.o make

现在可以执行make了,执行完后,会多.o和make的文件,直接./make就可以运行,得到执行结果

输入make clean可以清除.o与make文件

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值