blog/posts/clang-get-started.md
2024-12-16 17:27:47 +00:00

145 lines
4.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Debian for WSL 配置C语言开发环境
<br>
::: tip 写在前面的话
本文章已经默认你会使用`WSL`和`VSCode`且你的WSL为`Debian`发行版。
:::
<!-- 所需工具 **`Clang`** 和 **`GDB`** -->
## 在Debian中安装**Clang**和**GDB**包
```bash
apt-get install clang gdb
```
执行上述操作后,通过 `clang -v``gdb -v`,如果能看到输出正确的版本信息,则表示安装完成。
::: details 版本信息示例
:::code-group
```bash [clang]
Debian clang version 14.0.6
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/12
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/12
Candidate multilib: .;@m64
Selected multilib: .;@m64
```
```bash [gdb]
GNU gdb (Debian 13.1-3) 13.1
Copyright (C) 2023 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
```
:::
`Clang`包,作为`C`语言的编译和解释器,用来编译`C`文件代码。`GDB`用来在`vscode`中调试`C`文件。
:::tip 提示
在**Mac**上,安装`Clang`,默认会使用`lldb`进行调试。但我尝试在**Debian**上,`Clang`可以正常的使用,但是`lldb`不能使用,即使安装了`lldb`包,在*vscode*中配置了`lldb`为默认调试,也不能正常使用。所以,我选择使用`gdb`来替代`lldb`,在**Debian**上可以正常配合*vscode*使用了。
:::
## 在vscode中配置C
### 安装C/C++扩展
![C++扩展](/static/2024-12-16%20044411.png)
### 新建 `c_cpp_properties.json` 调用`clang`编译器
在当前项目下,新建 `.vscode`文件夹,并在此目录中,新建 `c_cpp_properties.json`文件:
并添加以下内容:
```json:line-numbers {9}
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/usr/bin/clang",
"cStandard": "c23",
"cppStandard": "c++23",
"intelliSenseMode": "linux-clang-x64"
}
],
"version": 4
}
```
此文件用来配置C/C++扩展,其中设置了编译器为 `/usr/bin/clang`
这样当前项目就可以通过`C/C++扩展`来使用当前系统安装的`clang`编译器了。
### 新建 `tasks.json`,用于调试前进行项目编译
```json:line-numbers{5,6,16,25}
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang++ build active file",
"command": "/usr/bin/clang",
"args": [
"-fcolor-diagnostics",
"-fansi-escape-codes",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
```
### 新建 `launch.json` ,配置项目自动编译和调试任务
```json:line-numbers{11,14,17,18,19}
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) 启动",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"preLaunchTask": "C/C++: clang++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
],
}
```
## 新建 `main.c` 文件,测试系统是否正常
```c:line-numbers
#include <stdio.h>
int main () {
printf("hello world, %s \n", "mike");
return 1;
}
```
按键盘上的<kbd>F5</kbd>启用调试,则`vscode`应该会自动运行调试,执行编译和运行。