- 在test(copy)文件夹下创建如下文件,内容如下。
- 5个文件的内容如下
/*********************main.c**************/
#include "test1.h"
#include "test2.h"
#include <stdio.h>
int main()
{
test1_func("hello test1!");
test2_func("hello test2!");
return 0;
}
/**********************test1.c***********************/
#include "test1.h"
#include <stdio.h>
void test1_func(char *str)
{
printf("This is test1 : %s ",str);
}
/*************************test1.h*******************/
#ifndef _TEST1_H
#define _TEST1_H
void test1_func(char *str);
#endif
/**********************test2.c**********************/
#include "test2.h"
#include <stdio.h>
void test2_func(char *str)
{
printf("This is test2 : %s",str);
}
/**********************test2.h*********************/
#ifndef _TEST2_H
#define _TEST2_H
void test2_func(char *str);
#endif
- makefile文件如下 :
首先两个命令:gcc -c xxx.c xxx.h 的意思是在xxx.c中包含头文件xxx.h,编译器gcc依赖xxx.h编译生成可执行文件.o
gcc -o main main.o test1.o test.o 将由test1.c,test2.c,main.c生成的可执行文件链接起来生成目标文件main.lib,然后执行:./main可运行生成文件。
#Makefile
main:main.o test1.o test2.o #[目标文件]:[依赖关系表] 链接
gcc -o main main.o test1.o test2.o
main.o:main.c test1.h test2.h #[需编译文件]:[依赖文件] 编译
gcc -c main.c
test1.o:test1.c test1.h
gcc -c test1.c
test2.o:test2.c test2.h
gcc -c test2.c
.PHONY:clean
clean:
rm -f *.o main
- 写好上述文件之后,在main.c的目录下先make, 然后./main即可输出相应结果。
- 输入make clean即可删除所有中间生成的.o文件。