首页 > 解决方案 > 使用不同目录时在 VSCode 中调试 C/C++

问题描述

我希望能够在使用不同目录时调试我的程序。

让我们看下面这个例子:

在此处输入图像描述

其中 main.c 包含:

#include <stdio.h>
#include "lib/A.h"

int main()
{
    test();
    return 0;
}

啊:

#pragma once
#include <stdio.h>

void test();

交流:

#include "A.h"

void test()
{
   printf("A\n");
   printf("B\n");
}

当我尝试调试此程序(主程序中的断点)时,出现以下错误:

The prelaunchtask C/C++ g++.exe build active file temrinated with exit code -1

但是,如果我删除 Ac 并将整个函数写入头文件中,调试将正常工作

啊:

#pragma once
#include <stdio.h>

void test()
{
   printf("A\n");
   printf("B\n");
}

我可能需要更改 tasks.json 或 settings.json 中的某些内容,但我不知道应该修改什么以便可以使用 Ac - Ah - main.c 调试程序。

任务.json

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\msys64\\mingw64\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
} 

启动.json:

   {

    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++.exe - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe build active file"
        }
    ]
}

我尝试了一些我发现的解决方案,但没有一个有效,这就是我想发布这个问题的原因

标签: c++cvisual-studio-codevscode-debugger

解决方案


推荐阅读