步骤:
先cmake编译得到可执行的二进制文件,将生成的二进制文件添加到launch.json的"program":处。
可用的json文件如下,根据自己程序更改:
tasks.json(编译器构建设置)
launch.json(调试器设置)
c_cpp_properties.json(编译器路径和 IntelliSense 设置)
1.launch.json
注:需要将可执行文件填到launch的program处。
"program": "${workspaceFolder}/build/main", //代表的可执行文件的绝对路径
"args": [], //传递给程序的命令行参数
// program字段告诉 vscode 应该调试哪一个程序
"program": "${workspaceFolder}/build/hello", // 工程所在目录下/build/可执行文件名
(对于一个项目由多个src编译成库,test.cpp链接上述生成库,生成一个可执行二进制文件的:Launch写入生成的该二进制文件路径及名称,即要调试的cpp对应的二进制文件。
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) 启动",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/bin/pi",
"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
}
]
}
]
}
2.task.json
tasks.json实际上就相当于帮我们完成了g++ -g main.cpp swap.cpp -o a.out的操作。
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc 生成活动文件",
"command": "/usr/bin/gcc",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
3.c_cpp_properties.json
注:在 "includePath"处需填入项目用到的头文件路径或库的相对(轻量级的库)或绝对路径(如opencv,eigen等),不填入有时会报错。
微软官方文档对ncludePath的解释:
includePath An include path is a folder that contains header files (such as #include "myHeaderFile.h") that are included in a source file. Specify a list of paths for the IntelliSense engine to use while searching for included header files. Searching on these paths is not recursive. Specify ** to indicate recursive search. For example, ${workspaceFolder}/** will search through all subdirectories while ${workspaceFolder} will not. If on Windows with Visual Studio installed, or if a compiler is specified in the compilerPath setting, it is not necessary to list the system include paths in this list.
在这些路径上进行搜索不是递归的。指定 * * 以指示递归搜索。例如,${ workspaceFolder }/* * 将搜索所有子目录,而 ${ workspaceFolder }不会。
例如:
"/usr/include/**",
"/usr/local/include/**",
"/usr/include/eigen3"
"includePath": [
"${workspaceFolder}/**",
"/usr/include/eigen3"
用这个在要包含的主目录后面加上**通配符就可以递归搜索。
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/usr/bin/g++",
"cStandard": "c11",
"cppStandard": "gnu++14",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
4.settings.json
可不改
{
"files.associations": {
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"csignal": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"array": "cpp",
"atomic": "cpp",
"strstream": "cpp",
"*.tcc": "cpp",
"bitset": "cpp",
"chrono": "cpp",
"codecvt": "cpp",
"complex": "cpp",
"cstdint": "cpp",
"deque": "cpp",
"list": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"filesystem": "cpp",
"functional": "cpp",
"iterator": "cpp",
"map": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"ratio": "cpp",
"set": "cpp",
"string": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"fstream": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"new": "cpp",
"ostream": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"thread": "cpp",
"cfenv": "cpp",
"cinttypes": "cpp",
"typeinfo": "cpp",
"*.ipp": "cpp",
"unordered_set": "cpp",
"hash_map": "cpp",
"hash_set": "cpp",
"condition_variable": "cpp",
"future": "cpp",
"mutex": "cpp",
"typeindex": "cpp",
"valarray": "cpp"
}
}
相关:
在工程目录下执行ls -a,可以看到 隐藏文件夹 .vscode。
g++用来编译C++代码,gdb用来调试C++代码即可。
调试和运行
调试F5, 不调试直接运行Cltr+F5。
在可执行二进制文件对应的cpp文件打断点,点左边的三角-调试。
用wsl的,需注意:
在wsl下通过code .打开vscode和编辑json文件,不是在win下打开vscode。