├── .gitignore ├── 0.Intro ├── 1.GCC_Introduction.md ├── 2.ComputerSystem.md ├── GNU-GCC.pdf └── images │ ├── ASCII.png │ ├── compilesystem.png │ ├── hello_1.png │ ├── hello_2.png │ └── hello_3.png ├── 1.C ├── CompileC.md ├── imgs │ └── filenamesuffixes1.png └── src │ ├── add.c │ ├── main.c │ └── minus.c ├── 2.C++ ├── CompileC++.md ├── imgs │ └── fundamentalcpp.png └── src │ └── helloworld.cpp ├── 3.Makefile-Tutorial ├── 1.Intro.md ├── 2.Variable.md ├── 3.Operator&Symbols.md ├── 4.Functions.md ├── 5.ConditionalStatement.md ├── 6.Compile.md ├── 7.StaticLibrary.md ├── 8.SharedLibrary.md ├── 9.Errors.md ├── GNU-make.pdf └── Makefile ├── README.md ├── Summary.md ├── Test ├── Makefile └── src │ ├── main.c │ ├── main.i │ └── main.s ├── imgs └── QRcode.png └── update.sh /.gitignore: -------------------------------------------------------------------------------- 1 | # files 2 | *.o 3 | *.a 4 | *.so 5 | exec 6 | .DS_Store 7 | 8 | # folders 9 | objs 10 | workspace 11 | lib 12 | -------------------------------------------------------------------------------- /0.Intro/1.GCC_Introduction.md: -------------------------------------------------------------------------------- 1 |   2 | # Intro 3 | # 1 GCC 官方文档 4 | GCC 官方文档网站:https://gcc.gnu.org/onlinedocs/ 5 | 6 | - 官方文档是最权威的,网上所有的答案都来自官方文档 7 | - 适应英语阅读,中文是母语,很多词我们自己有根深蒂固的含义和概念,再重新赋予新含义非常不容易理解或有歧义 8 | - 英语用词、逻辑比较严谨,网上的翻译水平参差不齐,坑很多 9 | - 国内论坛找到好答案全凭运气,好的答案可以节省时间,没有好的答案还是得花时间看官方文档,所以还是自己靠谱一点,锻炼一下英语能力 10 | 11 | 12 |   13 | # 2 GCC的介绍 14 |   GCC 是 GNU项目的一个产品。 15 | 16 |   GCC(GNU Compiler Collection,GNU编译程序集合)是最重要的开放源码软件。其他所有开放源码软件都在某种层次上依赖于它。甚至其他语言,例如 Python,都是由 C 语言开发的,由 GNU 编译程序编译的。 17 | 18 |   这个软件对于整个自由软件运动而言具有根本性的意义。如果没有它或类似的软件,就不可能有自由软件运动。GCC 为 Linux 的出现提供了可能性。 19 | 20 |   GCC 是由许多组件组成的,但它们也并不总是出现的。有些部分是和语言相关的,所以如果没有安装某种特定语言,系统中就不会出现相关的文件。 21 | 22 |   23 | ## 2.1 GCC常见的组成部分 24 | 25 | - **c++:** gcc 的一个版本,默认语言设置为 C++,而且在链接的时候自动包含标准 C++ 库。这和 g++ 一样 26 | 27 | - **configure:** GCC 源代码树根目录中的一个脚本。用于设置配置值和创建 GCC 编译程序必需的 make 程序文件 28 | 29 | 30 | - **g++:** gcc 的一个版本,默认语言设置为 C++,而且在链接的时候自动包含标准 C++库。这和 c++ 一样 31 | 32 | - **gcc:** 该驱动程序等同于执行编译程序和连接程序以产生需要的输出 33 | 34 | - **libgcc:** 该库包含的例程被作为编译程序的一部分,是因为它们可被链接到实际的可执行程序中。它们是特殊的例程,链接到可执行程序,来执行基本的任务,例如浮点运算。这些库中的例程通常都是平台相关的 35 | 36 | - **libstdc++:** 运行时库,包括定义为标准语言一部分的所有的 C++类和函数 37 | 38 |   39 | ## 2.2 GCC包含的常见的软件 40 | 41 | 42 | - **ar:** 这是一个程序,可通过从文档中增加、删除和析取文件来维护库文件。通常使用该工具是为了创建和管理连接程序使用的目标库文档。该程序是 binutils 包的一部分 43 | 44 | - **as:** GNU 汇编器。实际上它是一族汇编器,因为它可以被编译或能够在各种不同平台上工作。该程序是 binutjls 包的一部分 45 | autoconf:产生的 shell 脚本自动配置源代码包去编译某个特定版本的 UNIX 46 | 47 | 48 | - **gdb:** GNU 调试器,可用于检查程序运行时的值和行为 49 | GNATS:GNU 的调试跟踪系统(GNU Bug Tracking System)。一个跟踪 GCC和其他 GNU 软件问题的在线系统 50 | 51 | - **gprof:** 该程序会监督编译程序的执行过程,并报告程序中各个函数的运行时间,可以根据所提供的配置文件来优化程序。该程序是 binutils 包的一部分 52 | 53 | - **ld:** GNU 连接程序。该程序将目标文件的集合组合成可执行程序。该程序是 binutils 包的一部分 54 | 55 | - **libtool:** 一个基本库,支持 make 程序的描述文件使用的简化共享库用法的脚本 56 | 57 | - **make:** 一个工具程序,它会读 makefile 脚本来确定程序中的哪个部分需要编译和连接,然后发布必要的命令。它读出的脚本(叫做 makefile 或 Makefile)定义了文件关系和依赖关系 58 | 59 |   60 | # 3 GCC默认头文件搜索路径 61 | 查看命令 62 | ```shell 63 | echo | gcc -v -x c -E - 64 | ``` 65 | 66 | - /usr/lib/gcc/x86_64-linux-gnu/7/include 67 | - /usr/local/include 68 | - /usr/lib/gcc/x86_64-linux-gnu/7/include-fixed 69 | - /usr/include/x86_64-linux-gnu 70 | - /usr/include -------------------------------------------------------------------------------- /0.Intro/2.ComputerSystem.md: -------------------------------------------------------------------------------- 1 |   2 | # 简单编译原理 3 | # 1 hello, world在计算机的表示 4 | hello 程序的生命周期是从一个源程序(或者说源文件)开始的,即程序员通过编辑器创建并保存的文本文件,文件名是 hello.c。源程序实际上就是一个由值 0 和 1组成的位(又称为比特)序列,8 个位被组织成一组,称为字节。每个字节表示程序中的某些文本字符 5 | 6 | 大部分计算机使用 ASCII 标准来表示文本字符 7 | - 用一个唯一的单字节大小的整数值息来表示每个字符 8 | - hello.c 程序是以字节序列的方式储存在文件中的 9 | 10 | hello.c 的表示方法说明了一个基本思想∶ 系统中所有的信息——包括磁盘文件、内存中的程序、内存中存放的用户数据以及网络上传送的数据,都是由一串比特表示的 11 | 12 |
13 | 14 |
15 | 16 | 17 |   18 | # 2 编译过程 19 | hello 程序的生命周期从一个高级 C 语言程序开始 20 | 21 | 为了在系统上运行 hello.c 程序,每条 C 语句都必须被其他程序转化为一系列的低级机器语言指令 22 | 23 | 然后这些指令按照一种称为可执行目标程序的格式打好包,并以二进制磁盘文件的形式存放起来 24 | 25 | GCC 编译器读取源程序文件 hello.c,并把它翻译成一个可执行目标文件 hello。这个翻译过程可分为四个阶段完成,如下图所示 26 | 27 | 执行这四个阶段的程序(预处理器、编译器、汇编器和链接器)一起构成了编译系统(compilation system) 28 | 29 | 30 | 31 |
32 | 33 | 34 |   35 | ## 2.1 预处理阶段 36 | `预处理器(cpp)`根据以字符#开头的命令,修改原始的 C 程序。比如 hello.c中第 1行的 `#include ` 命令告诉预处理器读取系统头文件 stdio.h 的内容,并把它直接插入程序文本中。结果就得到了另一个 C 程序,通常是以.i作为文件扩展名 37 | 38 |   39 | ## 2.2 编译阶段 40 | `编译器(ccl)`将文本文件 hello.i翻译成文本文件 hello.s,它包含一 41 | 个汇编语言程序。该程序包含函数 main 的定义,如下所示: 42 | ``` 43 | main: 44 | subq $8, %rsp 45 | mov1 $.LCO,%edi 46 | call puts 47 | mov1 $0,%eax 48 | addq $8,%rsp 49 | ret 50 | ``` 51 | 每条语句都以一种文本格式描述了一条低级机器语言指令。汇编语言非常有用,它为不同高级语言的不同编译器提供了通用的输出语言 52 | 53 |   54 | ## 2.3 汇编阶段 55 | `汇编器(as)`将 hello.s 翻译成机器语言指令,把这些指令打包成一种叫做可重定位目标程序(relocatable object program)的格式,并将结果保存在目标文件 hello.o中。 56 | 57 | hello.o 文件是一个二进制文件,它包含的17 个字节是函数 main的指令编码。如果我们在文本编辑器中打开 hello.o文件,将看到一堆乱码。 58 | 59 | 60 |   61 | ## 2.4 链接阶段 62 | 注意,hello程序调用了 printf 函数,它是每个 C 编译器都提供的标准 C 库中的一个函数。printf 函数存在于一个名为 printf.o 的单独的预编译好了的目标文件中,而这个文件必须以某种方式合并到我们的 hello.o 程序中。 63 | 64 | `链接器(ld)`就负责处理这种合并。结果就得到 hello 文件,它是一个可执行目标文件(或者简称为可执行文件),可以被加载到内存中,由系统执行。 65 | 66 |   67 | # 3 hello, world 的执行过程 68 | 69 | 70 | ## 第一步 71 | - shell 等待我们输入一个命令 72 | - 当我们在键盘上输入字符串"./hello"(注意这里是编译好的可执行目标文件)后 73 | - shell 程序将字符逐一读入寄存器 74 | - 再把它存放到内存中 75 | 76 |
77 | 78 |
79 |   80 | 81 | 82 | ## 第二步 83 | - 当我们在键盘上敲回车键时,shell 程序就知道我们已经结束了命令的输人 84 | - 然后 shell 执行一系列指令来加载可执行的 hello 文件 85 | - 这些指令将 hello 目标文件中的代码和数据从磁盘复制到主存 86 | - 数据包括最终会被输出的字符串"hello,world\n"。 87 | 88 |
89 | 90 |
91 |   92 | 93 | 94 | ## 第三步 95 | - 一旦目标文件 hello 中的代码和数据被加载到主存 96 | - 处理器就开始执行 hello 程序的 main 程序中的机器语言指令 97 | - 这些指令将 "hello,world\n" 字符串中的字节从主存复制到寄存器文件 98 | - 再从寄存器文件中复制到显示设备,最终显示在屏幕上 99 | 100 |
101 | 102 |
103 |   104 | 105 | 106 |   107 | # 4 程序在计算机内的存储 108 | 109 | 上面的例子揭示了一个重要的问题,即系统“似乎”花费了大量的时间和步骤把信息从一个地方挪到另一个地方 110 | - hello程序的机器指令最初是存放在磁盘上 111 | - 当程序加载时,它们被复制到主存 112 | - 当处理器运行程序时,指令又从主存复制到处理器 113 | 114 | 相似地,数据串 "hello,world\n"开始时在磁盘上,然后被复制到主存,最后从主存上复制到显示设备 -------------------------------------------------------------------------------- /0.Intro/GNU-GCC.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WohimLee/GNC-Tutorial/0ef1c05ab78a76fb1d1baaf51b17aa9782922ead/0.Intro/GNU-GCC.pdf -------------------------------------------------------------------------------- /0.Intro/images/ASCII.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WohimLee/GNC-Tutorial/0ef1c05ab78a76fb1d1baaf51b17aa9782922ead/0.Intro/images/ASCII.png -------------------------------------------------------------------------------- /0.Intro/images/compilesystem.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WohimLee/GNC-Tutorial/0ef1c05ab78a76fb1d1baaf51b17aa9782922ead/0.Intro/images/compilesystem.png -------------------------------------------------------------------------------- /0.Intro/images/hello_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WohimLee/GNC-Tutorial/0ef1c05ab78a76fb1d1baaf51b17aa9782922ead/0.Intro/images/hello_1.png -------------------------------------------------------------------------------- /0.Intro/images/hello_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WohimLee/GNC-Tutorial/0ef1c05ab78a76fb1d1baaf51b17aa9782922ead/0.Intro/images/hello_2.png -------------------------------------------------------------------------------- /0.Intro/images/hello_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WohimLee/GNC-Tutorial/0ef1c05ab78a76fb1d1baaf51b17aa9782922ead/0.Intro/images/hello_3.png -------------------------------------------------------------------------------- /1.C/CompileC.md: -------------------------------------------------------------------------------- 1 |   2 | # 1 Fundamental Compiling 3 | >编译 C 语言相关的后缀 4 |
5 | 6 |
7 |   8 | 9 | 10 |   11 | # 2 Compiling C 12 | 13 | ## 2.1 Preprocessing 14 | 15 | ```shell 16 | # 不会生成 .i 文件 17 | gcc -E main.c 18 | gcc -E main.c -o helloworld.i 19 | ``` 20 | - -E 选项告诉编译器只进行预处理操作 21 | - -o 选项把预处理的结果输出到指定文件 22 | 23 | 24 |   25 | ## 2.2 Generating Assembly Language 26 | ```shell 27 | gcc -S main.c 28 | gcc -S main.c -o xxx.s 29 | ``` 30 | - -S 选项告诉编译器,进行预处理和编译成汇编语言操作 31 | 32 | 每个平台对应的汇编语言的形式是不同的,例如有很多型号的开发板,有很多型号的 CPU 33 | 34 | 35 |   36 | ## 2.3 Source File to Object File 37 | ```shell 38 | gcc -c main.c 39 | gcc -c main.c -o xxx.o 40 | # 编译多个 .c 文件 41 | gcc -c main.c add.c minus.c 42 | ``` 43 | 44 |   45 | ## 2.4 Single Source to Executable 46 | - 注意:后面三个命令执行后并没有按编译过程出现 .i .s 或 .o 文件,并不意味着没有经历这些过程 47 | 48 | ```shell 49 | gcc main.c 50 | gcc main.c -o xxx 51 | ``` 52 | 53 | 执行程序 54 | ``` 55 | ./可执行文件 56 | ``` 57 | 58 | ## 2.5 Multiple Sources to Executable 59 | ``` 60 | gcc main.c add.c minus.c -o exec 61 | ./exec 62 | ``` 63 | 64 | 65 |   66 | # 3 Creating a Static Library 67 | 68 | - 编译成 .o 的文件 69 | ```shell 70 | gcc -c [.c] -o [自定义文件名] 71 | gcc -c [.c] [.c] ... 72 | ``` 73 | - 编静态库 74 | ```shell 75 | ar -r [lib自定义库名.a] [.o] [.o] ... 76 | ``` 77 | - 链接成可执行文件 78 | ```shell 79 | gcc [.c] [.a] -o [自定义输出文件名] 80 | gcc [.c] -o [自定义输出文件名] -l[库名] -L[库所在路径] 81 | ``` 82 | 83 | 84 |   85 | # 4 Creating a Shared Library 86 | 87 | >编译二进制.o文件 88 | ```shell 89 | gcc -c -fpic [.c/.cpp][.c/.cpp]... 90 | ``` 91 | >编库 92 | ```shell 93 | gcc -shared [.o][.o]... -o [lib自定义库名.so] 94 | ``` 95 | - 链接库到可执行文件 96 | ```shell 97 | gcc [.c/.cpp] -o [自定义可执行文件名] -l[库名] -L[库路径] -Wl,-rpath=[库路径] 98 | ``` 99 | 100 |   101 | # 总结 102 | ## 1 编译过程 103 | 源文件.c文件 -> 预编译成.i文件 -> 编译成汇编语言.s -> 汇编成.o文件 -> 链接成可执行文件(名字自定义,后缀没关系) 104 | 105 | ## 2 编译过程命令 106 | - 预处理: 107 | ``` 108 | gcc -E [.c源文件] -o [自定义输出文件名.i] 109 | ``` 110 | - 编译成汇编语言(隐藏了预处理操作) : 111 | ``` 112 | gcc -S [.c源文件] 113 | ``` 114 | - 会变成.o的object文件(二进制文件,可用于链接) : 115 | ``` 116 | gcc -c [.c源文件] [.c源文件] [...] (可选选项:-o [自定文件名]) 117 | ``` 118 | ## 3 库 119 | >静态库 120 | - 编库(先转成.o文件,再编成lib[自定库名].a) 121 | ``` 122 | gcc -c [.c源文件] [.c源文件] [...] (可选选项:-o [自定文件名]) 123 | ``` 124 | ``` 125 | ar -r lib[自定库名].a [.o文件] [.o文件] [...] 126 | ``` 127 | - 链接 128 | ``` 129 | gcc [main文件] -o [自定义输出可执行文件名] -l[库名] -L[库的路径] 130 | ``` 131 | >动态库 132 | - 编库 133 | - 第一种做法, 先转成.o文件,再编成.so文件 134 | ```shell 135 | gcc -c -fpic [.c源文件] [.c源文件] [...] 136 | ``` 137 | ```shell 138 | gcc -shared [.o文件] [.o文件] [...] -o lib[库名].so 139 | ``` 140 | - 第二种做法,直接转成.so 141 | ```shell 142 | gcc -fpic -shared [.c源文件] [.c源文件] [...] -o lib[库名].so 143 | ``` 144 | - 链接 145 | 146 | ```shell 147 | gcc [main文件] -o [自定义输出可执行文件名] -l[库名] -L[库所在路径] -Wl,-rpath=[库所在路径] 148 | ``` 149 | -------------------------------------------------------------------------------- /1.C/imgs/filenamesuffixes1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WohimLee/GNC-Tutorial/0ef1c05ab78a76fb1d1baaf51b17aa9782922ead/1.C/imgs/filenamesuffixes1.png -------------------------------------------------------------------------------- /1.C/src/add.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | int add(int a, int b) 4 | { 5 | return a+b; 6 | } -------------------------------------------------------------------------------- /1.C/src/main.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | int add(int a, int b); 5 | int minus(int a, int b); 6 | 7 | int main() 8 | { 9 | int a = 10; 10 | int b = 5; 11 | int c; 12 | c = add(a, b); 13 | printf("a+b = %d\n", c); 14 | c = minus(a, b); 15 | printf("a_b = %d\n", c); 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /1.C/src/minus.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | int minus(int a, int b) 4 | { 5 | return a-b; 6 | } -------------------------------------------------------------------------------- /2.C++/CompileC++.md: -------------------------------------------------------------------------------- 1 |   2 | # 1 Fundamental Compiling 3 | >编译 C++ 语言相关的后缀 4 | 5 |
6 | 7 |
8 | 9 |   10 | # 2 Compiling C++ 11 | 12 | ## 2.1 Preprocessing 13 | 14 | ```shell 15 | g++ -E helloworld.c 16 | g++ -E helloworld.c -o helloworld.i 17 | ``` 18 | - -E 选项告诉编译器只进行预处理操作 19 | - -o 选项把预处理的结果输出到指定文件 20 | 21 | 22 | 23 |   24 | ## 2.2 Generating Assembly Language 25 | ```shell 26 | g++ -S helloworld.c 27 | g++ -S helloworld.c -o helloworld.s 28 | ``` 29 | - -S 选项告诉编译器,进行预处理和编译成汇编语言操作 30 | 31 | 每个平台对应的汇编语言的形式是不同的,例如有很多型号的开发板,有很多型号的 CPU 32 | 33 | 34 |   35 | ## 2.3 Source File to Object File 36 | ```shell 37 | g++ -c helloworld.c 38 | g++ -c helloworld.c -o harumph.o 39 | # 编译多个 .c 文件 40 | g++ -c helloworld.c helloworld1.c helloworld2.c 41 | ``` 42 | 43 | 44 |   45 | ## 2.4 Single Source to Executable 46 | - 注意:后面三个命令执行后并没有按编译过程出现 .i .s 或 .o 文件,并不意味着没有经历这些过程 47 | 48 | 49 | ```shell 50 | g++ helloworld.c 51 | g++ helloworld.c -o howdy 52 | ``` 53 | 54 | 执行程序 55 | ``` 56 | ./可执行文件 57 | ``` 58 | 59 |   60 | ## 2.5 Multiple Source to Executable 61 | 62 | ``` 63 | $ g++ hellomain.c sayhello.c -o hello 64 | ``` 65 | 66 | 67 |   68 | # 3 Creating a Static Library 69 | 70 | - 编译成 .o 的文件 71 | ```shell 72 | g++ -c [.c] -o [自定义文件名] 73 | g++ -c [.c] [.c] ... 74 | ``` 75 | - 编静态库 76 | ```shell 77 | ar -r [lib自定义库名.a] [.o] [.o] ... 78 | ``` 79 | - 链接成可执行文件 80 | ```shell 81 | g++ [.c] [.a] -o [自定义输出文件名] 82 | g++ [.c] -o [自定义输出文件名] -l[库名] -L[库所在路径] 83 | ``` 84 | 85 | 86 |   87 | # 4 Creating a Shared Library 88 | 89 | - 编译二进制.o文件 90 | ```shell 91 | g++ -c -fpic [.c/.cpp][.c/.cpp]... 92 | ``` 93 | - 编库 94 | ```shell 95 | g++ -shared [.o][.o]... -o [lib自定义库名.so] 96 | ``` 97 | - 连接动态库到可执行文件 98 | ```shell 99 | g++ [.c/.cpp] -o [自定义可执行文件名] -l[库名] -L[库路径] -Wl,-rpath=[库路径] 100 | ``` 101 | 102 |   103 | # 总结 104 | ## 1 编译过程 105 | 源文件.c文件 -> 预编译成.i文件 -> 编译成汇编语言.s -> 汇编成.o文件 -> 链接成可执行文件(名字自定义,后缀没关系) 106 | 107 | ## 2 编译过程命令 108 | - 预处理: 109 | ``` 110 | g++ -E [.c源文件] -o [自定义输出文件名.i] 111 | ``` 112 | - 编译成汇编语言(隐藏了预处理操作) : 113 | ``` 114 | g++ -S [.c源文件] 115 | ``` 116 | - 会变成.o的object文件(二进制文件,可用于链接) : 117 | ``` 118 | g++ -c [.c源文件] [.c源文件] [...] (可选选项:-o [自定文件名]) 119 | ``` 120 | ## 3 库 121 | >静态库 122 | - 编库(先转成.o文件,再编成lib[自定库名].a) 123 | ``` 124 | g++ -c [.c源文件] [.c源文件] [...] (可选选项:-o [自定文件名]) 125 | ``` 126 | ``` 127 | ar -r lib[自定库名].a [.o文件] [.o文件] [...] 128 | ``` 129 | - 链接 130 | ``` 131 | g++ [main文件] -o [自定义输出可执行文件名] -l[库名] -L[库的路径] 132 | ``` 133 | >动态库 134 | - 编库 135 | - 第一种做法, 先转成.o文件,再编成.so文件 136 | ```shell 137 | g++ -c -fpic [.c源文件] [.c源文件] [...] 138 | ``` 139 | ```shell 140 | g++ -shared [.o文件] [.o文件] [...] -o lib[库名].so 141 | ``` 142 | - 第二种做法,直接转成.so 143 | ```shell 144 | g++ -fpic -shared [.c源文件] [.c源文件] [...] -o lib[库名].so 145 | ``` 146 | - 链接 147 | 148 | ```shell 149 | g++ [main文件] -o [自定义输出可执行文件名] -l[库名] -L[库所在路径] -Wl,-rpath=[库所在路径] 150 | ``` 151 | -------------------------------------------------------------------------------- /2.C++/imgs/fundamentalcpp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WohimLee/GNC-Tutorial/0ef1c05ab78a76fb1d1baaf51b17aa9782922ead/2.C++/imgs/fundamentalcpp.png -------------------------------------------------------------------------------- /2.C++/src/helloworld.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | std::cout << "hello, world\n"; 7 | return (0); 8 | } -------------------------------------------------------------------------------- /3.Makefile-Tutorial/1.Intro.md: -------------------------------------------------------------------------------- 1 |   2 | # Makefile 3 | 4 | - GNU Make 官方网站:https://www.gnu.org/software/make/ 5 | - GNU Make 官方文档下载地址:https://www.gnu.org/software/make/manual/ 6 | - Makefile Tutorial:https://makefiletutorial.com/ 7 | 8 |   9 | ## 1 基本格式 10 | ```makefile 11 | targets : prerequisties 12 | [tab键]command 13 | ``` 14 | - target:目标文件,可以是 OjectFile,也可以是执行文件,还可以是一个标签(Label),对于标签这种特性,在后续的“伪目标”章节中会有叙述。 15 | - prerequisite:要生成那个 target 所需要的文件或是目标。 16 | - command:是 make 需要执行的命令, 17 | 18 | 19 |   20 | ## 2 Makefile 规则 21 | - make 会在当前目录下找到一个名字叫 `Makefile` 或 `makefile` 的文件 22 | - 如果找到,它会找文件中第一个目标文件(target),并把这个文件作为最终的目标文件 23 | - 如果 target 文件不存在,或是 target 文件依赖的 .o 文件(prerequities)的文件修改时间要比 target 这个文件新,就会执行后面所定义的命令 command 来生成 target 这个文件 24 | - 如果 target 依赖的 .o 文件(prerequisties)也存在,make 会在当前文件中找到 target 为 .o 文件的依赖性,如果找到,再根据那个规则生成 .o 文件 25 | 26 |   27 | ## 3 伪目标 28 | 29 | 30 | 为了避免 target 和 Makefile 同级目录下 `文件/文件夹` 重名的这种情况,我们可以使用一个特殊的标记 `.PHONY` 来显式地指明一个目标是 "伪目标",向 make 说明,不管是否有这个文件/文件夹,这个目标就是 "伪目标" 31 | 32 | ```makefile 33 | .PHONY : clean 34 | ``` 35 | 36 | 只要有这个声明,不管是否有 "clean" 文件/文件夹,要运行 "clean" 这个目标,只有"make clean" 这个命令 37 | 38 | >注意 39 | - 对于有 prerequisties 的 target -------------------------------------------------------------------------------- /3.Makefile-Tutorial/2.Variable.md: -------------------------------------------------------------------------------- 1 | 2 | 3 |   4 | # Makefile 的变量 5 | 变量在声明时需要给予初值,而在使用时,需要给在变量名前加上 `$` 符号,并用小括号 `()` 把变量给包括起来。 6 | 7 |   8 | ## 1 变量的定义 9 | 10 | ```makefile 11 | cpp := src/main.cpp 12 | obj := objs/main.o 13 | ``` 14 | ## 2 变量的引用 15 | - 可以用 `()` 或 `{}` 16 | ```makefile 17 | cpp := src/main.cpp 18 | obj := objs/main.o 19 | 20 | $(obj) : ${cpp} 21 | @g++ -c $(cpp) -o $(obj) 22 | 23 | compile : $(obj) 24 | ``` 25 | 26 |   27 | ## 3 预定义变量 28 | - `$@`: 目标(target)的完整名称 29 | - `$<`: 第一个依赖文件(prerequisties)的名称 30 | - `$^`: 所有的依赖文件(prerequisties),以空格分开,不包含重复的依赖文件 31 | ```make 32 | cpp := src/main.cpp 33 | obj := objs/main.o 34 | 35 | $(obj) : ${cpp} 36 | @g++ -c $< -o $@ 37 | @echo $^ 38 | 39 | compile : $(obj) 40 | .PHONY : compile 41 | ``` 42 | 43 |   44 | ## 4 -------------------------------------------------------------------------------- /3.Makefile-Tutorial/3.Operator&Symbols.md: -------------------------------------------------------------------------------- 1 | 2 |   3 | # Makefile 常用符号 4 | ## *1 = 5 | - 简单的赋值运算符 6 | - 用于将右边的值分配给左边的变量 7 | - 如果在后面的语句中重新定义了该变量,则将使用新的值 8 | 9 | >示例 10 | ```make 11 | HOST_ARCH = aarch64 12 | TARGET_ARCH = $(HOST_ARCH) 13 | 14 | # 更改了变量 a 15 | HOST_ARCH = amd64 16 | 17 | debug: 18 | @echo $(TARGET_ARCH) 19 | ``` 20 | 21 | ## 2 := 22 | - 立即赋值运算符 23 | - 用于在定义变量时立即求值 24 | - 该值在定义后不再更改 25 | - 即使在后面的语句中重新定义了该变量 26 | 27 | >示例 28 | ```make 29 | HOST_ARCH := aarch64 30 | TARGET_ARCH := $(HOST_ARCH) 31 | 32 | # 更改了变量 a 33 | HOST_ARCH := amd64 34 | 35 | debug: 36 | @echo $(TARGET_ARCH) 37 | ``` 38 | 39 | 40 |   41 | ## *3 ?= 42 | - 默认赋值运算符 43 | - 如果该变量已经定义,则不进行任何操作 44 | - 如果该变量尚未定义,则求值并分配 45 | ```make 46 | HOST_ARCH = aarch64 47 | HOST_ARCH ?= amd64 48 | 49 | debug: 50 | @echo $(HOST_ARCH) 51 | ``` 52 | 53 |   54 | ## 4 累加 += 55 | ```makefile 56 | CXXFLAGS := -m64 -fPIC -g -O0 -std=c++11 -w -fopenmp 57 | 58 | CXXFLAGS += $(include_paths) 59 | ``` 60 | 61 |   62 | ## 5 \ 63 | - 续行符 64 | >示例 65 | ```make 66 | LDLIBS := cudart opencv_core \ 67 | gomp nvinfer protobuf cudnn pthread \ 68 | cublas nvcaffe_parser nvinfer_plugin 69 | ``` 70 | 71 |   72 | ## 6 * 与 % 73 | - `*`: 通配符表示匹配任意字符串,可以用在目录名或文件名中 74 | - `%`: 通配符表示匹配任意字符串,并将匹配到的字符串作为变量使用 -------------------------------------------------------------------------------- /3.Makefile-Tutorial/4.Functions.md: -------------------------------------------------------------------------------- 1 | 2 | 3 |   4 | # Makefile 的常用函数 5 | 函数调用,很像变量的使用,也是以 “$” 来标识的,其语法如下: 6 | ```makefile 7 | $(fn, arguments) or ${fn, arguments} 8 | ``` 9 | - fn: 函数名 10 | - arguments: 函数参数,参数间以逗号 `,` 分隔,而函数名和参数之间以“空格”分隔 11 | 12 |   13 | ## 1 shell 14 | ```makefile 15 | $(shell ) 16 | ``` 17 | - 名称:shell 命令函数 —— shell 18 | - 功能:调用 shell 命令 command 19 | - 返回:函数返回 shell 命令 command 的执行结果 20 | 21 | 22 | >示例 23 | ```makefile 24 | # shell 指令,src 文件夹下找到 .cpp 文件 25 | cpp_srcs := $(shell find src -name "*.cpp") 26 | # shell 指令, 获取计算机架构 27 | HOST_ARCH := $(shell uname -m) 28 | ``` 29 | 30 |   31 | ## 2 subst 32 | ```makefile 33 | $(subst ,,) 34 | ``` 35 | - 名称:字符串替换函数——subst 36 | - 功能:把字串 \ 中的 \ 字符串替换成 \ 37 | - 返回:函数返回被替换过后的字符串 38 | >示例: 39 | ```makefile 40 | 41 | cpp_srcs := $(shell find src -name "*.cpp") 42 | cpp_objs := $(subst src/,objs/,$(cpp_objs)) 43 | 44 | ``` 45 | 46 |   47 | ## 3 patsubst 48 | ```makefile 49 | $(patsubst ,,) 50 | ``` 51 | - 名称:模式字符串替换函数 —— patsubst 52 | - 功能:通配符 `%`,表示任意长度的字串,从 text 中取出 patttern, 替换成 replacement 53 | - 返回:函数返回被替换过后的字符串 54 | >示例 55 | ```makefile 56 | cpp_srcs := $(shell find src -name "*.cpp") #shell指令,src文件夹下找到.cpp文件 57 | cpp_objs := $(patsubst %.cpp,%.o,$(cpp_srcs)) #cpp_srcs变量下cpp文件替换成 .o文件 58 | ``` 59 | 60 | 61 | 62 |   63 | ## 4 foreach 64 | ```makefile 65 | $(foreach ,,) 66 | ``` 67 | - 名称:循环函数——foreach。 68 | - 功能:把字串\中的元素逐一取出来,执行\包含的表达式 69 | - 返回:\所返回的每个字符串所组成的整个字符串(以空格分隔) 70 | 71 | >示例: 72 | ```makefile 73 | library_paths := /datav/shared/100_du/03.08/lean/protobuf-3.11.4/lib \ 74 | /usr/local/cuda-10.1/lib64 75 | 76 | library_paths := $(foreach item,$(library_paths),-L$(item)) 77 | ``` 78 | >同等效果 79 | ```makefile 80 | I_flag := $(include_paths:%=-I%) 81 | ``` 82 | 83 |   84 | ## 5 dir 85 | ```makefile 86 | $(dir ) 87 | ``` 88 | - 名称:取目录函数——dir。 89 | - 功能:从文件名序列中取出目录部分。目录部分是指最后一个反斜杠(“/”)之前 90 | 的部分。如果没有反斜杠,那么返回“./”。 91 | - 返回:返回文件名序列的目录部分。 92 | - 示例: 93 | ```makefile 94 | $(dir src/foo.c hacks) # 返回值是“src/ ./”。 95 | ``` 96 | 97 |   98 | ## 6 notdir 99 | ```makefile 100 | $(notdir ) 101 | ``` 102 | >示例 103 | ```makefile 104 | libs := $(notdir $(shell find /usr/lib -name lib*)) 105 | ``` 106 | 107 | 108 |   109 | ## 7 filter 110 | ```makefile 111 | $(filter ) 112 | ``` 113 | 114 | ```makefile 115 | libs := $(notdir $(shell find /usr/lib -name lib*)) 116 | a_libs := $(filter %.a,$(libs)) 117 | so_libs := $(filter %.so,$(libs)) 118 | ``` 119 | 120 |   121 | ## 8 basename 122 | ```makefile 123 | $(basename ) 124 | ``` 125 | 126 | ```makefile 127 | libs := $(notdir $(shell find /usr/lib -name lib*)) 128 | a_libs := $(subst lib,,$(basename $(filter %.a,$(libs)))) 129 | so_libs := $(subst lib,,$(basename $(filter %.so,$(libs)))) 130 | ``` 131 | 132 |   133 | ## 9 filter-out 134 | - 剔除不想要的字符串 135 | ```makefile 136 | objs := objs/add.o objs/minus.o objs/main.o 137 | cpp_objs := $(filter-out objs/main.o, $(objs)) 138 | ``` 139 | 140 |   141 | ## 10 wildcard 142 | - The wildcard function expands to a space-separated list of filenames that match the given patterns 143 | 144 | ```makefile 145 | cpp_srcs := $(wildcard src/*.cc src/*.cpp src/*.c) 146 | ``` 147 | 148 | 149 |   150 | ## Examples 151 | 152 | >boost.mk 153 | ```makefile 154 | ROOT := /usr 155 | 156 | sys_INCLUDE := $(ROOT)/include 157 | sys_LIB_DIR := /usr/lib/x86_64-linux-gnu 158 | full_paths := $(shell find $(sys_LIB_DIR) -name "libboost_*") 159 | sys_LIBS = $(filter %.a %.so, $(full_paths)) 160 | # sys_LIBS := $(wildcard $(sys_LIBS)*.a $(sys_LIBS)*.so) 161 | sys_LIBS := $(basename $(notdir $(sys_LIBS))) 162 | sys_LIBS := $(patsubst lib%,%,$(sys_LIBS)) 163 | ``` -------------------------------------------------------------------------------- /3.Makefile-Tutorial/5.ConditionalStatement.md: -------------------------------------------------------------------------------- 1 |   2 | # Conditional Rules 3 | >注意: 4 | - Condition 语句里面全部不能用 Tab 缩进, 你看到的 Makefile 如果好像有 "Tab", 那全部是空格 5 | - 使用 Tab 会报错:*** commands commence before first target 6 | 7 | 8 | ## 1 ifeq / else / endif 9 | ```makefile 10 | # build flags 11 | ifeq ($(TARGET_OS),darwin) 12 | LDFLAGS += -rpath $(CUDA_PATH)/lib 13 | CCFLAGS += -arch $(HOST_ARCH) 14 | else ifeq ($(HOST_ARCH)-$(TARGET_ARCH)-$(TARGET_OS),x86_64-armv7l-linux) 15 | LDFLAGS += --dynamic-linker=/lib/ld-linux-armhf.so.3 16 | CCFLAGS += -mfloat-abi=hard 17 | else ifeq ($(TARGET_OS),android) 18 | LDFLAGS += -pie 19 | CCFLAGS += -fpie -fpic -fexceptions 20 | endif 21 | ``` 22 | 23 | 24 |   25 | ## 2 ifneq / else / endif 26 | ```makefile 27 | HOST_ARCH := $(shell uname -m) 28 | TARGET_ARCH ?= $(HOST_ARCH) 29 | temp := $(filter $(TARGET_ARCH),x86_64 aarch64 sbsa ppc64le armv7l) 30 | 31 | ifneq (,$(filter $(TARGET_ARCH),x86_64 aarch64 sbsa ppc64le armv7l)) 32 | ifneq ($(TARGET_ARCH),$(HOST_ARCH)) 33 | ifneq (,$(filter $(TARGET_ARCH),x86_64 aarch64 sbsa ppc64le)) 34 | TARGET_SIZE := 64 35 | else ifneq (,$(filter $(TARGET_ARCH),armv7l)) 36 | TARGET_SIZE := 32 37 | endif 38 | else 39 | TARGET_SIZE := $(shell getconf LONG_BIT) 40 | endif 41 | else 42 | $(error ERROR - unsupported value $(TARGET_ARCH) for TARGET_ARCH!) 43 | endif 44 | ``` 45 | 46 |   47 | ## 3 ifdef / else / endif 48 | 49 | ```make 50 | ifdef TARGET_OVERRIDE # cuda toolkit targets override 51 | NVCCFLAGS += -target-dir $(TARGET_OVERRIDE) 52 | endif 53 | ``` -------------------------------------------------------------------------------- /3.Makefile-Tutorial/6.Compile.md: -------------------------------------------------------------------------------- 1 |   2 | # Compile 3 | 4 | # 1 编译过程 5 | ## 1.1 预处理 6 | 7 | >示例 8 | ```makefile 9 | cpp_srcs := $(shell find src -name *.cpp) 10 | pp_files := $(patsubst src/%.cpp,src/%.i,$(cpp_srcs)) 11 | 12 | src/%.i : src/%.cpp 13 | @g++ -E $^ -o $@ 14 | 15 | preprocess : $(pp_files) 16 | 17 | clean : 18 | @rm -f src/*.i 19 | 20 | debug : 21 | @echo $(pp_files) 22 | 23 | .PHONY : debug preprocess clean 24 | ``` 25 | 26 |   27 | ## 1.2 编译成汇编语言 28 | >示例 29 | ```makefile 30 | cpp_srcs := $(shell find src -name *.cpp) 31 | as_files := $(patsubst src/%.cpp,src/%.s,$(cpp_srcs)) 32 | 33 | src/%.s : src/%.cpp 34 | @g++ -S $^ -o $@ 35 | 36 | assemble : $(as_files) 37 | 38 | clean : 39 | @rm -f src/*.s 40 | 41 | debug : 42 | @echo $(as_files) 43 | 44 | .PHONY : debug assemble clean 45 | ``` 46 | 47 |   48 | ## 1.3 编译成目标文件 49 | >示例 50 | ```makefile 51 | cpp_srcs := $(shell find src -name *.cpp) 52 | cpp_objs := $(patsubst src/%.cpp,objs/%.o,$(cpp_srcs)) 53 | 54 | objs/%.o : src/%.cpp 55 | @mkdir -p $(dir $@) 56 | @g++ -c $^ -o $@ 57 | 58 | objects : $(cpp_objs) 59 | 60 | clean : 61 | @rm -rf objs src/*.o 62 | 63 | debug : 64 | @echo $(as_files) 65 | 66 | .PHONY : debug objects clean 67 | ``` 68 | 69 |   70 | ## 1.4 链接可执行文件 71 | ```makefile 72 | cpp_srcs := $(shell find src -name *.cpp) 73 | cpp_objs := $(patsubst src/%.cpp,objs/%.o,$(cpp_srcs)) 74 | 75 | 76 | objs/%.o : src/%.cpp 77 | @mkdir -p $(dir $@) 78 | @g++ -c $^ -o $@ 79 | 80 | workspace/exec : $(cpp_objs) 81 | @mkdir workspace/exec 82 | @g++ $^ -o $@ 83 | 84 | run : workspace 85 | @./$< 86 | 87 | clean : 88 | @rm -rf objs workspace/exec 89 | 90 | debug : 91 | @echo $(as_files) 92 | 93 | .PHONY : debug run clean 94 | ``` 95 | 96 | 97 |   98 | # 2 编译选项 99 | >编译选项 100 | - `-m64`: 指定编译为 64 位应用程序 101 | - `-std=`: 指定编译标准,例如:-std=c++11、-std=c++14 102 | - `-g`: 包含调试信息 103 | - `-w`: 不显示警告 104 | - `-O`: 优化等级,通常使用:-O3 105 | - `-I`: 加在头文件路径前 106 | - `fPIC`: (Position-Independent Code), 产生的没有绝对地址,全部使用相对地址,代码可以被加载到内存的任意位置,且可以正确的执行。这正是共享库所要求的,共享库被加载时,在内存的位置不是固定的 107 | 108 | 109 | >链接选项 110 | - `-l`: 加在库名前面 111 | - `-L`: 加在库路径前面 112 | - `-Wl,<选项>`: 将逗号分隔的 <选项> 传递给链接器 113 | - `-rpath=`: "运行" 的时候,去找的目录。运行的时候,要找 .so 文件,会从这个选项里指定的地方去找 114 | 115 | 116 | 117 |   118 | # 3 Implicit Rules 119 | - CC: Program for compiling C programs; default cc 120 | - CXX: Program for compiling C++ programs; default g++ 121 | - CFLAGS: Extra flags to give to the C compiler 122 | - CXXFLAGS: Extra flags to give to the C++ compiler 123 | - CPPFLAGS: Extra flags to give to the C preprocessor 124 | - LDFLAGS: Extra flags to give to compilers when they are supposed to invoke the linker 125 | 126 | 127 | 128 | 129 |   130 | # 4 编译带头文件的程序 131 | >add.hpp 132 | ```c++ 133 | #ifndef ADD_HPP 134 | #define ADD_HPP 135 | int add(int a, int b); 136 | 137 | #endif // ADD_HPP 138 | ``` 139 | >add.cpp 140 | ```c++ 141 | int add(int a, int b) 142 | { 143 | return a+b; 144 | } 145 | ``` 146 | >minus.hpp 147 | ```c++ 148 | #ifndef MINUS_HPP 149 | #define MINUS_HPP 150 | int minus(int a, int b); 151 | 152 | #endif // MINUS_HPP 153 | ``` 154 | >minus.cpp 155 | ```c++ 156 | int minus(int a, int b) 157 | { 158 | return a-b; 159 | } 160 | ``` 161 | >main.cpp 162 | ```c++ 163 | #include 164 | #include "add.hpp" 165 | #include "minus.hpp" 166 | 167 | int main() 168 | { 169 | int a=10; int b=5; 170 | int res = add(a, b); 171 | printf("a + b = %d\n", res); 172 | res = minus(a, b); 173 | printf("a - b = %d\n", res); 174 | 175 | return 0; 176 | } 177 | ``` 178 | 179 | >Makefile 180 | ```makefile 181 | cpp_srcs := $(shell find src -name *.cpp) 182 | cpp_objs := $(patsubst src/%.cpp,objs/%.o,$(cpp_srcs)) 183 | 184 | # 你的头文件所在文件夹路径(建议绝对路径) 185 | include_paths := 186 | I_flag := $(include_paths:%=-I%) 187 | 188 | 189 | objs/%.o : src/%.cpp 190 | @mkdir -p $(dir $@) 191 | @g++ -c $^ -o $@ $(I_flag) 192 | 193 | workspace/exec : $(cpp_objs) 194 | @mkdir -p $(dir $@) 195 | @g++ $^ -o $@ 196 | 197 | run : workspace/exec 198 | @./$< 199 | 200 | debug : 201 | @echo $(I_flag) 202 | 203 | clean : 204 | @rm -rf objs 205 | 206 | .PHONY : debug run 207 | ``` 208 | -------------------------------------------------------------------------------- /3.Makefile-Tutorial/7.StaticLibrary.md: -------------------------------------------------------------------------------- 1 | 2 |   3 | # Makefile 静态库编译 4 | 5 | ## 1 程序 6 | >add.hpp 7 | ```c++ 8 | #ifndef ADD_HPP 9 | #define ADD_HPP 10 | int add(int a, int b); 11 | 12 | #endif // ADD_HPP 13 | ``` 14 | >add.cpp 15 | ```c++ 16 | int add(int a, int b) 17 | { 18 | return a+b; 19 | } 20 | ``` 21 | >minus.hpp 22 | ```c++ 23 | #ifndef MINUS_HPP 24 | #define MINUS_HPP 25 | int minus(int a, int b); 26 | 27 | #endif // MINUS_HPP 28 | ``` 29 | >minus.cpp 30 | ```c++ 31 | int minus(int a, int b) 32 | { 33 | return a-b; 34 | } 35 | ``` 36 | >main.cpp 37 | ```c++ 38 | #include 39 | #include "add.hpp" 40 | #include "minus.hpp" 41 | 42 | int main() 43 | { 44 | int a=10; int b=5; 45 | int res = add(a, b); 46 | printf("a + b = %d\n", res); 47 | res = minus(a, b); 48 | printf("a - b = %d\n", res); 49 | 50 | return 0; 51 | } 52 | ``` 53 | 54 |   55 | ## 2 编译过程 56 | - 源文件[.c/cpp] -> Object文件[.o] 57 | ``` 58 | g++ -c [.c/cpp][.c/cpp]... -o [.o][.o]... -I[.h/hpp] -g 59 | ``` 60 | - Object文件[.o] -> 静态库文件[lib库名.a] 61 | ``` 62 | ar -r [lib库名.a] [.o][.o]... 63 | ``` 64 | - main 文件[.c/cpp] -> Object 文件[.o] 65 | ``` 66 | g++ -c [main.c/cpp] -o [.o] -I[.h/hpp] 67 | ``` 68 | - 链接 main 的 Object 文件与静态库文件 [lib库名.a] 69 | ``` 70 | g++ [main.o] -o [可执行文件] -l[库名] -L[库路径] 71 | ``` 72 | -------------------------------------------------------------------------------- /3.Makefile-Tutorial/8.SharedLibrary.md: -------------------------------------------------------------------------------- 1 |   2 | # 动态库(共享库) 3 | 4 | ## 1 编库 5 | >编译 .c 文件 6 | > 源文件[.c/cpp] -> Object文件[.o] 7 | ```makefile 8 | g++ -c [.c/cpp][.c/cpp]... -o [.o][.o]... -I[.h/hpp] -g -fpic 9 | ``` 10 | > Object文件[.o] -> 动态库文件[lib库名.so] 11 | ``` 12 | g++ -shared [.o][.o]... -o [lib库名.so] 13 | ``` 14 | > main文件[.c/cpp] -> Object文件[.o] 15 | ``` 16 | g++ -c [main.c/cpp] -o [.o] -I[.h/hpp] -g 17 | ``` 18 | 19 |   20 | ## 2 链接 21 | > 链接 main 的 Object 文件与动态库文件[lib库名.so] 22 | ``` 23 | g++ [main.o] -o [可执行文件] -l[库名] -L[库路径] -Wl,-rpath=[库路径] 24 | ``` -------------------------------------------------------------------------------- /3.Makefile-Tutorial/9.Errors.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # 常见 Error 4 | >*** missing separator. Stop. 5 | - 原因: Makefile 语法出错 6 | - 解决方法: 根据报错的行数,检查 tab 缩进,空格问题 7 | 8 | 9 | >*** commands commence before first target. Stop 10 | - 原因: if等语句里面用了 tab 缩进 11 | - 解决方法: 缩进的地方全部改为空格 12 | 13 | -------------------------------------------------------------------------------- /3.Makefile-Tutorial/GNU-make.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WohimLee/GNC-Tutorial/0ef1c05ab78a76fb1d1baaf51b17aa9782922ead/3.Makefile-Tutorial/GNU-make.pdf -------------------------------------------------------------------------------- /3.Makefile-Tutorial/Makefile: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GNC-Tutorial 2 | 3 | - Linux 下编译与 Makefile 的教程 4 | - Bilibili 配套视频教程:[GNU Makefile编译C/C++教程(Linux系统、VSCODE)](https://www.bilibili.com/video/BV1EM41177s1/?vd_source=ead820d10887c21595d014f264bcbb35) 5 | - git clone 完求个 star 啊~~~~ 6 | 7 | # Contents 8 | ## 0 Intro 9 | - GCC 的介绍 10 | - 编译系统 11 | ## 1 C 12 | C 程序的编译过程 13 | ## 2 C++ 14 | C++ 程序的编译过程 15 | 16 | ## 3 Makefile 17 | - Intro: 18 | - Variable: Makefile 中变量的写法 19 | - Operator&Symbols: 操作符和 Makefile 常用的符号 20 | - Functions: 常用函数 21 | - Conditional Statement: 条件语句 22 | - Compile: 编译过程与编译选项 23 | - Static Library: 静态库的编译与链接 24 | - Shared Library: 共享库/动态库的编译与链接 25 | - Errors: 常见的一些错误 26 | 27 |   28 |
29 | 30 |

