首页 > 解决方案 > 包含库时无法在 VS Code 中使用 GDB 调试 C++ 文件

问题描述

我对 C++ 编程非常陌生,正在尝试通过 VS Code 使用 GDB 调试程序。我可以用我当前的设置调试没有库的应用程序,但包括一个库(在本例中为 libcurl)我无法配置 GDB 工作。

我通过手动指定 gcc 参数包含并链接了 libcurl。我试图通过 VS 代码将类似的参数传递给 gdb,但由于我的深度不够,我无法找到任何关于该主题的信息。

下面是我正在尝试调试的代码(构建和运行良好)和我的 VS Code JSON 文件。

卷曲测试.cpp

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <curl/curl.h>

using namespace std;

size_t CurlWrite_CallbackFunc_StdString(void *contents, size_t size, size_t nmemb, string *s)
{
    size_t newLength = size*nmemb;
    try
    {
        s->append((char*)contents, newLength);
    }
    catch(bad_alloc &e)
    {
        return 0;
    }
    return newLength;
}

string getRequest(const char *url)
{
    CURL *curl;
    CURLcode res;

    const char *pCACertFile = "cacert.pem";

    string outputHTML;

    curl_global_init(CURL_GLOBAL_DEFAULT);

    curl = curl_easy_init();
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
        curl_easy_setopt(curl, CURLOPT_CAINFO, pCACertFile);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlWrite_CallbackFunc_StdString);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &outputHTML);

        res = curl_easy_perform(curl);

        if (res != CURLE_OK){
            fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
        }

        curl_easy_cleanup(curl);
    }

    curl_global_cleanup();
    return outputHTML;
}

int main()
{
    string outputHTML;

    const char *baseUrl = "https://example.com";

    outputHTML = getRequest(baseUrl);

    cout << outputHTML << endl;
}

任务.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g",
                "-I",
                "\"C:/bin/curlpp/include/\"",
                "-I",
                "\"C:/bin/curl/include\"",
                "-L",
                "\"C:/bin/curl/lib\"",
                "-o",
                "curltest",
                "curltest.cpp",
                "-l",
                "curl"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "run",
            "type": "shell",
            "command": "./curltest.exe",
            "args": [],
            "group": {
                "kind": "test",
                "isDefault": true
            }
        }
    ]
}

启动.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/curltest.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "C:/bin/mingw-w64/mingw64/bin/gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "C:/bin/curlpp/include/",
                "C:/bin/curl/include/"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.17763.0",
            "compilerPath": "C:/bin/mingw-w64/mingw64/bin/gcc.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "gcc-x64"
        }
    ],
    "version": 4
}

通过此配置进行调试会在调试控制台中提供以下输出。

../../../../src/gdb-8.1/gdb/buildsym.c:1039: internal-error: void prepare_for_building(const char*, CORE_ADDR): Assertion `current_subfile == NULL' failed.
A problem internal to GDB has been detected,
further debugging may prove unreliable.
Quit this debugging session?
(y or n) [answered Y; input not from terminal]
../../../../src/gdb-8.1/gdb/buildsym.c:1039: internal-error: void prepare_for_building(const char*, CORE_ADDR): Assertion `current_subfile == NULL' failed.
A problem internal to GDB has been detected,
further debugging may prove unreliable.
Create a core file of GDB?
(y or n) [answered Y; input not from terminal]
ERROR: Unable to start debugging. GDB exited unexpectedly with exit code 3 (0x3).
ERROR: GDB exited unexpectedly with exit code 3 (0x3). Debugging will now abort.
The program 'C:\Users\Ethan\Desktop\vscpp\curltest.exe' has exited with code -1 (0xffffffff).

标签: c++visual-studio-codegdb

解决方案


推荐阅读