在linux下编程,习惯于将.h 文件放在include目录中,.c文件放入src目录中,今天看了一下makefile的教程,写了个简单的makefile,
文件目录如下:
show01---icnlude
|-------show.h
src
|-------main.c show.c
Makefile
//show.h
1 #ifndef _SHOW_H
2 #define _SHOW_H
3 #include <stdio.h>
4
5 void show_str();
6
7 #endif
//show.c
1 #include <stdio.h>
2 #include "show.h"
3
4 void show_str()
5 {
6 printf("hello this is function show_str()\r\n");
7
8 }
//main.c
1 #include <stdio.h>
2 #include "show.h"
3
4 int main()
5 {
6 show_str();
7 return 0;
8 }
//Makefile
1 #makefile test
2
3 #DIR_INC = ./include
4 #DIR_SRC = ./src
5
6 #include ${DIR_SRC}/Dir.mk
7
8 hello : main.o show.o
9 gcc main.o show.o -o hello
10 main.o : ./src/main.c ./include/show.h
11 gcc -c ./src/main.c -I ./include
12 show.o : ./src/show.c ./include/show.h
13 gcc -c ./src/show.c -I ./include
14 clean:
15 rm -rf *.o
使用结果:
ninja@ninja:~/makefiletest/show01$ ls
include Makefile src
ninja@ninja:~/makefiletest/show01$ make hello
gcc -c ./src/main.c -I ./include
gcc -c ./src/show.c -I ./include
gcc main.o show.o -o hello
ninja@ninja:~/makefiletest/show01$ ls
hello include main.o Makefile show.o src
ninja@ninja:~/makefiletest/show01$ ./hello
hello this is function show_str()
ninja@ninja:~/makefiletest/show01$ make clean
rm -rf *.o
ninja@ninja:~/makefiletest/show01$ ls
hello include Makefile src
ninja@ninja:~/makefiletest/show01$