vscode
# 一 vscode调试c++程序
所需插件:c++ tasks.json (compiler build settings) ,负责编译 launch.json (debugger settings),负责调试 c_cpp_properties.json (compiler path and IntelliSense settings),负责更改路径等设置
警告
windows中使用vscode进行编译,需要安装mingw,将bin加入环境变量
# 1 c_cpp_properties.json
由于程序是根据当前系统环境配置基本信息,因此有可能配置不完整,通过该文件配置缺少的信息。
ctrl+shift+p
打开搜索,输入c/c++,选择C/Cpp: Edit configurations...,生成c_cpp_properties.json
- linux配置如下:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/opt/rh/devtoolset-8/root/usr/bin/gcc", //编译器路径,如果不是此路径,程序中在包含库的地方会有波浪线
"cStandard": "c89",
"cppStandard": "c++17",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
- windows配置如下:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.19041.0",
"compilerPath": "D:/tools/mingw64/bin/g++.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-msvc-x64"
},
],
"version": 4
}
# 2 task.json
如果要构建应用程序,则需要生成tasks.json文件
- linux配置如下:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc 生成活动文件",
"command": "/opt/rh/devtoolset-8/root/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-I", // 包含的头文件目录
"${workspaceFloder}/include/",
"-C", // 包含的库文件
"${workspaceFloder}/src/*.cpp",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
- windows配置如下:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe 生成活动文件",
"command": "D:\\tools\\mingw64\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "调试器生成的任务。"
},
],
"version": "2.0.0"
}
# 3 launch.json
如果要构建应用程序,则需要生成tasks.json文件
- linux配置如下:
{
"configurations": [
{
"name": "C/C++: g++ 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}", //指调试的程序,其值对应程序的路径
// ${xxxx}是vscode内置的变量,可以方便获取到需要的路径或者文件名,
// 具体什么变量参考别人的博客,
// 这里列举一部分
// ${workspaceFolder} :表示当前workspace文件夹路径,也即/home/Coding/Test
// ${workspaceRootFolderName}:表示workspace的文件夹名,也即Test
// ${file}:文件自身的绝对路径,也即/home/Coding/Test/.vscode/tasks.json
// ${relativeFile}:文件在workspace中的路径,也即.vscode/tasks.json
// ${fileBasenameNoExtension}:当前文件的文件名,不带后缀,也即tasks
// ${fileBasename}:当前文件的文件名,tasks.json
// ${fileDirname}:文件所在的文件夹路径,也即/home/Coding/Test/.vscode
// ${fileExtname}:当前文件的后缀,也即.json
// ${lineNumber}:当前文件光标所在的行号
// ${env:PATH}:系统中的环境变量
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
// preLaunchTask 表示在 执行调试前 要完成的任务
// 比如这里 要完成 C/C++: gcc 生成活动文件 这个tasks任务(重新生成程序)
// 这里的 C/C++: gcc 生成活动文件 是 tasks.json 中 lable 标记的任务名称
"preLaunchTask": "C/C++: gcc 生成活动文件",
// 调试器的路径
"miDebuggerPath": "/opt/rh/devtoolset-8/root/usr/bin/gdb"
}
],
"version": "2.0.0"
}
- windows配置如下:
{
"configurations": [
{
"name": "C/C++: g++.exe 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "D:\\tools\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe 生成活动文件"
}
],
"version": "2.0.0"
}
tasks.json的"label"参数值和launch.json的"preLaunchTask"参数值需要保持一致
# 4 附加
tasks中可以有多个任务,在一个列表中保存,根据自己需要添加或删除。
{
"tasks": [
{
// 任务一: 创建 build 文件夹
"type": "shell",
"label": "CreateBuildDir", // lable 标记任务名称
"command": "mkdir", // 命令
// 传给上面命令的参数,这里是传给 Unix 系统的参数,windows下稍有不用,下边有
"args": [
"-p",
"build"
],
"windows": {
"options": {
"shell": {
"executable": "powershell.exe"
}
},
"args": [ // 对于windows系统,传的参数
"-Force",
"build"
]
},
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
},
// 任务二: Cmake
// 在 build 文件夹中调用 cmake 进行项目配置
// 如果想配置比如 release 还是 debug 可以添加参数或者在
// CMakeLists.txt中设置也行
{
"type": "shell",
"label": "cmakeRun", // 给这个任务起个名字
// 这里的cmake,用我后面小程序创建的结果填的是全路径,
// 命令写全路径,则路径中不能包含带空格
// 如果你添加了环境变量,那么直接填写命令即可,也不会有
// 路径是否包含空格的问题(下面的命令同理)
"command": "cmake",
"args": [
"-DCMAKE_MAKE_PROGRAM=E:\\Resource\\mingw64\\bin\\mingw32-make.exe", // MinGW目录下bin目录下的mingw32-make.exe
"-G",
// 不使用-G "Unix Makefiles" 参数可能会编译成了VS用的工程文件
// 之所以三个斜杠,是因为vscode终端自己还要转义一次
// 2021-01-21更新:我在32位的win7上发现,vscode自己又不转义了
// 所以如果以下三个斜杠不行的话,大家手动改成一个斜杠就好,即\"Unix Makefiles\"
// 后面我给的小程序默认写的是3个
"\\\"Unix Makefiles\\\"",
"../" // ../ 表示build文件夹的上级目录,CMakeLists.txt就放在上级目录中
],
"options": {
"cwd": "${workspaceFolder}/build"
},
"dependsOn":[
"CreateBuildDir" // 表示在 创建目录 任务结束后进行
]
},
// 任务三: make编译
{
"type": "shell",
"label": "makeRun",
"command": "mingw32-make", // 这个也是MinGW目录下bin目录下的mingw32-make.exe,如果添加了环境变量,这里直接写mingw32-make.exe
"args": [],
"options": {
"cwd": "${workspaceFolder}/build"
}, // 注意这里是编译到了项目文件夹下的 build 文件夹里面,这里就解释了
// 为什么 launch.json 中 program 路径要那么设置了。
"dependsOn":[
"cmakeRun" // 表示在Cmake任务结束后进行
]
},
],
"version": "2.0.0"
}
# 二 vscode调试cmake工程
所需插件:c/c++、CMake、cmake tools
# 1 选择编译器
- Open the Command Palette (Ctrl+Shift+P) and run CMake: Select a Kit.
- Select the compiler you want to use.
注意
If you don't see the compiler you're looking for, you can edit the cmake-tools-kits.json file in your project. To edit the file, open the Command Palette (Ctrl+Shift+P) and run the CMake: Edit User-Local CMake Kits command. 如果没有找到所需的compiler,执行open the Command Palette (Ctrl+Shift+P) and run the CMake: Edit User-Local CMake Kits command. 将所需的compiler添加到文件中。
[
{
"name": "GCC 4.8.5 x86_64-redhat-linux",
"compilers": {
"C": "/usr/bin/gcc"
}
},
{
"name": "GCC 8.3.1 x86_64-redhat-linux",
"compilers": {
"C": "/opt/rh/devtoolset-8/root/usr/bin/gcc"
}
}
]
# 2 选择编译方式
- open the Command Palette (Ctrl+Shift+P) run the CMake: Select Variant command
- Select Debug to include debug information with your build.
点击vscode下方build即可编译
# 3 附加:如果cmake路径不对
- 添加task.json
{
"version": "2.0.0",
"tasks": [
{
"label": "mkdir",
"type": "shell",
"command": "mkdir build -p"
},
{
"label": "/usr/local/bin/cmake",
"type": "shell",
"command": "cmake -DCMAKE_BUILD_TYPE=debug ..",
"dependsOn": [
"mkdir"
],
"options": {
"cwd": "${workspaceFolder}/build" // cwd表明切换到build目录下
}
},
{
"label": "/usr/local/bin/make",
"type": "shell",
"command": "make -j4",
"options": {
"cwd": "${workspaceFolder}/build"
}
},
]
}
- 添加launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/hello", // 调试程序路径
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"miDebuggerPath": "/opt/rh/devtoolset-8/root/usr/bin/gdb"
}
]
}
# 4 执行
- open the Command Palette (Ctrl+Shift+P) run the Tasks: Run Task command
- 先选择cmake
- 再选择make
# 5 附加:设置编译器路径
如果gcc路径不是默认路径,在CMakeLists.txt中需添加gcc路径
cmake_minimum_required(VERSION 3.10)
set(CMAKE_C_COMPILER "/opt/rh/devtoolset-8/root/usr/bin/cc")
set(CMAKE_CXX_COMPILER "/opt/rh/devtoolset-8/root/usr/bin/c++")
project(hello)
add_executable(hello main.cpp)
linux 下库的标准目录结构: /usr/lib 系统自带库的目录; /usr/include 系统自带包含文件的目录;