本文档内容:
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