使用Visual Studio Code Ubuntu调试C ++代码

大家晚上好,我尝试在ubuntu的Visual Studio代码中调试此小程序:

#include <string>
#include <iostream>

int main(int argc,char* argv[]) 
{
  std::string folder = argv[1];
}

但是调试终止并在终端中出现以下错误:

”终止在引发“ std :: logic_error”实例后调用   what():basic_string :: _ M_construct null无效 “

,在调试控制台中:

“无法打开'raise.c':无法读取文件(错误:找不到文件(/build/glibc-4WA41p/glibc-2.30/sysdeps/unix/sysv/linux/raise.c))。”

所以问题是:

1)可以显示发生错误的行号吗? (在这种情况下,第6行)

2)为什么会发生此错误,以及如何避免该错误?

3)为了避免这个问题,我可以写例如:

string folder = "/home/lorenzo/Images";

但是我不想那样做。为了从终端“运行”程序,我编写了./main / home / lorenzo / Images,因此我将文件夹以这种方式传递给程序。调试时是否可以执行相同的操作,而无需直接在程序中编写文件夹或使用cin?

谢谢!

GlacierZ 回答:使用Visual Studio Code Ubuntu调试C ++代码

如果要使用VS Code进行调试,则必须对每个项目进行设置,但是设置完成后,很简单。

如果尚未安装gdb,请安装。然后,您需要在调试面板中选择一个配置。可以在here中找到说明。至少用for($j)编译程序。我更喜欢添加-g以最小化优化。

设置完成后,就可以使用VS Code进行调试了。现在,[希望]回答您的问题。

  1. gdb可以对某些分段错误执行此操作。通常,您需要自己学习如何遍历代码。
  2. 我尝试编译并运行您的程序,但效果很好。可执行文件的名称是main吗?我使用gcc 5.5在Debian上编译。我没有命名可执行文件,因此我的调用看起来像这样:
    -O0。由于我的努力没有失败,因此我在这里不能提供太多帮助。但是您的编译器在说文件不存在。 您安装了构建必备软件包吗?
  3. 是的,您可以通过将选项添加到VS Code项目的launch.json文件中来自动化额外的参数。

这是一个简短的例子:

./a.out /home/sweenish/tmp

我在您的示例中增加了一行代码。单击行号左侧来设置断点,将出现一个红色圆圈。

#include <string>
#include <iostream>

int main(int argc,char* argv[]) 
{
  std::string folder = argv[1];
  std::cout << folder << '\n';  // Set a breakpoint here
}

这是一个经过自动修改的任务。我添加了{ // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "2.0.0","tasks": [ { "type": "shell","label": "g++ build active file","command": "g++","args": [ "-Wall","-std=c++17","-g","-O0","${file}","-o","${fileDirname}/${fileBasenameNoExtension}" ],"options": { "cwd": "/home/linuxbrew/.linuxbrew/bin" },"problemMatcher": [ "$gcc" ],"group": "build" } ] } -Wall-std=c++17。该文件是tasks.json。如果您在尝试执行调试之前未创建它,它将询问您是否生成它。

-O0

这是自动生成的launch.json。注意,我添加了path参数。调试器将始终使用该参数进行调用,从而节省了键入时间。

然后,在我的C ++文件处于活动状态时,单击调试面板中的“播放”按钮,它将为我编译并启动调试器。从调试控制台运行: { // 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": "${fileDirname}/${fileBasenameNoExtension}","args": [ "/home/lorenzo/Images" ],"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": "gdb" } ] } 打印出我正在用作程序参数的文件路径。

本文链接:https://www.f2er.com/3136747.html

大家都在问