官方讲解 http://www.gnu.org/software/make/manual/make.html
个人理解:简单的说 makefile的产生源于需要对大型项目的文件依赖关系的解决。 它可以解决重复使用编译指令和每次都要处理文件依赖的麻烦。
gcc -c表示只编译(compile)源文件但不链接,会把.c或.cc的c源程序编译成目标文件,一般是.o文件。
gcc -o用于指定输出(out)文件名。不用-o的话,一般会在当前文件夹下生成默认的a.out文件作为可执行程序。//对于多个文件,需要用.o文件作为参数,无法直接使用.c文件。
假如我有如下几个文件:
head.c head.h main.c
//head.h
#include <stdio.h>
#include <stdlib.h>
#define MAXN 1005
struct node{
int x, y;
};
void print();
void scan();
//head.c
#include "head.h"
void print()
{
printf("print()\n");
}
void scan()
{
int x;
scanf("%d", &x);
printf("%d\n", x);
}
//main.c
#include "head.h"
int main()
{
struct node c;
printf("hello\n");
scan();
print();
}
不用makefile的写法 :
gcc -c head.h head.c main.c -o test
放入makefile文件中
objects = head.o main.o
test : $(objects)
gcc -o test $(objects)
head.o: head.c head.h
gcc -c head.c head.h
main.o: head.h main.c
gcc -c head.h main.c
.empty : clean
clean :
-rm -rf $(objects) test
objects 里放gcc -o 要用到的文件
$表示前面宏定义的展开
生成的文件名写在前面, 每一个第二行的生成指令注意前面要有TAB键缩进