安装WSL
启动Linux 子系统功能
进制控制面板->程序和功能, 将适用于Linux的Windows子系统
勾选。
安装 Windows Terminal
在 Microsoft Store市场上搜索 WSL
安装 Terminal
安装WSL
在 Microsoft Store市场上搜索 WSL
安装 Ubuntu 18.04 LTS
如何启动 Ubuntu?
- 在 cmd/powershell中输入
bash
回车即可进入linux子系统 - 直接在terminal下拉中选择
如果觉得默认的终端主题不好看可以自行安装 oh my zsh
安装 编译工具链
sudo apt update # 更新软件包
sudo apt install build-essential #安装build-essential软件包
sudo apt install gdb #
sudo apt install cmake
等待安装完成,之后查看安装结果
cmake --version
gcc -v
g++ -v
安装vscode
vscode的不说,宇宙第一编辑器,传送门 https://code.visualstudio.com/
安装vscode插件
- Rmote - WSL
- C/C++ IntelliSense
- Beautify
- Rainbow Brackets
- cmake
配置C/C++调试参数
先写一个简单的演示项目工程
test.h
#ifndef _TEST_H_
#define _TEST_H_
int add(int a, int b);
#endif
test.c
#include "test.h"
int add(int a, int b)
{
return a + b;
}
main.c
#include "test.h"
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a = 88;
int b = 12;
int c = add(a, b);
printf("结果是%d\n", c);
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(demo)
add_definitions("-Wall -g -Wno-unknown-warning-option"
" -Wno-format-extra-args -Wno-format -Wno-pragmas"
" -Wno-incompatible-pointer-types")
add_executable(1
src/main.c
src/test.c
)
在.vscode目录下添加tasks.json
{
"tasks": [
{
// 清空编译
"type": "shell",
"label": "rebuild",
"command": "cd build; rm -rf *; cmake .. ; make",
"group": {
"kind": "build",
"isDefault": true
},
},
{
// 增量编译
"type": "shell",
"label": "build",
"command": "cd build; make",
"group": {
"kind": "build",
"isDefault": true
},
}
],
"version": "2.0.0"
}
在launch.json中添加 调试启动配置
1. "program":"配置调试的应用路径"
2. "preLanchTask":"配置Lanch前先执行的任务,可以先执行make"
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "增量 (gdb) Launch ",
"type": "cppdbg",
"request": "launch",
"program": "enter program name, for example ${workspaceFolder}/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build"
},
{
"name": "清空 (gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/1",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
,
"preLaunchTask": "rebuild"
}
]
}
开始调试
- 设置断点,点击调试开关启动调试