通过例子了解make
本节从一个简单的例子入手。
假设例子中含1个头文件hello.h和2个.c文件main.c、hello.c。具体内容如下:
- main.c:
#include"hello.h"
int main(){
hello();
return 0;
}
- hello.h:
#ifndef HELLO_H
#define HELLO_H
void hello();
#endif
- hello.c:
#include"stdio.h"
#include"hello.h"
void hello(){
printf("hello world!\n");
}
为了使用make编译上述工程,还需要一个Makefile文件,且需和源代码放在同一目录下。Makefile文件内容如下:
- Makefile:
main: main.o hello.o
gcc -o main main.o hello.o
main.o: main.c hello.h
gcc -c main.c
hello.o: hello.c hello.h
gcc -c hello.c
clean:
rm*.o
rm main
特别注意:行首的空白只能用Tab键
在这个目录下运行make编译(我的当前目录为test),效果如图1所示。
图1
这样,最简单的make编译就完成了。
暂时整理到这,3.23继续更新。