上课不听讲的后果就是课下疯狂补知识了
原文来自https://www.cnblogs.com/chenguanfu/p/4415072.html
在Windows下,只需要简单的点击以下make,rebuild即可。而在Linux下,这样的IDE环境并没有提供,难道必须每一步都执行一遍吗?比较ok的做法自然是能够利用批处理脚本来进行操作了,这样,只需要修改脚本中需要编译的文件即可。在Linux下,提供了这么一个方便的工具,make。那么接下来我们来利用make进行程序的组织编译吧。
1. 编写tool.h
1
2
3
4
5
6
|
#ifndef __TOOL_H
#define __TOOL_H
void
printInteger(
int
number);
#endif // end of tool.h
|
2. 编写tool.c
1
2
3
4
5
6
7
|
#include "tool.h"
#include "stdio.h"
void
printInteger(
int
number)
{
printf
(
"the number is:%d\n"
,number);
}
|
3. 编写main.c
1
2
3
4
5
6
7
8
9
10
11
|
#include "tool.h"
#include "stdio.h"
int
main(
int
argc,
char
* argv[])
{
int
number;
number=10;
printf
(
"Context-Type:text/html\n\n"
);
printInteger(number);
return
0;
}
|
4. 编译链接文件生成可执行文件main
方法1:
命令行中进行编译
4.1.1 编译main.c生成main.o
1
|
sudo cc -c main.c
|
4.1.2 编译tool.c生成tool.o
1
|
sudo cc -c tool.c
|
4.1.3 链接main.o和tool.o生成main可执行文件
1
|
sudo cc -o main main.o tool.o
|
4.1.4 运行main
1
|
sudo ./main
|
方法2:
makefile进行编译链接
4.2.1 编写makefile文件(没有后缀名)
1
2
3
4
5
6
7
8
9
10
11
12
|
#MakeFile
main: main.o tool.o
main.o: main.c tool.h
cc -c main.c
tool.o: tool.c tool.h
cc -c tool.c
.PHONY:clean
clean:
rm *.o main
|
注:直接粘贴复制可能不行 注意严格使用tab进行缩进 可以使用vim 看见字体特殊颜色
4.2.2 运行make进行编译链接
1
|
sudo make
|
4.2.3 运行main
1
|
sudo ./main
|
4.2.4 删除所有.o文件并再次编译
1
2
|
sudo make clean
sudo make
|