如何在大型c++项目的可视化代码中设置调试

How do I set up debugging in visual code for a large c++ project

本文关键字:代码 可视化 设置 调试 项目 大型 c++      更新时间:2023-10-16

我是c++和vs代码的初学者,但我的任务是使用/开发现有的c++项目(在此之前我使用过python和sublime)。

我希望能够配置vs代码,这样我就可以轻松地调试和编译代码,而不会有太多麻烦。

目前,我是这样做的:

  • 对代码进行一些更改,例如在$basedir/src/folder1/file.cpp中
  • 转到终端中的$basedir,然后运行
  • a) 制造
  • b)等待它浏览一堆文件夹,找到一个已更改的文件
  • c) cd$basedir/programs/dawn;清洁;制造
  • 最后运行一个类型的项目:$basedir/programs/project/projectname inputfile.json

我想做的是只按F5(用于调试,或ctrl+F5运行),但我不知道如何设置所有内容。有人能帮我吗?

这些是我的配置文件:在c_cpp_properties.json中,我引用了另外两个项目,但我也必须添加当前项目的include目录,以便vs代码不会抱怨include文件等(我以为它会自动在带有${workspaceFolder}/**的项目中找到文件)

{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"~/otherproject1/include/",
"~/otherproject2/include/",
"~/Documents/projectname/include"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
{

tasks.json

"tasks": [
{
"type": "shell",
"label": "g++ build active file",
"command": "/usr/bin/g++",
"args": [
"cd ${workspaceFolder};",
"make;",
"make clean -C programs/projectname;",
"make -C programs/projectname;",
],
"options": {
"cwd": "/usr/bin"
}
}
],
"version": "2.0.0"
}

launch.json

{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++ build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/programs/projectname inputfile.json",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}

Microsoft为设置调试环境提供了很棒的教程。

请查看以下教程:

VSCode CPP调试

配置mingW和GCC调试

在您的配置中显而易见的是,您错过了此处的.exe

"program": "${fileDirname}/${fileBasenameNoExtension}", //< -- wrong
"program": "${fileDirname}/${fileBasenameNoExtension}.exe", // < -- correct

找到你的调试器并在这里添加路径:

"miDebuggerPath": "C:\path\to\gdb.exe",

要开始调试,只需点击F5。对于"构建",请单击CTRL + SHIFT + B并选择您的构建任务。