在写一个较大的项目时,难免会写很多的c文件和头文件,如果单独的编译每一个,费时费力,并且如果后序因为需要,要改动其中的一个源文件,那么编译器是不是又要重新把每个文件重新编译。为了解决这个问题,就要借助于Makefile了。
先看一个例子:
有下面这个程序,源代码如下:
/*main.c*/
#include "my_fun1.h"
#include "my_fun2.h"
int main(){
my_fun1_print("this is function 1!");
my_fun2_print("this is function 2!");
}
/*my_fun1.h*/
#ifndef _MY_FUN1_H
#define _MY_FUN1_H
void my_fun1_print(char *str);
#endif
/*my_fun1.c*/
#include "my_fun1.h"
#include <stdio.h>
void my_fun1_print(char *printStr){
printf("%s\n",printStr);
}
/* my_fun2.h*/
#ifndef _MY_FUN2_H
#define _MY_FUN2_H
void my_fun2_print(char *str);
#endif
/*my_fun2.c*/
#include "my_fun2.h"
#include

当项目包含多个C文件和头文件时,手动编译每个文件既耗时又不便。通过Makefile,可以自动化编译过程,仅在源文件变更时编译必要的部分。本文以一个包含5个源文件的简单程序为例,演示如何编写和使用Makefile,以提高开发效率。
最低0.47元/天 解锁文章
1万+

被折叠的 条评论
为什么被折叠?



