c
Before Width: | Height: | Size: 536 KiB |
Before Width: | Height: | Size: 1.0 MiB |
Before Width: | Height: | Size: 150 KiB |
Before Width: | Height: | Size: 440 KiB |
Before Width: | Height: | Size: 64 KiB |
Before Width: | Height: | Size: 20 KiB |
Before Width: | Height: | Size: 232 KiB |
@ -206,137 +206,7 @@
|
||||
|
||||
|
||||
|
||||
# 第三章:环境切换为 WSL(⭐)
|
||||
|
||||
## 3.1 概述
|
||||
|
||||
* 前文说过,C 语言中的指针是否使用是个颇具争议的话题,现代化的高级编程语言通过各种策略和机制,在编译期就能解决指针危险的问题。但是,遗憾的是,C 语言的指针很大程度上,在运行期才会暴露问题。
|
||||
|
||||
* 幸运的是,我们可以使用 `Valgrind` 项目来进行`内存泄露检测`和`性能分析`,而 Valgrind 只支持 Linux 。
|
||||
|
||||
> [!NOTE]
|
||||
>
|
||||
> win 11 中的 WSL2 就是个 Linux 环境,我们可以在上面跑各种 Linux 工具,这样我们就不需要再安装虚拟机软件了,如:VMware Workstation(它会完整的模拟一个硬件系统,并在上面跑各种 Linux ,实在是太笨重了)。
|
||||
|
||||
## 3.2 使用步骤
|
||||
|
||||
* 在 WSL2 上安装 Valgrind :
|
||||
|
||||
```shell
|
||||
dnf -y upgrade && dnf -y install valgrind # AlmaLinux
|
||||
```
|
||||
|
||||
```shell
|
||||
apt -y update && apt -y upgrade && apt -y install valgrind # Ubuntu
|
||||
```
|
||||
|
||||
![](./assets/4.gif)
|
||||
|
||||
* 查看 valgrind 可执行文件的安装位置:
|
||||
|
||||
```shell
|
||||
which valgrind
|
||||
```
|
||||
|
||||
![](./assets/5.gif)
|
||||
|
||||
* CLion 中将工具链设置为 WSL2 :
|
||||
|
||||
![](./assets/6.gif)
|
||||
|
||||
* CLion 中配置 valgrind 的路径:
|
||||
|
||||
![](./assets/7.png)
|
||||
|
||||
* 查看 WSL2 中 cmake 的版本:
|
||||
|
||||
```shell
|
||||
cmake --version
|
||||
```
|
||||
|
||||
![](./assets/8.png)
|
||||
|
||||
* 修改项目中 CMakeLists.txt 中 cmake 的版本:
|
||||
|
||||
```{1} txt
|
||||
cmake_minimum_required(VERSION 3.26.5) # 3.26.5
|
||||
|
||||
# 项目名称和版本号
|
||||
project(c-study VERSION 1.0 LANGUAGES C)
|
||||
|
||||
# 设置 C 标准
|
||||
set(CMAKE_C_STANDARD 23)
|
||||
set(CMAKE_C_STANDARD_REQUIRED True)
|
||||
|
||||
# 辅助函数,用于递归查找所有源文件
|
||||
function(collect_sources result dir)
|
||||
file(GLOB_RECURSE new_sources "${dir}/*.c")
|
||||
set(${result} ${${result}} ${new_sources} PARENT_SCOPE)
|
||||
endfunction()
|
||||
|
||||
# 查找顶层 include 目录(如果存在)
|
||||
if (EXISTS "${CMAKE_SOURCE_DIR}/include")
|
||||
include_directories(${CMAKE_SOURCE_DIR}/include)
|
||||
endif ()
|
||||
|
||||
# 查找所有源文件
|
||||
set(SOURCES)
|
||||
collect_sources(SOURCES ${CMAKE_SOURCE_DIR})
|
||||
|
||||
# 用于存储已经处理过的可执行文件名,防止重复
|
||||
set(EXECUTABLE_NAMES)
|
||||
|
||||
# 创建可执行文件
|
||||
foreach (SOURCE ${SOURCES})
|
||||
# 获取文件的相对路径
|
||||
file(RELATIVE_PATH REL_PATH ${CMAKE_SOURCE_DIR} ${SOURCE})
|
||||
# 将路径中的斜杠替换为下划线,生成唯一的可执行文件名
|
||||
string(REPLACE "/" "_" EXECUTABLE_NAME ${REL_PATH})
|
||||
string(REPLACE "\\" "_" EXECUTABLE_NAME ${EXECUTABLE_NAME})
|
||||
string(REPLACE "." "_" EXECUTABLE_NAME ${EXECUTABLE_NAME})
|
||||
|
||||
# 处理与 CMakeLists.txt 文件同名的问题
|
||||
if (${EXECUTABLE_NAME} STREQUAL "CMakeLists_txt")
|
||||
set(EXECUTABLE_NAME "${EXECUTABLE_NAME}_exec")
|
||||
endif ()
|
||||
|
||||
# 检查是否已经创建过同名的可执行文件
|
||||
if (NOT EXECUTABLE_NAME IN_LIST EXECUTABLE_NAMES)
|
||||
list(APPEND EXECUTABLE_NAMES ${EXECUTABLE_NAME})
|
||||
|
||||
# 创建可执行文件
|
||||
add_executable(${EXECUTABLE_NAME} ${SOURCE})
|
||||
|
||||
# 查找源文件所在的目录,并添加为包含目录(头文件可能在同一目录下)
|
||||
get_filename_component(DIR ${SOURCE} DIRECTORY)
|
||||
target_include_directories(${EXECUTABLE_NAME} PRIVATE ${DIR})
|
||||
|
||||
# 检查并添加子目录中的 include 目录(如果存在)
|
||||
if (EXISTS "${DIR}/include")
|
||||
target_include_directories(${EXECUTABLE_NAME} PRIVATE ${DIR}/include)
|
||||
endif ()
|
||||
|
||||
# 检查并添加 module 目录中的所有 C 文件(如果存在)
|
||||
if (EXISTS "${DIR}/module")
|
||||
file(GLOB_RECURSE MODULE_SOURCES "${DIR}/module/*.c")
|
||||
target_sources(${EXECUTABLE_NAME} PRIVATE ${MODULE_SOURCES})
|
||||
endif ()
|
||||
endif ()
|
||||
endforeach ()
|
||||
|
||||
```
|
||||
|
||||
* 在 CLion 中正常运行代码:
|
||||
|
||||
![](./assets/9.gif)
|
||||
|
||||
* 在 CLion 中通过 valgrind 运行代码:
|
||||
|
||||
![](./assets/10.gif)
|
||||
|
||||
|
||||
|
||||
# 第四章:指针的理解和定义(⭐)
|
||||
# 第三章:指针的理解和定义(⭐)
|
||||
|
||||
## 4.1 概述
|
||||
|
||||
|