Makefile的功用

本文详细介绍了使用Makefile进行项目构建的过程,包括创建源代码文件、编写Makefile规则、编译链接生成可执行文件,以及清理编译产物。通过具体实例,展示了如何将多个源文件(如hello.c和print.c)整合并编译成一个可执行程序。

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

新建一个makefile文件夹

创建hello.c

[root@localhost makefile]# vim hello.c   //创建hello.c文件


#include<stdio.h>
int main()
{
	print("helloword");
	return 0;
}

创建print.c

[root@localhost makefile]# vim print.c    //创建print.c文件


#include<stdio.h>
void print(char *s)
{
	printf("%s\n",s);
}

创建Makefile

    [root@localhost makefile]# vim Makefile   //创建Makefile文件
    
    
    Target=hello
    Object=hello.o print.o
    $(Target):$(Object)
    	gcc $(Object) -o $(Target)
    #hello.o:hello.c

    #	gcc -c hello.c -o hello.o
    #print.o:print.c
    #	gcc -c print.c -o print.o

#clean声明成尾目标
.PHONY : clean        
clean:
		rm *.o hello     //清除.o类文件和hello

编译print.c和hello.c链接hello

[root@localhost makefile]# make  //将hello.c与print.c编译成hello文件
cc    -c -o hello.o hello.c
cc    -c -o print.o print.c
[root@localhost makefile]# ls
hello  hello.c  hello.o  Makefile  print.c  print.o
[root@localhost makefile]# make clean    //清除.o文件和hello
rm *.o hello
[root@localhost makefile]# ls
hello.c  Makefile  print.c
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值