If u wanna buy me a cup of coffee. 31 |

-------------------------------------------------------------------------------- /Summary.md: -------------------------------------------------------------------------------- 1 |   2 | # 总结 3 | 4 | 5 |   6 | # 1 基本介绍 7 | 8 | ## 1.1 Linux 9 | - 文件目录 10 | - 基本操作命令 11 | - vim 基本操作命令 12 | 13 |   14 | # 2 编译过程 15 | 16 | ## 2.1 C 语言编译 17 | - 预编译 18 | 19 | 把 .c源文件编译成 .i 预处理文件 20 | ``` 21 | gcc -E [源文件.c] -o [自定义名.i] 22 | ``` 23 | - 编译成汇编语言 24 | 25 | 把 .i 文件编译成 .s 汇编语言文件 26 | ``` 27 | gcc -S [源文件.c] 28 | ``` 29 | - 注意:隐藏了预编译、删除预处理i文件的过程 30 | 31 | - 编译成二进制 32 | 把 .s 编译成二进制.o 文件 33 | ``` 34 | gcc -c [源文件.c] -o [自定义文件名.o] [编译选项] 35 | ``` 36 | - 注意:隐藏了。。。 37 | 38 | - 链接成可执行文件 39 | 把 .o 文件,链接成可执行的二进制文件 40 | ``` 41 | gcc [.o] -o [自定义文件名] [链接选项] 42 | ``` 43 | 44 | ## 2.2 C++ 语言编译 45 | - 预编译 46 | 47 | 把 .c源文件编译成 .ii 预处理文件 48 | ``` 49 | gcc -E [源文件.c] -o [自定义名.ii] 50 | ``` 51 | - 编译成汇编语言 52 | 53 | 把 .i 文件编译成 .s 汇编语言文件 54 | ``` 55 | gcc -S [源文件.c] 56 | ``` 57 | - 注意:隐藏了预编译、删除预处理i文件的过程 58 | 59 | - 编译成二进制 60 | 把 .s 编译成二进制.o 文件 61 | ``` 62 | gcc -c [源文件.c] -o [自定义文件名.o] [编译选项] 63 | ``` 64 | - 注意:隐藏了。。。 65 | 66 | - 链接成可执行文件 67 | 把 .o 文件,链接成可执行的二进制文件 68 | ``` 69 | gcc [.o] -o [自定义文件名] [链接选项] 70 | ``` 71 |   72 | # 3 GCC 73 | 74 | 75 |   76 | # 4 Makefile 77 | 78 | ## 4.1 基础语法 79 | ``` 80 | target : prequisties 81 | @command 82 | ``` 83 | 84 | ## 4.2 变量使用 85 | 先赋初值,调用语法如下: 86 | ``` 87 | $(变量名) 88 | ``` 89 | 90 | ## 4.3 函数 91 | 92 | ### shell 93 | - 使用Linux命令 94 | 95 | ### patsubst 96 | - 按照一定的模式替换字符串 97 | 98 | ### subst 99 | - 直接替换字符串 100 | 101 | ### dir 102 | - 取父级目录 103 | 104 | ## 4.4 其它 105 | (1)增加编译选项的方法 106 | ``` 107 | include_paths := /datav/Lean/opencv4/include/opencv4 \ 108 | /datav/Lean/OpenBLAS/include 109 | 110 | I_option := $(include_paths:%=-I%) 111 | ``` 112 | (2)伪目标.PHONY 113 | 告诉 Make 在.PHONY 后面的都是伪目标(命令),不用生成文件,每次都会执行下面的 command(如果有 command 的话) 114 | 115 | -------------------------------------------------------------------------------- /Test/Makefile: -------------------------------------------------------------------------------- 1 | 2 | 3 | exec : src/main.c 4 | gcc src/main.c -o exec 5 | 6 | run : exec 7 | ./exec -------------------------------------------------------------------------------- /Test/src/main.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | int main() 5 | { 6 | printf("Hello World!\n"); 7 | return 0; 8 | } -------------------------------------------------------------------------------- /Test/src/main.i: -------------------------------------------------------------------------------- 1 | # 1 "src/main.c" 2 | # 1 "" 1 3 | # 1 "" 3 4 | # 400 "" 3 5 | # 1 "" 1 6 | # 1 "" 2 7 | # 1 "src/main.c" 2 8 | 9 | # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdio.h" 1 3 4 10 | # 64 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdio.h" 3 4 11 | # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/_stdio.h" 1 3 4 12 | # 68 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/_stdio.h" 3 4 13 | # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/cdefs.h" 1 3 4 14 | # 666 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/cdefs.h" 3 4 15 | # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_symbol_aliasing.h" 1 3 4 16 | # 667 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/cdefs.h" 2 3 4 17 | # 732 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/cdefs.h" 3 4 18 | # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_posix_availability.h" 1 3 4 19 | # 733 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/cdefs.h" 2 3 4 20 | # 69 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/_stdio.h" 2 3 4 21 | # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/Availability.h" 1 3 4 22 | # 165 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/Availability.h" 3 4 23 | # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/AvailabilityVersions.h" 1 3 4 24 | # 166 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/Availability.h" 2 3 4 25 | # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/AvailabilityInternal.h" 1 3 4 26 | # 167 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/Availability.h" 2 3 4 27 | # 70 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/_stdio.h" 2 3 4 28 | 29 | # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/_types.h" 1 3 4 30 | # 27 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/_types.h" 3 4 31 | # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types.h" 1 3 4 32 | # 33 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types.h" 3 4 33 | # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/machine/_types.h" 1 3 4 34 | # 34 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/machine/_types.h" 3 4 35 | # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/arm/_types.h" 1 3 4 36 | # 15 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/arm/_types.h" 3 4 37 | typedef signed char __int8_t; 38 | 39 | 40 | 41 | typedef unsigned char __uint8_t; 42 | typedef short __int16_t; 43 | typedef unsigned short __uint16_t; 44 | typedef int __int32_t; 45 | typedef unsigned int __uint32_t; 46 | typedef long long __int64_t; 47 | typedef unsigned long long __uint64_t; 48 | 49 | typedef long __darwin_intptr_t; 50 | typedef unsigned int __darwin_natural_t; 51 | # 48 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/arm/_types.h" 3 4 52 | typedef int __darwin_ct_rune_t; 53 | 54 | 55 | 56 | 57 | 58 | typedef union { 59 | char __mbstate8[128]; 60 | long long _mbstateL; 61 | } __mbstate_t; 62 | 63 | typedef __mbstate_t __darwin_mbstate_t; 64 | 65 | 66 | typedef long int __darwin_ptrdiff_t; 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | typedef long unsigned int __darwin_size_t; 75 | 76 | 77 | 78 | 79 | 80 | typedef __builtin_va_list __darwin_va_list; 81 | 82 | 83 | 84 | 85 | 86 | typedef int __darwin_wchar_t; 87 | 88 | 89 | 90 | 91 | typedef __darwin_wchar_t __darwin_rune_t; 92 | 93 | 94 | typedef int __darwin_wint_t; 95 | 96 | 97 | 98 | 99 | typedef unsigned long __darwin_clock_t; 100 | typedef __uint32_t __darwin_socklen_t; 101 | typedef long __darwin_ssize_t; 102 | typedef long __darwin_time_t; 103 | # 35 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/machine/_types.h" 2 3 4 104 | # 34 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types.h" 2 3 4 105 | # 55 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types.h" 3 4 106 | typedef __int64_t __darwin_blkcnt_t; 107 | typedef __int32_t __darwin_blksize_t; 108 | typedef __int32_t __darwin_dev_t; 109 | typedef unsigned int __darwin_fsblkcnt_t; 110 | typedef unsigned int __darwin_fsfilcnt_t; 111 | typedef __uint32_t __darwin_gid_t; 112 | typedef __uint32_t __darwin_id_t; 113 | typedef __uint64_t __darwin_ino64_t; 114 | 115 | typedef __darwin_ino64_t __darwin_ino_t; 116 | 117 | 118 | 119 | typedef __darwin_natural_t __darwin_mach_port_name_t; 120 | typedef __darwin_mach_port_name_t __darwin_mach_port_t; 121 | typedef __uint16_t __darwin_mode_t; 122 | typedef __int64_t __darwin_off_t; 123 | typedef __int32_t __darwin_pid_t; 124 | typedef __uint32_t __darwin_sigset_t; 125 | typedef __int32_t __darwin_suseconds_t; 126 | typedef __uint32_t __darwin_uid_t; 127 | typedef __uint32_t __darwin_useconds_t; 128 | typedef unsigned char __darwin_uuid_t[16]; 129 | typedef char __darwin_uuid_string_t[37]; 130 | 131 | # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_pthread/_pthread_types.h" 1 3 4 132 | # 57 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_pthread/_pthread_types.h" 3 4 133 | struct __darwin_pthread_handler_rec { 134 | void (*__routine)(void *); 135 | void *__arg; 136 | struct __darwin_pthread_handler_rec *__next; 137 | }; 138 | 139 | struct _opaque_pthread_attr_t { 140 | long __sig; 141 | char __opaque[56]; 142 | }; 143 | 144 | struct _opaque_pthread_cond_t { 145 | long __sig; 146 | char __opaque[40]; 147 | }; 148 | 149 | struct _opaque_pthread_condattr_t { 150 | long __sig; 151 | char __opaque[8]; 152 | }; 153 | 154 | struct _opaque_pthread_mutex_t { 155 | long __sig; 156 | char __opaque[56]; 157 | }; 158 | 159 | struct _opaque_pthread_mutexattr_t { 160 | long __sig; 161 | char __opaque[8]; 162 | }; 163 | 164 | struct _opaque_pthread_once_t { 165 | long __sig; 166 | char __opaque[8]; 167 | }; 168 | 169 | struct _opaque_pthread_rwlock_t { 170 | long __sig; 171 | char __opaque[192]; 172 | }; 173 | 174 | struct _opaque_pthread_rwlockattr_t { 175 | long __sig; 176 | char __opaque[16]; 177 | }; 178 | 179 | struct _opaque_pthread_t { 180 | long __sig; 181 | struct __darwin_pthread_handler_rec *__cleanup_stack; 182 | char __opaque[8176]; 183 | }; 184 | 185 | typedef struct _opaque_pthread_attr_t __darwin_pthread_attr_t; 186 | typedef struct _opaque_pthread_cond_t __darwin_pthread_cond_t; 187 | typedef struct _opaque_pthread_condattr_t __darwin_pthread_condattr_t; 188 | typedef unsigned long __darwin_pthread_key_t; 189 | typedef struct _opaque_pthread_mutex_t __darwin_pthread_mutex_t; 190 | typedef struct _opaque_pthread_mutexattr_t __darwin_pthread_mutexattr_t; 191 | typedef struct _opaque_pthread_once_t __darwin_pthread_once_t; 192 | typedef struct _opaque_pthread_rwlock_t __darwin_pthread_rwlock_t; 193 | typedef struct _opaque_pthread_rwlockattr_t __darwin_pthread_rwlockattr_t; 194 | typedef struct _opaque_pthread_t *__darwin_pthread_t; 195 | # 81 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types.h" 2 3 4 196 | # 28 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/_types.h" 2 3 4 197 | # 40 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/_types.h" 3 4 198 | typedef int __darwin_nl_item; 199 | typedef int __darwin_wctrans_t; 200 | 201 | typedef __uint32_t __darwin_wctype_t; 202 | # 72 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/_stdio.h" 2 3 4 203 | 204 | 205 | 206 | # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_va_list.h" 1 3 4 207 | # 31 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_va_list.h" 3 4 208 | # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/machine/types.h" 1 3 4 209 | # 37 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/machine/types.h" 3 4 210 | # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/arm/types.h" 1 3 4 211 | # 55 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/arm/types.h" 3 4 212 | # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_int8_t.h" 1 3 4 213 | # 30 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_int8_t.h" 3 4 214 | typedef signed char int8_t; 215 | # 56 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/arm/types.h" 2 3 4 216 | # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_int16_t.h" 1 3 4 217 | # 30 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_int16_t.h" 3 4 218 | typedef short int16_t; 219 | # 57 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/arm/types.h" 2 3 4 220 | # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_int32_t.h" 1 3 4 221 | # 30 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_int32_t.h" 3 4 222 | typedef int int32_t; 223 | # 58 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/arm/types.h" 2 3 4 224 | # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_int64_t.h" 1 3 4 225 | # 30 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_int64_t.h" 3 4 226 | typedef long long int64_t; 227 | # 59 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/arm/types.h" 2 3 4 228 | 229 | # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_u_int8_t.h" 1 3 4 230 | # 30 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_u_int8_t.h" 3 4 231 | typedef unsigned char u_int8_t; 232 | # 61 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/arm/types.h" 2 3 4 233 | # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_u_int16_t.h" 1 3 4 234 | # 30 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_u_int16_t.h" 3 4 235 | typedef unsigned short u_int16_t; 236 | # 62 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/arm/types.h" 2 3 4 237 | # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_u_int32_t.h" 1 3 4 238 | # 30 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_u_int32_t.h" 3 4 239 | typedef unsigned int u_int32_t; 240 | # 63 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/arm/types.h" 2 3 4 241 | # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_u_int64_t.h" 1 3 4 242 | # 30 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_u_int64_t.h" 3 4 243 | typedef unsigned long long u_int64_t; 244 | # 64 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/arm/types.h" 2 3 4 245 | 246 | 247 | typedef int64_t register_t; 248 | 249 | 250 | 251 | 252 | # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_intptr_t.h" 1 3 4 253 | # 30 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_intptr_t.h" 3 4 254 | # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/machine/types.h" 1 3 4 255 | # 31 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_intptr_t.h" 2 3 4 256 | 257 | typedef __darwin_intptr_t intptr_t; 258 | # 72 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/arm/types.h" 2 3 4 259 | # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_uintptr_t.h" 1 3 4 260 | # 34 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_uintptr_t.h" 3 4 261 | typedef unsigned long uintptr_t; 262 | # 73 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/arm/types.h" 2 3 4 263 | 264 | 265 | 266 | 267 | typedef u_int64_t user_addr_t; 268 | typedef u_int64_t user_size_t; 269 | typedef int64_t user_ssize_t; 270 | typedef int64_t user_long_t; 271 | typedef u_int64_t user_ulong_t; 272 | typedef int64_t user_time_t; 273 | typedef int64_t user_off_t; 274 | # 104 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/arm/types.h" 3 4 275 | typedef u_int64_t syscall_arg_t; 276 | # 38 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/machine/types.h" 2 3 4 277 | # 32 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_va_list.h" 2 3 4 278 | typedef __darwin_va_list va_list; 279 | # 76 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/_stdio.h" 2 3 4 280 | # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_size_t.h" 1 3 4 281 | # 31 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_size_t.h" 3 4 282 | typedef __darwin_size_t size_t; 283 | # 77 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/_stdio.h" 2 3 4 284 | # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_null.h" 1 3 4 285 | # 78 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/_stdio.h" 2 3 4 286 | 287 | # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/stdio.h" 1 3 4 288 | # 47 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/stdio.h" 3 4 289 | int renameat(int, const char *, int, const char *) __attribute__((availability(macosx,introduced=10.10))); 290 | 291 | 292 | 293 | int renamex_np(const char *, const char *, unsigned int) __attribute__((availability(macosx,introduced=10.12))) __attribute__((availability(ios,introduced=10.0))) __attribute__((availability(tvos,introduced=10.0))) __attribute__((availability(watchos,introduced=3.0))); 294 | int renameatx_np(int, const char *, int, const char *, unsigned int) __attribute__((availability(macosx,introduced=10.12))) __attribute__((availability(ios,introduced=10.0))) __attribute__((availability(tvos,introduced=10.0))) __attribute__((availability(watchos,introduced=3.0))); 295 | # 80 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/_stdio.h" 2 3 4 296 | 297 | typedef __darwin_off_t fpos_t; 298 | # 92 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/_stdio.h" 3 4 299 | struct __sbuf { 300 | unsigned char *_base; 301 | int _size; 302 | }; 303 | 304 | 305 | struct __sFILEX; 306 | # 126 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/_stdio.h" 3 4 307 | typedef struct __sFILE { 308 | unsigned char *_p; 309 | int _r; 310 | int _w; 311 | short _flags; 312 | short _file; 313 | struct __sbuf _bf; 314 | int _lbfsize; 315 | 316 | 317 | void *_cookie; 318 | int (* _Nullable _close)(void *); 319 | int (* _Nullable _read) (void *, char *, int); 320 | fpos_t (* _Nullable _seek) (void *, fpos_t, int); 321 | int (* _Nullable _write)(void *, const char *, int); 322 | 323 | 324 | struct __sbuf _ub; 325 | struct __sFILEX *_extra; 326 | int _ur; 327 | 328 | 329 | unsigned char _ubuf[3]; 330 | unsigned char _nbuf[1]; 331 | 332 | 333 | struct __sbuf _lb; 334 | 335 | 336 | int _blksize; 337 | fpos_t _offset; 338 | } FILE; 339 | # 65 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdio.h" 2 3 4 340 | 341 | 342 | extern FILE *__stdinp; 343 | extern FILE *__stdoutp; 344 | extern FILE *__stderrp; 345 | # 142 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdio.h" 3 4 346 | void clearerr(FILE *); 347 | int fclose(FILE *); 348 | int feof(FILE *); 349 | int ferror(FILE *); 350 | int fflush(FILE *); 351 | int fgetc(FILE *); 352 | int fgetpos(FILE * restrict, fpos_t *); 353 | char *fgets(char * restrict, int, FILE *); 354 | 355 | 356 | 357 | FILE *fopen(const char * restrict __filename, const char * restrict __mode) __asm("_" "fopen" ); 358 | 359 | int fprintf(FILE * restrict, const char * restrict, ...) __attribute__((__format__ (__printf__, 2, 3))); 360 | int fputc(int, FILE *); 361 | int fputs(const char * restrict, FILE * restrict) __asm("_" "fputs" ); 362 | size_t fread(void * restrict __ptr, size_t __size, size_t __nitems, FILE * restrict __stream); 363 | FILE *freopen(const char * restrict, const char * restrict, 364 | FILE * restrict) __asm("_" "freopen" ); 365 | int fscanf(FILE * restrict, const char * restrict, ...) __attribute__((__format__ (__scanf__, 2, 3))); 366 | int fseek(FILE *, long, int); 367 | int fsetpos(FILE *, const fpos_t *); 368 | long ftell(FILE *); 369 | size_t fwrite(const void * restrict __ptr, size_t __size, size_t __nitems, FILE * restrict __stream) __asm("_" "fwrite" ); 370 | int getc(FILE *); 371 | int getchar(void); 372 | 373 | 374 | __attribute__((__deprecated__("This function is provided for compatibility reasons only. Due to security concerns inherent in the design of gets(3), it is highly recommended that you use fgets(3) instead."))) 375 | 376 | char *gets(char *); 377 | 378 | void perror(const char *) __attribute__((__cold__)); 379 | int printf(const char * restrict, ...) __attribute__((__format__ (__printf__, 1, 2))); 380 | int putc(int, FILE *); 381 | int putchar(int); 382 | int puts(const char *); 383 | int remove(const char *); 384 | int rename (const char *__old, const char *__new); 385 | void rewind(FILE *); 386 | int scanf(const char * restrict, ...) __attribute__((__format__ (__scanf__, 1, 2))); 387 | void setbuf(FILE * restrict, char * restrict); 388 | int setvbuf(FILE * restrict, char * restrict, int, size_t); 389 | 390 | __attribute__((__availability__(swift, unavailable, message="Use snprintf instead."))) 391 | 392 | __attribute__((__deprecated__("This function is provided for compatibility reasons only. Due to security concerns inherent in the design of sprintf(3), it is highly recommended that you use snprintf(3) instead."))) 393 | 394 | int sprintf(char * restrict, const char * restrict, ...) __attribute__((__format__ (__printf__, 2, 3))); 395 | 396 | int sscanf(const char * restrict, const char * restrict, ...) __attribute__((__format__ (__scanf__, 2, 3))); 397 | FILE *tmpfile(void); 398 | 399 | __attribute__((__availability__(swift, unavailable, message="Use mkstemp(3) instead."))) 400 | 401 | __attribute__((__deprecated__("This function is provided for compatibility reasons only. Due to security concerns inherent in the design of tmpnam(3), it is highly recommended that you use mkstemp(3) instead."))) 402 | 403 | char *tmpnam(char *); 404 | 405 | int ungetc(int, FILE *); 406 | int vfprintf(FILE * restrict, const char * restrict, va_list) __attribute__((__format__ (__printf__, 2, 0))); 407 | int vprintf(const char * restrict, va_list) __attribute__((__format__ (__printf__, 1, 0))); 408 | 409 | __attribute__((__availability__(swift, unavailable, message="Use vsnprintf instead."))) 410 | 411 | __attribute__((__deprecated__("This function is provided for compatibility reasons only. Due to security concerns inherent in the design of sprintf(3), it is highly recommended that you use vsnprintf(3) instead."))) 412 | 413 | int vsprintf(char * restrict, const char * restrict, va_list) __attribute__((__format__ (__printf__, 2, 0))); 414 | # 222 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdio.h" 3 4 415 | # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/_ctermid.h" 1 3 4 416 | # 31 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/_ctermid.h" 3 4 417 | char *ctermid(char *); 418 | # 223 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdio.h" 2 3 4 419 | 420 | 421 | 422 | 423 | FILE *fdopen(int, const char *) __asm("_" "fdopen" ); 424 | 425 | int fileno(FILE *); 426 | # 240 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdio.h" 3 4 427 | int pclose(FILE *) __attribute__((__availability__(swift, unavailable, message="Use posix_spawn APIs or NSTask instead. (On iOS, process spawning is unavailable.)"))); 428 | 429 | 430 | 431 | FILE *popen(const char *, const char *) __asm("_" "popen" ) __attribute__((__availability__(swift, unavailable, message="Use posix_spawn APIs or NSTask instead. (On iOS, process spawning is unavailable.)"))); 432 | # 259 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdio.h" 3 4 433 | int __srget(FILE *); 434 | int __svfscanf(FILE *, const char *, va_list) __attribute__((__format__ (__scanf__, 2, 0))); 435 | int __swbuf(int, FILE *); 436 | # 270 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdio.h" 3 4 437 | inline __attribute__ ((__always_inline__)) int __sputc(int _c, FILE *_p) { 438 | if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n')) 439 | return (*_p->_p++ = _c); 440 | else 441 | return (__swbuf(_c, _p)); 442 | } 443 | # 296 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdio.h" 3 4 444 | void flockfile(FILE *); 445 | int ftrylockfile(FILE *); 446 | void funlockfile(FILE *); 447 | int getc_unlocked(FILE *); 448 | int getchar_unlocked(void); 449 | int putc_unlocked(int, FILE *); 450 | int putchar_unlocked(int); 451 | 452 | 453 | 454 | int getw(FILE *); 455 | int putw(int, FILE *); 456 | 457 | 458 | __attribute__((__availability__(swift, unavailable, message="Use mkstemp(3) instead."))) 459 | 460 | __attribute__((__deprecated__("This function is provided for compatibility reasons only. Due to security concerns inherent in the design of tempnam(3), it is highly recommended that you use mkstemp(3) instead."))) 461 | 462 | char *tempnam(const char *__dir, const char *__prefix) __asm("_" "tempnam" ); 463 | # 334 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdio.h" 3 4 464 | # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_off_t.h" 1 3 4 465 | # 31 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_off_t.h" 3 4 466 | typedef __darwin_off_t off_t; 467 | # 335 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdio.h" 2 3 4 468 | 469 | 470 | int fseeko(FILE * __stream, off_t __offset, int __whence); 471 | off_t ftello(FILE * __stream); 472 | 473 | 474 | 475 | 476 | 477 | int snprintf(char * restrict __str, size_t __size, const char * restrict __format, ...) __attribute__((__format__ (__printf__, 3, 4))); 478 | int vfscanf(FILE * restrict __stream, const char * restrict __format, va_list) __attribute__((__format__ (__scanf__, 2, 0))); 479 | int vscanf(const char * restrict __format, va_list) __attribute__((__format__ (__scanf__, 1, 0))); 480 | int vsnprintf(char * restrict __str, size_t __size, const char * restrict __format, va_list) __attribute__((__format__ (__printf__, 3, 0))); 481 | int vsscanf(const char * restrict __str, const char * restrict __format, va_list) __attribute__((__format__ (__scanf__, 2, 0))); 482 | # 359 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdio.h" 3 4 483 | # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_ssize_t.h" 1 3 4 484 | # 31 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_ssize_t.h" 3 4 485 | typedef __darwin_ssize_t ssize_t; 486 | # 360 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdio.h" 2 3 4 487 | 488 | 489 | int dprintf(int, const char * restrict, ...) __attribute__((__format__ (__printf__, 2, 3))) __attribute__((availability(macosx,introduced=10.7))); 490 | int vdprintf(int, const char * restrict, va_list) __attribute__((__format__ (__printf__, 2, 0))) __attribute__((availability(macosx,introduced=10.7))); 491 | ssize_t getdelim(char ** restrict __linep, size_t * restrict __linecapp, int __delimiter, FILE * restrict __stream) __attribute__((availability(macosx,introduced=10.7))); 492 | ssize_t getline(char ** restrict __linep, size_t * restrict __linecapp, FILE * restrict __stream) __attribute__((availability(macosx,introduced=10.7))); 493 | FILE *fmemopen(void * restrict __buf, size_t __size, const char * restrict __mode) __attribute__((availability(macos,introduced=10.13))) __attribute__((availability(ios,introduced=11.0))) __attribute__((availability(tvos,introduced=11.0))) __attribute__((availability(watchos,introduced=4.0))); 494 | FILE *open_memstream(char **__bufp, size_t *__sizep) __attribute__((availability(macos,introduced=10.13))) __attribute__((availability(ios,introduced=11.0))) __attribute__((availability(tvos,introduced=11.0))) __attribute__((availability(watchos,introduced=4.0))); 495 | # 377 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdio.h" 3 4 496 | extern const int sys_nerr; 497 | extern const char *const sys_errlist[]; 498 | 499 | int asprintf(char ** restrict, const char * restrict, ...) __attribute__((__format__ (__printf__, 2, 3))); 500 | char *ctermid_r(char *); 501 | char *fgetln(FILE *, size_t *); 502 | const char *fmtcheck(const char *, const char *) __attribute__((format_arg(2))); 503 | int fpurge(FILE *); 504 | void setbuffer(FILE *, char *, int); 505 | int setlinebuf(FILE *); 506 | int vasprintf(char ** restrict, const char * restrict, va_list) __attribute__((__format__ (__printf__, 2, 0))); 507 | 508 | 509 | 510 | 511 | 512 | FILE *funopen(const void *, 513 | int (* _Nullable)(void *, char *, int), 514 | int (* _Nullable)(void *, const char *, int), 515 | fpos_t (* _Nullable)(void *, fpos_t, int), 516 | int (* _Nullable)(void *)); 517 | # 416 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdio.h" 3 4 518 | # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/secure/_stdio.h" 1 3 4 519 | # 31 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/secure/_stdio.h" 3 4 520 | # 1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/secure/_common.h" 1 3 4 521 | # 32 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/secure/_stdio.h" 2 3 4 522 | # 42 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/secure/_stdio.h" 3 4 523 | extern int __sprintf_chk (char * restrict, int, size_t, 524 | const char * restrict, ...); 525 | # 52 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/secure/_stdio.h" 3 4 526 | extern int __snprintf_chk (char * restrict, size_t, int, size_t, 527 | const char * restrict, ...); 528 | 529 | 530 | 531 | 532 | 533 | 534 | 535 | extern int __vsprintf_chk (char * restrict, int, size_t, 536 | const char * restrict, va_list); 537 | 538 | 539 | 540 | 541 | 542 | 543 | 544 | extern int __vsnprintf_chk (char * restrict, size_t, int, size_t, 545 | const char * restrict, va_list); 546 | # 417 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdio.h" 2 3 4 547 | # 3 "src/main.c" 2 548 | 549 | int main() 550 | { 551 | 552 | printf("Hello World!"); 553 | 554 | return 0; 555 | } 556 | -------------------------------------------------------------------------------- /Test/src/main.s: -------------------------------------------------------------------------------- 1 | .section __TEXT,__text,regular,pure_instructions 2 | .build_version macos, 13, 0 sdk_version 13, 1 3 | .globl _main ; -- Begin function main 4 | .p2align 2 5 | _main: ; @main 6 | .cfi_startproc 7 | ; %bb.0: 8 | sub sp, sp, #32 9 | stp x29, x30, [sp, #16] ; 16-byte Folded Spill 10 | add x29, sp, #16 11 | .cfi_def_cfa w29, 16 12 | .cfi_offset w30, -8 13 | .cfi_offset w29, -16 14 | mov w8, #0 15 | str w8, [sp, #8] ; 4-byte Folded Spill 16 | stur wzr, [x29, #-4] 17 | adrp x0, l_.str@PAGE 18 | add x0, x0, l_.str@PAGEOFF 19 | bl _printf 20 | ldr w0, [sp, #8] ; 4-byte Folded Reload 21 | ldp x29, x30, [sp, #16] ; 16-byte Folded Reload 22 | add sp, sp, #32 23 | ret 24 | .cfi_endproc 25 | ; -- End function 26 | .section __TEXT,__cstring,cstring_literals 27 | l_.str: ; @.str 28 | .asciz "Hello World!" 29 | 30 | .subsections_via_symbols 31 | -------------------------------------------------------------------------------- /imgs/QRcode.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WohimLee/GNC-Tutorial/0ef1c05ab78a76fb1d1baaf51b17aa9782922ead/imgs/QRcode.png -------------------------------------------------------------------------------- /update.sh: -------------------------------------------------------------------------------- 1 | git add . 2 | git commit -m "update" 3 | git push --------------------------------------------------------------------------------