实例学习编写Makefile

1.test1.c包含主函数main的文件:

#include <stdio.h>

int main()
{
	test2_func();

	test3_func();
}

2A../test2/test2.c目录下子文件:

#include <stdio.h>
#include "test2.h"

int test2_func(void)
{
	printf("this is test2_func function %d ... \n",TEST_NUM);
}
2B../test2/test2.h :

#define TEST_NUM	3

3A../test/test3.c: 目录下子文件:

#include "test3.h"

int test3_func(void)
{
	printf("this is test2_func function %d ... \n",TEST_NUM3);

}
3B../test3/test3.h :

#include <stdio.h>

#define TEST_NUM3 5


现在代码已经写好,我们开始编写Makefile文件。就像我同事说的,在每个文件夹下编写一个Makefile,这样更便于管理。

首先我们编写子目录下的Makefile:

1. test2 目录下的Makefile文件 :

CC = gcc
TARGET = test2.o

$(TARGET):
	$(CC) -o $(TARGET)

2. test3 目录下的Makefile文件 :
CC = gcc
TARGET = test3.o

$(TARGET):
	$(CC) -o $(TARGET)

3.更目录下的Makefile文件 :

CC = gcc
TARGET = test

XSYSOBJ = test2/test2.o
XSYSOBJ += test3/test3.o

APPOBJ = test1.o


$(TARGET): $(APPOBJ) $(XSYSOBJ)
	$(CC) -o $(TARGET) $(APPOBJ) $(XSYSOBJ)

OK,现在我们的Makefile文件已经写好,make一下,显示:

[root@localhost test_makefile]# make
make: Warning: File `makefile' has modification time 35 s in the future
gcc -o test test1.o test2/test2.o test3/test3.o

然后我们运行一下:

[root@localhost test_makefile]# ./test
this is test2_func function 3 ... 
this is test2_func function 5 ... 

嗯,我们的Makefile成功啦 。。。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值