linux makefile 课后练习

本文档内容:
1,验证在makefile文件中是否一定需要指定.h文件(头文件)?
2,遍历子目录,并且在子目录执行make命令。
3,执行make clean时,遍历子目录执行make clean命令。
4,使用makefile生成动态库和静态库。

测试用例:
twohellos.c 文件中有main函数,main 函数调用hellofirst.c和hellosecond.c中的函数hellofirst() 和 hellosecond();
其中hellofirst.h和hellosecond.h是其对应的头文件。

下面是文件源码:

/* ------hellofirst.c------------*/
#include <stdio.h>
void hellofirst()
{
 printf("The first hello\n");
}

/* ------hellofirst.h------------*/
#ifndef HELLOFIRST_H
#define HELLOFIRST_H
extern void hellofirst(void);
#endif

/* ------hellosecond.c------------*/
#include <stdio.h>
void hellosecond()
{
 printf("The second hello\n");
}

/* ------hellosecond.h------------*/
#ifndef HELLOSECOND_H
#define HELLOSECOND_H
extern void hellosecond(void);
#endif

/* ------twohellos.c------------*/
int main(int argc, char *argv[])
{
 hellofirst();
 hellosecond();
 return 0;
}

通过makefile实现生成可执行文件twohellos,
为了突出主题,gcc中有关的参数(比如:-g ,-Wall 等)就不写了。
内容如下:
---------------------------------------------------------------

twohellos : twohellos.o hellofirst.o hellosecond.o
 gcc twohellos.o hellofirst.o hellosecond.o  -o twohellos

twohellos.o : twohellos.c hellofirst.h hellosecond.h
 gcc -c twohellos.c

hellofirst.o : hellofirst.c hellofirst.h
 gcc -c hellofirst.c

hellosecond.o : hellosecond.c hellosecond.h
 gcc -c hellosecond.c

.PHONY : clean
clean :
 -rm -f *.o
 -rm -f twohellos
---------------------------------------------------------------
操作过程如下:

[dingqs@localhost test]$ ll
total 24
-rw-rw-r--. 1 dingqs dingqs  74 Feb 13 14:12 hellofirst.c
-rw-rw-r--. 1 dingqs dingqs  98 Feb 14 10:13 hellofirst.h
-rw-rw-r--. 1 dingqs dingqs  76 Feb 13 14:12 hellosecond.c
-rw-rw-r--. 1 dingqs dingqs 102 Feb 14 10:14 hellosecond.h
-rw-rw-r--. 1 dingqs dingqs 385 Feb 14 10:25 Makefile
-rw-rw-r--. 1 dingqs dingqs  82 Feb 14 10:14 twohellos.c
[dingqs@localhost home]$ make
gcc -c twohellos.c
gcc -c hellofirst.c
gcc -c hellosecond.c
gcc twohellos.o hellofirst.o hellosecond.o  -o twohellos
[dingqs@localhost test]$ ll
total 44
-rw-rw-r--. 1 dingqs dingqs   74 Feb 13 14:12 hellofirst.c
-rw-rw-r--. 1 dingqs dingqs   98 Feb 14 10:13 hellofirst.h
-rw-rw-r--. 1 dingqs dingqs 1504 Feb 14 10:53 hellofirst.o
-rw-rw-r--. 1 dingqs dingqs   76 Feb 13 14:12 hellosecond.c
-rw-rw-r--. 1 dingqs dingqs  102 Feb 14 10:14 hellosecond.h
-rw-rw-r--. 1 dingqs dingqs 1504 Feb 14 10:53 hellosecond.o
-rw-rw-r--. 1 dingqs dingqs  385 Feb 14 10:25 Makefile
-rwxrwxr-x. 1 dingqs dingqs 6719 Feb 14 10:53 twohellos
-rw-rw-r--. 1 dingqs dingqs   82 Feb 14 10:14 twohellos.c
-rw-rw-r--. 1 dingqs dingqs 1464 Feb 14 10:53 twohellos.o
[dingqs@localhost test]$ ./twohellos
The first hello
The second hello
[dingqs@localhost test]$ make clean
rm -f *.o
rm -f twohellos
[dingqs@localhost test]$ ll
total 24
-rw-rw-r--. 1 dingqs dingqs  74 Feb 13 14:12 hellofirst.c
-rw-rw-r--. 1 dingqs dingqs  98 Feb 14 10:13 hellofirst.h
-rw-rw-r--. 1 dingqs dingqs  76 Feb 13 14:12 hellosecond.c
-rw-rw-r--. 1 dingqs dingqs 102 Feb 14 10:14 hellosecond.h
-rw-rw-r--. 1 dingqs dingqs 385 Feb 14 10:25 Makefile
-rw-rw-r--. 1 dingqs dingqs  82 Feb 14 10:14 twohellos.c
[dingqs@localhost home]$

在真实的项目中.c文件中提供的对外接口一般放在同名的.h文件中,然后在.c文件中include即可。
同时,在包含main()函数的.c文件的头部都会include main函数使用其他.c文件对应的.h文件。
比较接近真实项目的代码如下(主要添加箭头指的那四行):
/* ------hellofirst.c------------*/
#include <stdio.h>
#include "hellof

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值