Cannot debug a C++ file using GDB in VS Code when a library is included

0

I am very new to C++ programming and am trying to debug a program using GDB through VS Code. I am able to debug an application without a library fine with my current setup, but including a library (in this case libcurl) I cannot configure GDB to work.

I have included and linked libcurl by manually specifying gcc arguments. I have tried to pass similar arguments through to gdb through VS code, but I couldn't get anything to work as I am out of my depth and there is little information on the subject that I can find.

Below is the code I am trying to debug (which builds and runs fine) and my VS Code JSON files.

curltest.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;
}

tasks.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
            }
        }
    ]
}

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

Debugging through this configuration gives the following output in the debug console.

../../../../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-code
gdb

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0