make编译多c文件,bash命令执行

先准备两个函数printA、printB:

//a.h
#ifndef __A_H
#define __A_H
#include<stdlib.h>
#include<stdio.h>

void printA();

#endif
//a.c
#include"a.h"
void printA()
{
    printf("print A from printA!\n");
}
//b.h
#ifndef __B_H
#define __B_H
#include<stdlib.h>
#include<stdio.h>

void printB();

#endif
//b.c
#include"b.h"
void printB()
{
    printf("print B from printB!\n");
}

然后是main函数:

//main.c
#include<stdio.h>
#include<stdlib.h>
#include"a.h"
#include"b.h"

int main()
{
    printf("print from main!\n");
    printA();
    printB();
    return 0;
}

然后编写Makefile:

all: test

test: main.o a.o b.o
	gcc -o test main.o a.o b.o

main.o: main.c a.h b.h
	gcc -c main.c

a.o: a.c a.h
	gcc -c a.c

b.o: b.c b.h
	gcc -c b.c

.PHONY: clean
clean: 
	rm *.o test

然后使用脚本启动:

使用bash脚本启动make,并执行生成的文件,此处-s可以禁止编译信息打印:

make -s
./test

最终的输出:

jay@Catttt:~/C-code$ ./test.sh
print from main!
print A from printA!
print B from printB!
jay@Catttt:~/C-code$ 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值