Linux 程序员必须学会使用GNU make来构建和管理自己的软件工程。
[root@localhost 409]# cd makeFile
[root@localhost makeFile]# tree
.
|-- add
| |-- add.c
| `-- add.h
|-- add.o
|-- main
|-- main.c
|-- main.o
|-- makefile
|-- mul
| |-- mul.c
| `-- mul.h
|-- mul.o
|-- sub
| |-- sub.c
| `-- sub.h
`-- sub.o
3 directories, 13 files
#include < stdio.h > 在系统中找
#include " a.h " 在当前文件夹中找
makefile
obj = main.o add.o sub.o mul.o
main:$(obj)
gcc $(obj) -o $@
main.o:main.c add/add.h sub/sub.h mul/mul.h
gcc -c main.c -Iadd -Isub -Imul
add.o:add/add.c add/add.h
gcc -c add/add.c
sub.o:sub/sub.c sub/sub.h
gcc -c sub/sub.c
mul.o:mul/mul.c mul/mul.h
gcc -c mul/mul.c
.PHONY:clean
clean:
rm -f main.o add.o sub.o mul.o
main 函数
#include <stdio.h>
#include "add.h"
#include "sub.h"
#include "mul.h"
int main()
{
printf("+%d\n",add(2,3));
printf("-%d\n",sub(4,3));
printf("*%d\n",mul(5,5));
return 0;
}
[root@localhost makeFile]# ./main
+5
-1
*25
头文件的写法:
#ifndef _ADD_H_
#define _ADD_H_
int add(int a,int b);
#endif
[root@localhost 409]# cd makeFile
[root@localhost makeFile]# tree
.
|-- add
| |-- add.c
| `-- add.h
|-- add.o
|-- main
|-- main.c
|-- main.o
|-- makefile
|-- mul
| |-- mul.c
| `-- mul.h
|-- mul.o
|-- sub
| |-- sub.c
| `-- sub.h
`-- sub.o
3 directories, 13 files
#include < stdio.h > 在系统中找
#include " a.h " 在当前文件夹中找
makefile
obj = main.o add.o sub.o mul.o
main:$(obj)
gcc $(obj) -o $@
main.o:main.c add/add.h sub/sub.h mul/mul.h
gcc -c main.c -Iadd -Isub -Imul
add.o:add/add.c add/add.h
gcc -c add/add.c
sub.o:sub/sub.c sub/sub.h
gcc -c sub/sub.c
mul.o:mul/mul.c mul/mul.h
gcc -c mul/mul.c
.PHONY:clean
clean:
rm -f main.o add.o sub.o mul.o
main 函数
#include <stdio.h>
#include "add.h"
#include "sub.h"
#include "mul.h"
int main()
{
printf("+%d\n",add(2,3));
printf("-%d\n",sub(4,3));
printf("*%d\n",mul(5,5));
return 0;
}
[root@localhost makeFile]# ./main
+5
-1
*25
头文件的写法:
#ifndef _ADD_H_
#define _ADD_H_
int add(int a,int b);
#endif