c
Before Width: | Height: | Size: 48 KiB After Width: | Height: | Size: 96 KiB |
Before Width: | Height: | Size: 27 KiB After Width: | Height: | Size: 48 KiB |
Before Width: | Height: | Size: 66 KiB After Width: | Height: | Size: 27 KiB |
Before Width: | Height: | Size: 40 KiB After Width: | Height: | Size: 66 KiB |
Before Width: | Height: | Size: 308 KiB |
BIN
docs/notes/01_c-basic/01_xdx/assets/104.png
Normal file
After Width: | Height: | Size: 40 KiB |
Before Width: | Height: | Size: 359 KiB After Width: | Height: | Size: 308 KiB |
Before Width: | Height: | Size: 590 KiB After Width: | Height: | Size: 359 KiB |
BIN
docs/notes/01_c-basic/01_xdx/assets/107.gif
Normal file
After Width: | Height: | Size: 590 KiB |
Before Width: | Height: | Size: 26 KiB |
Before Width: | Height: | Size: 44 KiB After Width: | Height: | Size: 26 KiB |
Before Width: | Height: | Size: 62 KiB After Width: | Height: | Size: 44 KiB |
Before Width: | Height: | Size: 675 KiB |
Before Width: | Height: | Size: 27 KiB After Width: | Height: | Size: 62 KiB |
Before Width: | Height: | Size: 288 KiB After Width: | Height: | Size: 675 KiB |
Before Width: | Height: | Size: 821 KiB After Width: | Height: | Size: 288 KiB |
BIN
docs/notes/01_c-basic/01_xdx/assets/113.gif
Normal file
After Width: | Height: | Size: 821 KiB |
Before Width: | Height: | Size: 378 KiB |
Before Width: | Height: | Size: 123 KiB After Width: | Height: | Size: 123 KiB |
Before Width: | Height: | Size: 590 KiB After Width: | Height: | Size: 378 KiB |
Before Width: | Height: | Size: 36 KiB |
Before Width: | Height: | Size: 240 KiB After Width: | Height: | Size: 240 KiB |
Before Width: | Height: | Size: 52 KiB After Width: | Height: | Size: 36 KiB |
Before Width: | Height: | Size: 41 KiB |
Before Width: | Height: | Size: 62 KiB After Width: | Height: | Size: 52 KiB |
Before Width: | Height: | Size: 29 KiB After Width: | Height: | Size: 41 KiB |
Before Width: | Height: | Size: 53 KiB After Width: | Height: | Size: 29 KiB |
Before Width: | Height: | Size: 38 KiB After Width: | Height: | Size: 53 KiB |
Before Width: | Height: | Size: 39 KiB After Width: | Height: | Size: 38 KiB |
Before Width: | Height: | Size: 42 KiB After Width: | Height: | Size: 39 KiB |
Before Width: | Height: | Size: 1.1 MiB After Width: | Height: | Size: 42 KiB |
Before Width: | Height: | Size: 35 KiB |
Before Width: | Height: | Size: 310 KiB After Width: | Height: | Size: 310 KiB |
Before Width: | Height: | Size: 2.0 MiB After Width: | Height: | Size: 35 KiB |
Before Width: | Height: | Size: 572 KiB After Width: | Height: | Size: 2.0 MiB |
Before Width: | Height: | Size: 1.3 MiB After Width: | Height: | Size: 572 KiB |
Before Width: | Height: | Size: 601 KiB After Width: | Height: | Size: 1.3 MiB |
Before Width: | Height: | Size: 681 KiB After Width: | Height: | Size: 601 KiB |
BIN
docs/notes/01_c-basic/01_xdx/assets/131.gif
Normal file
After Width: | Height: | Size: 681 KiB |
Before Width: | Height: | Size: 205 KiB |
Before Width: | Height: | Size: 153 KiB After Width: | Height: | Size: 205 KiB |
BIN
docs/notes/01_c-basic/01_xdx/assets/133.png
Normal file
After Width: | Height: | Size: 153 KiB |
Before Width: | Height: | Size: 614 KiB After Width: | Height: | Size: 614 KiB |
Before Width: | Height: | Size: 1.0 MiB |
Before Width: | Height: | Size: 62 KiB After Width: | Height: | Size: 62 KiB |
Before Width: | Height: | Size: 38 KiB After Width: | Height: | Size: 1.0 MiB |
Before Width: | Height: | Size: 209 KiB |
Before Width: | Height: | Size: 34 KiB After Width: | Height: | Size: 34 KiB |
Before Width: | Height: | Size: 1.1 MiB After Width: | Height: | Size: 209 KiB |
Before Width: | Height: | Size: 456 KiB After Width: | Height: | Size: 1.1 MiB |
Before Width: | Height: | Size: 388 KiB After Width: | Height: | Size: 456 KiB |
Before Width: | Height: | Size: 379 KiB After Width: | Height: | Size: 388 KiB |
BIN
docs/notes/01_c-basic/01_xdx/assets/142.gif
Normal file
After Width: | Height: | Size: 379 KiB |
Before Width: | Height: | Size: 16 KiB |
Before Width: | Height: | Size: 51 KiB After Width: | Height: | Size: 16 KiB |
BIN
docs/notes/01_c-basic/01_xdx/assets/144.png
Normal file
After Width: | Height: | Size: 51 KiB |
Before Width: | Height: | Size: 21 KiB After Width: | Height: | Size: 21 KiB |
Before Width: | Height: | Size: 11 KiB |
Before Width: | Height: | Size: 278 KiB |
Before Width: | Height: | Size: 205 KiB After Width: | Height: | Size: 11 KiB |
Before Width: | Height: | Size: 316 KiB After Width: | Height: | Size: 278 KiB |
Before Width: | Height: | Size: 290 KiB After Width: | Height: | Size: 316 KiB |
Before Width: | Height: | Size: 1.0 MiB After Width: | Height: | Size: 290 KiB |
Before Width: | Height: | Size: 150 KiB After Width: | Height: | Size: 1.0 MiB |
Before Width: | Height: | Size: 440 KiB After Width: | Height: | Size: 150 KiB |
BIN
docs/notes/01_c-basic/01_xdx/assets/152.gif
Normal file
After Width: | Height: | Size: 440 KiB |
Before Width: | Height: | Size: 64 KiB |
Before Width: | Height: | Size: 20 KiB After Width: | Height: | Size: 64 KiB |
Before Width: | Height: | Size: 232 KiB |
BIN
docs/notes/01_c-basic/01_xdx/assets/154.png
Normal file
After Width: | Height: | Size: 20 KiB |
Before Width: | Height: | Size: 536 KiB After Width: | Height: | Size: 232 KiB |
Before Width: | Height: | Size: 134 KiB After Width: | Height: | Size: 536 KiB |
Before Width: | Height: | Size: 109 KiB After Width: | Height: | Size: 134 KiB |
Before Width: | Height: | Size: 40 KiB After Width: | Height: | Size: 109 KiB |
Before Width: | Height: | Size: 37 KiB After Width: | Height: | Size: 40 KiB |
Before Width: | Height: | Size: 179 KiB After Width: | Height: | Size: 37 KiB |
Before Width: | Height: | Size: 145 KiB After Width: | Height: | Size: 179 KiB |
Before Width: | Height: | Size: 7.2 MiB After Width: | Height: | Size: 145 KiB |
Before Width: | Height: | Size: 1.4 MiB After Width: | Height: | Size: 7.2 MiB |
Before Width: | Height: | Size: 718 KiB After Width: | Height: | Size: 1.4 MiB |
BIN
docs/notes/01_c-basic/01_xdx/assets/165.gif
Normal file
After Width: | Height: | Size: 718 KiB |
Before Width: | Height: | Size: 69 KiB After Width: | Height: | Size: 69 KiB |
Before Width: | Height: | Size: 926 KiB After Width: | Height: | Size: 926 KiB |
Before Width: | Height: | Size: 169 KiB |
Before Width: | Height: | Size: 137 KiB After Width: | Height: | Size: 169 KiB |
Before Width: | Height: | Size: 169 KiB After Width: | Height: | Size: 137 KiB |
Before Width: | Height: | Size: 393 KiB |
BIN
docs/notes/01_c-basic/01_xdx/assets/170.png
Normal file
After Width: | Height: | Size: 169 KiB |
Before Width: | Height: | Size: 758 KiB After Width: | Height: | Size: 393 KiB |
BIN
docs/notes/01_c-basic/01_xdx/assets/172.gif
Normal file
After Width: | Height: | Size: 758 KiB |
Before Width: | Height: | Size: 178 KiB |
Before Width: | Height: | Size: 163 KiB After Width: | Height: | Size: 178 KiB |
BIN
docs/notes/01_c-basic/01_xdx/assets/174.png
Normal file
After Width: | Height: | Size: 163 KiB |
Before Width: | Height: | Size: 77 KiB After Width: | Height: | Size: 46 KiB |
Before Width: | Height: | Size: 193 KiB |
BIN
docs/notes/01_c-basic/01_xdx/assets/86.png
Normal file
After Width: | Height: | Size: 77 KiB |
Before Width: | Height: | Size: 46 KiB After Width: | Height: | Size: 193 KiB |
Before Width: | Height: | Size: 48 KiB After Width: | Height: | Size: 46 KiB |
BIN
docs/notes/01_c-basic/01_xdx/assets/89.gif
Normal file
After Width: | Height: | Size: 48 KiB |
Before Width: | Height: | Size: 571 KiB |
Before Width: | Height: | Size: 54 KiB After Width: | Height: | Size: 54 KiB |
Before Width: | Height: | Size: 505 KiB After Width: | Height: | Size: 571 KiB |
BIN
docs/notes/01_c-basic/01_xdx/assets/92.gif
Normal file
After Width: | Height: | Size: 505 KiB |
Before Width: | Height: | Size: 151 KiB |
Before Width: | Height: | Size: 164 KiB After Width: | Height: | Size: 151 KiB |
Before Width: | Height: | Size: 818 KiB |
BIN
docs/notes/01_c-basic/01_xdx/assets/94.png
Normal file
After Width: | Height: | Size: 164 KiB |
Before Width: | Height: | Size: 242 KiB After Width: | Height: | Size: 818 KiB |
Before Width: | Height: | Size: 190 KiB After Width: | Height: | Size: 242 KiB |
Before Width: | Height: | Size: 461 KiB After Width: | Height: | Size: 190 KiB |
BIN
docs/notes/01_c-basic/01_xdx/assets/98.gif
Normal file
After Width: | Height: | Size: 461 KiB |
Before Width: | Height: | Size: 187 KiB |
Before Width: | Height: | Size: 96 KiB After Width: | Height: | Size: 187 KiB |
@ -226,7 +226,7 @@ pacman -Sy mingw-w64-ucrt-x86_64-toolchain --noconfirm # 安装开发 gcc 相关
|
||||
> * ② 严格来讲, VS Code 属于编辑器,而不是 IDE;但是,可以通过安装各种插件,来完成 IDE 的功能;而 Visual Studio 和 CLion 属于 IDE。
|
||||
> * ③ 在实际开发中,使用 IDE 进行编程并不是一种非常丢人的事情。而使用编辑器,如:`记事本`、`vi/vim` 等,进行编程,也并不是一件非常骄傲的事情。可能有些人会在网上发布这样的言论:“学习编程,刚开始需要使用`记事本`或 `vi/vim`等简单的编辑器软件,不要使用 IDE ”,目的可能是为了让初学者熟悉编程的基础概念和语法,并避免依赖 IDE 的辅助功能。但是,这种方法或许可以起到锻炼基础技能的功能,但这并不意味着 IDE 就不适合初学者。事实上,许多 IDE 还提供了初学者友好的界面和工具,可以帮助新手更快地入门和理解编程。
|
||||
|
||||
## 1.43 IDE 的安装和配置
|
||||
## 1.4 IDE 的安装和配置
|
||||
|
||||
### 1.4.1 CLion
|
||||
|
||||
@ -466,6 +466,46 @@ pacman -Sy mingw-w64-ucrt-x86_64-toolchain --noconfirm # 安装开发 gcc 相关
|
||||
|
||||
![](./assets/84.png)
|
||||
|
||||
## 1.5 什么是工程/项目 ?
|
||||
|
||||
### 1.5.1 概述
|
||||
|
||||
* 一个真正的软件往往包含多项功能,每一项功能都需要几十行、几千行甚至几万行的代码,如果我们将这些代码都放到一个源文件中,不但打开的速度极慢,代码的编写和维护也会变得非常困难。
|
||||
* 在实际开发中,随着软件规模的增加,代码的复杂性也会显著提升,为了提高代码的易读性、维护性等,程序员会将代码按照功能分别放到不同的源文件中。
|
||||
|
||||
> [!NOTE]
|
||||
>
|
||||
> 需要说明的是,一个真正的软件除了源代码之外,往往还会包括图片、视频、音频、库(框架)等其它资源,这些也是一个个的文件。
|
||||
|
||||
* 为了有效的管理这些种类繁杂、数目众多的文件,我们会将这些文件按照功能放到不同的目录中进行统一管理,并且这个目录下只存放与当前程序有关的资源。其实,这就是工程或项目。
|
||||
|
||||
> [!NOTE]
|
||||
>
|
||||
> 总结:
|
||||
>
|
||||
> * ① 随着软件规模的增加,代码的复杂性也会显著提升。将代码分割成多个模块或文件并分别管理,可以减少每个文件的复杂度,使代码更易读、易理解、易维护。工程提供了一个结构化的环境,将这些文件组织在一个系统化的目录结构中。
|
||||
> * ② 除了代码,软件开发还涉及到各种资源的管理,如:图片、音频、视频、配置文件等。工程能够帮助开发者将这些资源合理地分类存放,并与代码一同管理,确保它们在开发、编译和运行时能被正确引用。
|
||||
|
||||
* 许多 IDE 都提供了工程或项目的概念,其目的就是为了帮助开发者合理的管理软件开发中所需要的资源,如:图片、视频、音频、库(框架)等。
|
||||
|
||||
![](./assets/85.png)
|
||||
|
||||
### 1.5.2 工程类型/项目类型
|
||||
|
||||
* 程序或软件是一个非常宽泛的概念,它可以细分为很多种类,如下所示:
|
||||
* 控制台程序(Console Application):控制台程序是一种不具备图形用户界面的程序,它通过文本方式与用户交互,通常运行在命令行窗口(黑底白字的终端),如:Unix/Linux 中的`ls`命令、Windows 中的`cmd.exe`等。
|
||||
* GUI 程序(Graphical User Interface Program):GUI 程序是一种具有图形用户界面的程序,通过窗口、按钮、菜单等图形控件与用户交互,如:微信、QQ 等。
|
||||
* 静态库和动态库:
|
||||
* 静态库指的是在编译时包含到程序中的库,程序不依赖外部文件运行,如:在 C/C++ 中,静态库通常以`.lib`(Windows)或`.a`(Unix/Linux)为扩展名。
|
||||
* 动态库指的是在运行时加载的库,允许多个程序共享,并且程序在运行时依赖这些库,如: 在Windows中,动态库通常以`.dll`为扩展名;在 Unix/Linux 中,以`.so`为扩展名。
|
||||
|
||||
* 不同类型的程序(控制台程序、GUI 程序、静态库、动态库等)需要不同的配置和文件结构,因此在 IDE 中创建项目时,选择正确的工程类型非常重要。不同的工程类型决定了 IDE 为你生成的初始文件、目录结构,以及预设的一些编译和链接参数。
|
||||
|
||||
> [!IMPORTANT]
|
||||
>
|
||||
> * ① 控制台程序适合初学者,因为它更简单,没有复杂的界面元素,开发时可以专注于逻辑和代码本身。
|
||||
> * ② 而 GUI 程序则涉及到用户界面设计和事件驱动编程,更适合有一定编程基础的人进行学习和开发。
|
||||
|
||||
|
||||
|
||||
# 第二章:C 语言入门(HelloWorld,⭐)
|
||||
@ -474,7 +514,7 @@ pacman -Sy mingw-w64-ucrt-x86_64-toolchain --noconfirm # 安装开发 gcc 相关
|
||||
|
||||
* ① 新建一个 `HelloWorld.c` 的文件:
|
||||
|
||||
![](./assets/85.png)
|
||||
![](./assets/86.png)
|
||||
|
||||
* ② 通过`记事本`等软件打开该文件,输入如下的代码,并保存:
|
||||
|
||||
@ -487,7 +527,7 @@ int main(){
|
||||
}
|
||||
```
|
||||
|
||||
![](./assets/86.gif)
|
||||
![](./assets/87.gif)
|
||||
|
||||
* ③ 通过 `gcc` 命令编译该文件:
|
||||
|
||||
@ -495,7 +535,7 @@ int main(){
|
||||
gcc HelloWorld.c -o HelloWorld.exe
|
||||
```
|
||||
|
||||
![](./assets/87.gif)
|
||||
![](./assets/88.gif)
|
||||
|
||||
* ④ 执行:
|
||||
|
||||
@ -503,28 +543,28 @@ gcc HelloWorld.c -o HelloWorld.exe
|
||||
./HelloWorld.exe
|
||||
```
|
||||
|
||||
![](./assets/88.gif)
|
||||
![](./assets/89.gif)
|
||||
|
||||
## 2.2 VS Code 版
|
||||
|
||||
* ① 新建一个`空`文件夹(目录),用于存放代码:
|
||||
|
||||
![](./assets/89.png)
|
||||
![](./assets/90.png)
|
||||
|
||||
* ② 通过 `vscode` 打开该目录:
|
||||
|
||||
![](./assets/90.gif)
|
||||
![](./assets/91.gif)
|
||||
|
||||
* ③ 在 `vscode` 中新建 `HelloWorld.c` 文件:
|
||||
|
||||
![](./assets/91.gif)
|
||||
![](./assets/92.gif)
|
||||
|
||||
* ④ 设置 VSCode 中 C/C++ 的代码格式为行尾风格(可选):
|
||||
|
||||
![](./assets/92.png)
|
||||
|
||||
![](./assets/93.png)
|
||||
|
||||
![](./assets/94.png)
|
||||
|
||||
* ⑤ 编写如下的代码,并保存:
|
||||
|
||||
```c
|
||||
@ -536,7 +576,7 @@ int main(){
|
||||
}
|
||||
```
|
||||
|
||||
![](./assets/94.gif)
|
||||
![](./assets/95.gif)
|
||||
|
||||
* ⑥ 通过 `gcc` 命令编译该文件:
|
||||
|
||||
@ -544,7 +584,7 @@ int main(){
|
||||
gcc HelloWorld.c -o HelloWorld.exe
|
||||
```
|
||||
|
||||
![](./assets/95.gif)
|
||||
![](./assets/96.gif)
|
||||
|
||||
* ⑦ 执行:
|
||||
|
||||
@ -552,33 +592,33 @@ gcc HelloWorld.c -o HelloWorld.exe
|
||||
./HelloWorld.exe
|
||||
```
|
||||
|
||||
![](./assets/96.gif)
|
||||
![](./assets/97.gif)
|
||||
|
||||
* ⑧ 安装 Code Runner 插件(步骤略),实现右键直接编译执行(可选):
|
||||
|
||||
![](./assets/97.gif)
|
||||
![](./assets/98.gif)
|
||||
|
||||
## 2.3 VS 版
|
||||
|
||||
* ① 新建空项目:
|
||||
|
||||
![](./assets/98.png)
|
||||
|
||||
![](./assets/99.png)
|
||||
|
||||
![](./assets/100.png)
|
||||
|
||||
![](./assets/101.png)
|
||||
|
||||
* ② 打开`解决方案资源管理器`:
|
||||
|
||||
![](./assets/102.png)
|
||||
|
||||
* ② 打开`解决方案资源管理器`:
|
||||
|
||||
![](./assets/103.png)
|
||||
|
||||
![](./assets/104.png)
|
||||
|
||||
* ③ 新建 `HelloWorld.c` 文件:
|
||||
|
||||
![](./assets/104.gif)
|
||||
![](./assets/105.gif)
|
||||
|
||||
* ④ 编写如下代码,并保存:
|
||||
|
||||
@ -591,22 +631,22 @@ int main(){
|
||||
}
|
||||
```
|
||||
|
||||
![](./assets/105.gif)
|
||||
![](./assets/106.gif)
|
||||
|
||||
* ⑤ 编译和执行:
|
||||
|
||||
![](./assets/106.gif)
|
||||
![](./assets/107.gif)
|
||||
|
||||
## 2.4 CLion 版
|
||||
|
||||
* ① 新建空项目:
|
||||
|
||||
![](./assets/107.png)
|
||||
|
||||
![](./assets/108.png)
|
||||
|
||||
![](./assets/109.png)
|
||||
|
||||
![](./assets/110.png)
|
||||
|
||||
* ② 编写如下代码,并保存:
|
||||
|
||||
```c
|
||||
@ -618,21 +658,21 @@ int main(){
|
||||
}
|
||||
```
|
||||
|
||||
![](./assets/110.gif)
|
||||
![](./assets/111.gif)
|
||||
|
||||
* ③ 编译和运行:
|
||||
|
||||
![](./assets/111.gif)
|
||||
![](./assets/112.gif)
|
||||
|
||||
* ④ 默认情况下,一个项目只能有一个 c 源文件包含 main 函数,但是 CLion 可以有多个,如下:
|
||||
|
||||
![](./assets/112.gif)
|
||||
![](./assets/113.gif)
|
||||
|
||||
* ⑤ 如果之后,有中文乱码问题,那么请做如下步骤:
|
||||
|
||||
![](./assets/113.png)
|
||||
![](./assets/114.png)
|
||||
|
||||
![](./assets/114.gif)
|
||||
![](./assets/115.gif)
|
||||
|
||||
> [!NOTE]
|
||||
>
|
||||
@ -1014,7 +1054,7 @@ printf("Hello World\n");
|
||||
* WSL2,全称为 Windows Subsystem for Linux 2,是微软提供的一种技术,允许用户在 Windows 操作系统上运行 Linux 内核。WSL2 是 WSL1 的升级版,它引入了一个真正的 Linux 内核来代替 WSL1 中使用的兼容层,从而提供更高的性能和更广泛的系统调用支持。
|
||||
* 和传统的虚拟化技术的对比,如下所示:
|
||||
|
||||
![](./assets/115.svg)
|
||||
![](./assets/116.svg)
|
||||
|
||||
> [!NOTE]
|
||||
>
|
||||
@ -1039,11 +1079,11 @@ printf("Hello World\n");
|
||||
|
||||
* ① BIOS 或 UEFI 中,开启虚拟化:步骤略。
|
||||
|
||||
![](./assets/116.png)
|
||||
![](./assets/117.png)
|
||||
|
||||
* ② 查看是否开启了虚拟化:
|
||||
|
||||
![](./assets/117.png)
|
||||
![](./assets/118.png)
|
||||
|
||||
* ③ 启用适用于 Linux 的 Windows 子系统:
|
||||
|
||||
@ -1057,7 +1097,7 @@ dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux
|
||||
|
||||
|
||||
|
||||
![](./assets/118.gif)
|
||||
![](./assets/119.gif)
|
||||
|
||||
* ④ 启用虚拟机功能:
|
||||
|
||||
@ -1069,7 +1109,7 @@ dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /nores
|
||||
>
|
||||
> 以管理员身份打开 PowerShell 并运行,执行完上述命令之后,如果提示需要重启计算机,那就重启计算机。
|
||||
|
||||
![](./assets/119.gif)
|
||||
![](./assets/120.gif)
|
||||
|
||||
* ⑤ 更新 Linux 内核包:
|
||||
|
||||
@ -1081,7 +1121,7 @@ wsl --update
|
||||
>
|
||||
> wsl2 的最新 Linux 内核包托管在 GitHub 上,某些国家可能会污染 Github 相关的域名,那么就需要手动下载,然后安装即可,下载地址在[这里](https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi)。
|
||||
|
||||
![](./assets/120.gif)
|
||||
![](./assets/121.gif)
|
||||
|
||||
* ⑥ 将 wsl2 设置为默认版本:
|
||||
|
||||
@ -1089,7 +1129,7 @@ wsl --update
|
||||
wsl --set-default-version 2
|
||||
```
|
||||
|
||||
![](./assets/121.gif)
|
||||
![](./assets/122.gif)
|
||||
|
||||
* ⑦ 查看官方在线支持的 Linux 版本:
|
||||
|
||||
@ -1097,7 +1137,7 @@ wsl --set-default-version 2
|
||||
wsl --list --online
|
||||
```
|
||||
|
||||
![](./assets/122.gif)
|
||||
![](./assets/123.gif)
|
||||
|
||||
* ⑧ 安装指定版本的 Linux :
|
||||
|
||||
@ -1109,11 +1149,11 @@ wsl --install Ubuntu-24.04
|
||||
>
|
||||
> 官方支持的 Linux 版本,托管在 Github 上,某些国家可能会污染 Github 的域名;此时,要么走科学上网,要么在 `Microsoft Store` 中搜索并安装。
|
||||
|
||||
![](./assets/123.gif)
|
||||
![](./assets/124.gif)
|
||||
|
||||
* ⑨ 在 Microsoft Store 中搜索并安装(可选):
|
||||
|
||||
![](./assets/124.png)
|
||||
![](./assets/125.png)
|
||||
|
||||
* ⑩ 查询本地安装的 Linux 版本:
|
||||
|
||||
@ -1121,7 +1161,7 @@ wsl --install Ubuntu-24.04
|
||||
wsl --list
|
||||
```
|
||||
|
||||
![](./assets/125.gif)
|
||||
![](./assets/126.gif)
|
||||
|
||||
### 6.1.3 配置 WSL2
|
||||
|
||||
@ -1136,11 +1176,11 @@ sudo dnf install make -y # 安装 make
|
||||
sudo dnf install gdb -y # 安装 gdb
|
||||
```
|
||||
|
||||
![](./assets/126.gif)
|
||||
![](./assets/127.gif)
|
||||
|
||||
* 可以通过 CLion 测试是否安装成功:
|
||||
|
||||
![](./assets/127.gif)
|
||||
![](./assets/128.gif)
|
||||
|
||||
### 6.1.4 配置 WSL2
|
||||
|
||||
@ -1154,7 +1194,7 @@ sudo apt install cmake -y # 安装 cmake
|
||||
sudo apt install gdb -y # 安装 gdb
|
||||
```
|
||||
|
||||
![](./assets/128.gif)
|
||||
![](./assets/129.gif)
|
||||
|
||||
* 可以通过 CLion 测试是否安装成功:
|
||||
|
||||
@ -1162,19 +1202,19 @@ sudo apt install gdb -y # 安装 gdb
|
||||
>
|
||||
> 默认仓库中的 GDB 的版本会很高,CLion 会不支持,那么就需要手动下载源码,自行编译,本次不再演示!!!
|
||||
|
||||
![](./assets/129.gif)
|
||||
![](./assets/130.gif)
|
||||
|
||||
## 6.2 切换 CLion 中的 cmake 的工具链
|
||||
|
||||
* 可以在 CLoin 中切换 cmake 的工具链,以便支持不同平台的 cmake ,即:
|
||||
|
||||
![](./assets/130.gif)
|
||||
![](./assets/131.gif)
|
||||
|
||||
## 6.3 修改 CMakeLists.txt 文件
|
||||
|
||||
* 前文也提到了,在一个 C 语言项目中,只能有一个 main() 函数;但是,我们可以修改 `CMakeLists.txt` 文件的内容,以便其支持在一个 C 语言项目中,可以有多个包含 main() 函数的文件,如下所示:
|
||||
|
||||
![](./assets/131.png)
|
||||
![](./assets/132.png)
|
||||
|
||||
* `CMakeLists.txt` 的内容,如下所示:
|
||||
|
||||
@ -1249,7 +1289,7 @@ endforeach ()
|
||||
|
||||
* 配置 `.clang-format` 格式化文件,以便写代码的时候,可以自动保存并格式化 C 程序代码,如下所示:
|
||||
|
||||
![](./assets/132.png)
|
||||
![](./assets/133.png)
|
||||
|
||||
* `.clang-format` 的内容,如下所示:
|
||||
|
||||
@ -1291,13 +1331,13 @@ SpacesInCStyleCastParentheses: false
|
||||
|
||||
* CLion 中配置`保存`的时候`自动格式化`,即:
|
||||
|
||||
![](./assets/133.gif)
|
||||
![](./assets/134.gif)
|
||||
|
||||
## 6.5 配置 .gitignore 文件
|
||||
|
||||
* 需要在项目中,配置 `.gitignore` 文件,以便在提交代码到 Git 仓库的时候,忽略某些文件或目录,如下所示:
|
||||
|
||||
![](./assets/134.png)
|
||||
![](./assets/135.png)
|
||||
|
||||
* `.gitignore` 文件的内容,如下所示:
|
||||
|
||||
@ -1316,7 +1356,7 @@ build
|
||||
>
|
||||
> 如果是复制并粘贴一个文件到项目中,请点击`重新加载 CMake 项目`!!!
|
||||
|
||||
![](./assets/135.gif)
|
||||
![](./assets/136.gif)
|
||||
|
||||
|
||||
|
||||
@ -1326,7 +1366,7 @@ build
|
||||
|
||||
* C 程序的编译过程,如下所示:
|
||||
|
||||
![](./assets/136.png)
|
||||
![](./assets/137.png)
|
||||
|
||||
* 过程 ① :编写(编辑)源代码,即:编写 C 语言源程序代码,并以文件的形式存储在磁盘中。
|
||||
|
||||
@ -1382,7 +1422,7 @@ build
|
||||
gcc HelloWorld.c -o HelloWorld.exe
|
||||
```
|
||||
|
||||
![](./assets/137.gif)
|
||||
![](./assets/138.gif)
|
||||
|
||||
## 7.4 通过 gcc 分步编译
|
||||
|
||||
@ -1420,7 +1460,7 @@ gcc 源文件.o -o 源文件.exe # 在 Linux 中,通常以 .out 结尾;在 W
|
||||
gcc -E HelloWorld.c -o HelloWorld.i
|
||||
```
|
||||
|
||||
![](./assets/138.gif)
|
||||
![](./assets/139.gif)
|
||||
|
||||
|
||||
|
||||
@ -1430,7 +1470,7 @@ gcc -E HelloWorld.c -o HelloWorld.i
|
||||
gcc -S HelloWorld.i -o HelloWorld.s
|
||||
```
|
||||
|
||||
![](./assets/139.gif)
|
||||
![](./assets/140.gif)
|
||||
|
||||
|
||||
|
||||
@ -1440,7 +1480,7 @@ gcc -S HelloWorld.i -o HelloWorld.s
|
||||
gcc -c HelloWorld.s -o HelloWorld.o
|
||||
```
|
||||
|
||||
![](./assets/140.gif)
|
||||
![](./assets/141.gif)
|
||||
|
||||
|
||||
|
||||
@ -1450,7 +1490,7 @@ gcc -c HelloWorld.s -o HelloWorld.o
|
||||
gcc HelloWorld.o -o HelloWorld.exe
|
||||
```
|
||||
|
||||
![](./assets/141.gif)
|
||||
![](./assets/142.gif)
|
||||
|
||||
# 第八章:附录
|
||||
|
||||
@ -1458,7 +1498,7 @@ gcc HelloWorld.o -o HelloWorld.exe
|
||||
|
||||
* 在安装和配置 WSL2 之后,可能会出现如下的提示,即:
|
||||
|
||||
![](./assets/142.png)
|
||||
![](./assets/143.png)
|
||||
|
||||
* 那么,只需要修改 `%USERPROFILE%\.wslconfig`文件,内容如下:
|
||||
|
||||
@ -1475,7 +1515,7 @@ bestEffortDnsParsing=true
|
||||
useWindowsDnsCache=true
|
||||
```
|
||||
|
||||
![](./assets/143.png)
|
||||
![](./assets/144.png)
|
||||
|
||||
* 在命令行中,执行如下的命令:
|
||||
|
||||
@ -1483,21 +1523,21 @@ useWindowsDnsCache=true
|
||||
wsl --shutdown
|
||||
```
|
||||
|
||||
![](./assets/144.gif)
|
||||
![](./assets/145.gif)
|
||||
|
||||
* 此时,再打开终端,就没有这种提示了:
|
||||
|
||||
![](./assets/145.png)
|
||||
![](./assets/146.png)
|
||||
|
||||
## 8.2 CLion 调试问题
|
||||
|
||||
* 在 CLion 中进行 run(运行)程序的时候,对于 `printf` 函数或 `scanf` 函数很正常,如下所示:
|
||||
|
||||
![](./assets/146.gif)
|
||||
![](./assets/147.gif)
|
||||
|
||||
* 但是,当我们 debug(调试) 的时候,对于 `printf` 函数或 `scanf` 函数会一直没有输出,如下所示:
|
||||
|
||||
![](./assets/147.gif)
|
||||
![](./assets/148.gif)
|
||||
|
||||
* 原因是 `scanf` 函数并不是直接让用户从键盘输入数据,而是先检查缓冲区,处理缓冲区中的数据;当遇到 `scanf` 函数时,程序会先检查输入缓冲区中是否有数据,所以解决方案就是`禁用缓冲区`,如下所示:
|
||||
|
||||
@ -1522,7 +1562,7 @@ int main() {
|
||||
|
||||
* 那么,就会达到我们想要的效果了,如下所示:
|
||||
|
||||
![](./assets/148.gif)
|
||||
![](./assets/149.gif)
|
||||
|
||||
## 8.3 内存泄露检测
|
||||
|
||||
@ -1547,7 +1587,7 @@ dnf -y upgrade && dnf -y install valgrind # AlmaLinux
|
||||
apt -y update && apt -y upgrade && apt -y install valgrind # Ubuntu
|
||||
```
|
||||
|
||||
![](./assets/149.gif)
|
||||
![](./assets/150.gif)
|
||||
|
||||
* 查看 valgrind 可执行文件的安装位置:
|
||||
|
||||
@ -1555,17 +1595,17 @@ apt -y update && apt -y upgrade && apt -y install valgrind # Ubuntu
|
||||
which valgrind
|
||||
```
|
||||
|
||||
![](./assets/150.gif)
|
||||
![](./assets/151.gif)
|
||||
|
||||
### 8.3.3 整合
|
||||
|
||||
* CLion 中将工具链设置为 WSL2 :
|
||||
|
||||
![](./assets/151.gif)
|
||||
![](./assets/152.gif)
|
||||
|
||||
* CLion 中配置 valgrind 的路径:
|
||||
|
||||
![](./assets/152.png)
|
||||
![](./assets/153.png)
|
||||
|
||||
* 查看 WSL2 中 cmake 的版本:
|
||||
|
||||
@ -1573,7 +1613,7 @@ which valgrind
|
||||
cmake --version
|
||||
```
|
||||
|
||||
![](./assets/153.png)
|
||||
![](./assets/154.png)
|
||||
|
||||
* 修改项目中 CMakeLists.txt 中 cmake 的版本:
|
||||
|
||||
@ -1646,11 +1686,11 @@ endforeach ()
|
||||
|
||||
* 在 CLion 中正常运行代码:
|
||||
|
||||
![](./assets/154.gif)
|
||||
![](./assets/155.gif)
|
||||
|
||||
* 在 CLion 中通过 valgrind 运行代码:
|
||||
|
||||
![](./assets/155.gif)
|
||||
![](./assets/156.gif)
|
||||
|
||||
## 8.4 性能分析
|
||||
|
||||
@ -1668,7 +1708,7 @@ endforeach ()
|
||||
dnf -y install perf
|
||||
```
|
||||
|
||||
![](./assets/156.gif)
|
||||
![](./assets/157.gif)
|
||||
|
||||
#### 8.4.2.2 Ubuntu 22.04
|
||||
|
||||
@ -1679,7 +1719,7 @@ apt -y update \
|
||||
&& apt -y install linux-tools-common linux-tools-generic linux-tools-$(uname -r)
|
||||
```
|
||||
|
||||
![](./assets/157.gif)
|
||||
![](./assets/158.gif)
|
||||
|
||||
> [!NOTE]
|
||||
>
|
||||
@ -1691,7 +1731,7 @@ apt -y update \
|
||||
uname -sr
|
||||
```
|
||||
|
||||
![](./assets/158.gif)
|
||||
![](./assets/159.gif)
|
||||
|
||||
* 设置环境变量,方便后续引用:
|
||||
|
||||
@ -1699,7 +1739,7 @@ uname -sr
|
||||
export KERNEL_VERSION=$(uname -r | cut -d'-' -f1)
|
||||
```
|
||||
|
||||
![](./assets/159.gif)
|
||||
![](./assets/160.gif)
|
||||
|
||||
* 安装依赖库:
|
||||
|
||||
@ -1713,7 +1753,7 @@ apt -y update && \
|
||||
libelf++0 libbfb0-dev python-dev-is-python3
|
||||
```
|
||||
|
||||
![](./assets/160.gif)
|
||||
![](./assets/161.gif)
|
||||
|
||||
* 下载源码:
|
||||
|
||||
@ -1724,7 +1764,7 @@ git clone \
|
||||
https://github.com/microsoft/WSL2-Linux-Kernel.git
|
||||
```
|
||||
|
||||
![](./assets/161.gif)
|
||||
![](./assets/162.gif)
|
||||
|
||||
* 编译内核代码:
|
||||
|
||||
@ -1736,7 +1776,7 @@ cd WSL2-Linux-Kernel
|
||||
make -j $(nproc) KCONFIG_CONFIG=Microsoft/config-wsl
|
||||
```
|
||||
|
||||
![](./assets/162.gif)
|
||||
![](./assets/163.gif)
|
||||
|
||||
* 编译 perf 工具:
|
||||
|
||||
@ -1748,7 +1788,7 @@ cd tools/perf
|
||||
make clean && make
|
||||
```
|
||||
|
||||
![](./assets/163.gif)
|
||||
![](./assets/164.gif)
|
||||
|
||||
* 复制到 PATH 变量所指向的路径中:
|
||||
|
||||
@ -1756,17 +1796,17 @@ make clean && make
|
||||
cp perf /usr/bin/
|
||||
```
|
||||
|
||||
![](./assets/164.gif)
|
||||
![](./assets/165.gif)
|
||||
|
||||
### 8.4.3 整合
|
||||
|
||||
* CLion 中配置 perf 的路径:
|
||||
|
||||
![](./assets/165.png)
|
||||
![](./assets/166.png)
|
||||
|
||||
* 在 CLion 中通过 perf 运行代码:
|
||||
|
||||
![](./assets/166.gif)
|
||||
![](./assets/167.gif)
|
||||
|
||||
## 8.5 Win 中文乱码问题
|
||||
|
||||
@ -1779,15 +1819,15 @@ cp perf /usr/bin/
|
||||
|
||||
* 其实,还有一种解决方案,如下所示:
|
||||
|
||||
![](./assets/167.png)
|
||||
|
||||
![](./assets/168.png)
|
||||
|
||||
![](./assets/169.png)
|
||||
|
||||
![](./assets/170.png)
|
||||
|
||||
* 测试一下,是否配置成功:
|
||||
|
||||
![](./assets/170.gif)
|
||||
![](./assets/171.gif)
|
||||
|
||||
## 8.6 CLion 中自动导入头文件
|
||||
|
||||
@ -1798,11 +1838,11 @@ cp perf /usr/bin/
|
||||
> * ① CLion 中的`自动导入头文件`的`快捷键`是 `Alt + Enter` 。
|
||||
> * ② CLion 中的`自动提取变量的类型`的`快捷键`是 `Ctrl + Alt + V` 。
|
||||
|
||||
![](./assets/171.gif)
|
||||
![](./assets/172.gif)
|
||||
|
||||
* 开启自动导入头文件的步骤,如下所示:
|
||||
|
||||
![](./assets/172.png)
|
||||
|
||||
![](./assets/173.png)
|
||||
|
||||
![](./assets/174.png)
|
||||
|
||||
|