在 VSCode 中配置 C++ 调试器

时间:2021-07-02 20:41:26

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

我最近搬到了 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"
        }
    ]
}

我不完全理解我应该在参数或环境中放入什么

编辑:

这是错误信息

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"
}

1 个答案:

答案 0 :(得分:1)

如果您已经有一个带有调试信息 (-g) 的编译版本,那么您不需要再次包含头文件。

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

相关问题