3 | 1. 简单的GCC语法:
4 |
5 | - gcc –c test.c,表示只编译test.c文件,成功时输出目标文件test.o
6 | - gcc –o test test.o,将test.o连接成可执行的二进制文件test
7 | - gcc –o test test.c,将test.c编译并连接成可执行的二进制文件test
8 | - -o选项表示我们要求输出的可执行文件名。
9 | - -c选项表示我们只要求编译器输出目标代码,而不必要输出可执行文件。
10 | - -g选项表示我们要求编译器在编译的时候提供我们以后对程序进行调试的信息。
11 | - $@--目标文件,$^--所有的依赖文件,$<--第一个依赖文件.详细请查看别个大神的博客:[http://blog.csdn.net/kesaihao862/article/details/7332528](http://blog.csdn.net/kesaihao862/article/details/7332528 "引用:makefile 中 $@ $^ %< 使用")
12 |
13 | ----------
14 |
15 | 2. makefile:在makefile中写入如下语句:
16 |
17 | main:
18 | gcc -o hello hello.c
19 | clean:
20 | rm -f hello hello.o
21 |
22 |
23 | 在上面的代码块中,第一行的那个是默认的,比如你执行命令时输入make,则会默认执行main下面的语句.
24 | 在定义好依赖关系后,后续的那一行定义了如何生成目标文件的操作系统命令,一定要以一个Tab键作为开头。当我们输入命令 make main时,会执行编译并链接hello.c文件.当输入make clean时,会执行命令把hello 和 hello.c文件删除.是不是特别方便.
25 | 3. Linux压缩命令:
26 | gzip -cr test > 1.zip 将test文件夹压缩到1.zip中
27 | gunzip -r 1.zip > 3.txt 将1.zip解压到3.txt中
28 | tar -czvf my.tar.gz test 将test文件夹压缩到my.tar.gz中
29 | tar –zxvf my.tar.gz 将my.tar.gz解压
30 |
31 | -c :建立一个压缩文件的参数指令(create 的意思)
32 | -z :是否同时具有 gzip 的属性?亦即是否需要用 gzip 压缩?
33 | -v :压缩的过程中显示文件!这个常用,但不建议用在背景执行过程!
34 | -f :强制转换
35 | 4. 下面再插入一段makefile,继续分析
36 |
37 | # 变量的声明(有点像C语言里面的宏定义)
38 | objects = main.o print.o
39 |
40 | # helloworld:main.o print.o
41 | helloworld:$(objects)
42 | # helloworld就是我们要生成的目标
43 | # main.o print.o是生成此目标的先决条件
44 | gcc -o helloworld $(objects)
45 | # shell命令,最前面的一定是tab键
46 | $(objects) : print.h # 都依赖print.h
47 |
48 | main.o:main.c print.h
49 | gcc -c main.c
50 |
51 | print.o:print.c print.h
52 | gcc -c print.c
53 |
54 | clean:
55 | rm helloworld $(objects)
56 |
57 | ---------------------------------------------------------------------
58 | 上面其实是3个文件,如下
59 |
60 | 1. print.h
61 | #include
62 | void printhello();
63 |
64 | 2. print.c
65 | #include"print.h"
66 | void printhello(){
67 | printf("Hello, world\n");
68 | }
69 |
70 | 3. main.c
71 | #include "print.h"
72 | int main(void){
73 | printhello();
74 | return 0;
75 | }
76 |
77 | -----------------------------------------------------------------
78 | 解释一下,首先objects就像C语言里面的宏定义,在下面的任何地方都代表main.o print.o;
79 | 在helloworld那个语句之前就会执行main.o和print.o,进行编译.
80 |
81 |