【问题标题】:Configure c++ debugger in VSCode在 VSCode 中配置 c++ 调试器
【发布时间】:2021-07-02 20:41:26
【问题描述】:

我最近搬到了 VSCode,我有点迷失了。

如果我用这个控制台命令编译我的程序

g++ -Wall -o main main.cpp src/*.cpp -I included

它能正确编译生成.exe文件。

但我有一个错误,所以我想使用调试器来了解发生了什么。 当我在 VSCode 中点击 run -> star debuggin 时,我收到一条控制台消息“不是这样的文件或目录”,其中包含一个包含的文件。所以,我认为我的 launch.json 中有一些错误的配置

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++.exe - Compilar y depurar el archivo activo",
            "type": "cppdbg",
            "request": "launch",
            "program": "C:\\Users\\Don\\Desktop\\IColections\\main.exe",
            "args": ["-I[included/*/*.h]"],
            "stopAtEntry": false,
            "cwd": "C:\\Users\\Don\\Desktop\\IColections",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Habilitar la impresión con sangría para gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe compilar archivo activo"
        }
    ]
}

我不明白我应该在 args 或 environment 中放什么

编辑:

这是错误信息

C:\MinGW\bin\g++.exe -g *.cpp -o C:\Users\Don\Desktop\IColections\main.exe
main.cpp:2:31: fatal error: ../included/Fruta.h: No such file or directory
 #include "../included/Fruta.h"
                               ^
compilation terminated.

这是 main.cpp

#include <iostream>
#include "../included/Fruta.h"
#include "../included/Uva.h"
#include "../included/Naranja.h"
#include "../included/List.h"
#include "../included/List_Iterator.h"

int main(){
    
    ICollection* fruta = new List;
    
    Uva* misUvas = new Uva;
    misUvas->setKg(80.5);
    misUvas->setRacimos(15000);

    Naranja* misNaranjas = new Naranja;
    misNaranjas->setKg(200);
    misNaranjas->setNaranjas(1000);

    fruta->agregar(misNaranjas);
    fruta->agregar(misUvas);

    IIterator* it = dynamic_cast<List*> (fruta)->getIterator();    

    ICollectible* elem;
    Fruta* frut;

    if(!it->hasCurrent()){
    }

    while(it->hasCurrent()){ 
        elem = it->getCurrent();
        frut = dynamic_cast <Fruta*> (elem); 
        frut->printFruta();
        it->next();
    }

    system("pause");
    



    return 0;
}

tasks.json

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe compilar archivo activo",
            "command": "C:\\MinGW\\bin\\g++.exe",
            "args": [
                "-g",
                "*.cpp",
                "-o",
                "${fileDirname}\\main.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Tarea generada por el depurador."
        }
    ],
    "version": "2.0.0"
}

【问题讨论】:

  • 你能给出确切的错误吗? "args": ["-I[included/*/*.h]"], 已经看错了,我不知道它是干什么用的。
  • vscode 文档在 args 中没有任何内容:https://code.visualstudio.com/docs/cpp/config-mingw#_debug-helloworldcpp
  • 首先,在编译命令中添加-g
  • 我不明白我应该在 args 或 environment 中放入什么您的程序是否使用命令行参数或 environment?
  • 我编辑了帖子,添加了确切的错误消息和 main.cpp 的代码(给出错误的那个)我认为我的程序不使用它们@drescherjm 但我不知道什么参数或环境是对于 VSCode

标签: c++ debugging visual-studio-code


【解决方案1】:

如果您已经有带有调试信息 (-g) 的编译版本,则无需再次包含头文件。

只需从配置中删除 "preLaunchTask": "C/C++: g++.exe compilar archivo activo" 行,因为您的程序已经编译。

【讨论】:

    猜你喜欢
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-25
    • 2021-05-02
    相关资源
    最近更新 更多