Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 调试C/C++;使用不同目录时在VSCode中_C++_C_Visual Studio Code_Vscode Debugger - Fatal编程技术网

C++ 调试C/C++;使用不同目录时在VSCode中

C++ 调试C/C++;使用不同目录时在VSCode中,c++,c,visual-studio-code,vscode-debugger,C++,C,Visual Studio Code,Vscode Debugger,我希望能够在使用不同目录时调试我的程序 让我们以下面这个例子为例: 其中main.c包含: #include <stdio.h> #include "lib/A.h" int main() { test(); return 0; } 当我尝试调试此程序(主程序中的断点)时,我得到以下错误: The prelaunchtask C/C++ g++.exe build active file temrinated with exit code -

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

让我们以下面这个例子为例:

其中main.c包含:

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

int main()
{
    test();
    return 0;
}
当我尝试调试此程序(主程序中的断点)时,我得到以下错误:

The prelaunchtask C/C++ g++.exe build active file temrinated with exit code -1
但是,如果我删除A.c并将整个函数写入头文件,调试将正常工作:

A.h:

launch.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"
        }
    ]
}

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

您使用多个cpp文件,因此使用诸如CMake、g++terminates、read the errorOnly之类的构建工具构建活动文件不是一个非常有用的功能,由于几乎所有非平凡的程序都分布在多个文件上。在
tasks.json
中,错误是
“${file}”,
,这意味着只使用活动文件进行构建。并非所有文件都在多个文件夹中。官方文档解释了这一点:在您查看并找到
“${workspaceFolder}\\\*.cpp”
之后,请注意这可能不是递归的。每个文件夹可能需要一个单独的条目。或者跟随第一条评论,因为CMake是一个很好的解决方案。
The prelaunchtask C/C++ g++.exe build active file temrinated with exit code -1
#pragma once
#include <stdio.h>

void test()
{
   printf("A\n");
   printf("B\n");
}
{
    "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"
} 
   {

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