Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to see elements of std::vector with gcc in VS code

I'm currently using VS Code to learn C++ because it was easy to setup and is much lighter than VS Studio. However, one thing that I'm unable to do is to see the elements of an array (or string, etc), in debug mode.

vector

I've searched for solutions here, and it seems that enabling pretty printing would solve the issue, but unfortunetly it did not (i have Python 3.6 installed). I've also tried using VS Studio compiler and debugger, but was unable to get it to work the way i wanted (basically clicking F5 to compile single cpp files without needing to change any options, just a single click).

Can you guys help me out on this? I'm currently using MinGW compiler on windows 10, with the following tasks file:

"version": "2.0.0",
"tasks": [
    {
        "label": "echo",
        "type": "shell",
        "command": "g++",
        "args": [
            "-g", "${relativeFile}", "-o", "example"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        }
    }
]

and launch:

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

Thank in advance!

like image 242
tomas-silveira Avatar asked Jun 30 '19 22:06

tomas-silveira


People also ask

How do I add GCC to visual code?

Install Visual Studio Code. Install the C/C++ extension for VS Code. You can install the C/C++ extension by searching for 'c++' in the Extensions view (Ctrl+Shift+X). Get the latest version of Mingw-w64 via MSYS2, which provides up-to-date native builds of GCC, Mingw-w64, and other helpful C++ tools and libraries.


3 Answers

For me this works, but check first if you have 'c:\msys64\mingw64\share\gcc-8.3.0\python' or 'c:\usr\share\gcc-8.3.0\python' folder and adjust the snippet

launch.json
...
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "python import sys;sys.path.insert(0, 'c:\\msys64\\mingw64\\share\\gcc-8.3.0\\python');from libstdcxx.v6.printers import register_libstdcxx_printers;register_libstdcxx_printers(None)",
                    "ignoreFailures": false
                },
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
...
like image 80
pawel Avatar answered Oct 19 '22 11:10

pawel


I used the method just like Georgy described above.

*((int(*)[10])array._M_impl._M_start)

Debug console output:
-exec p array
$7 = {<std::_Vector_base<int, std::allocator<int> >> = {_M_impl = {<std::allocator<int>> = {<__gnu_cxx::new_allocator<int>> = {<No data fields>}, <No data fields>}, _M_start = 0x2924cb0, _M_finish = 0x2924cd8, _M_end_of_storage = 0x2924cf0}}, <No data fields>}

-exec p *((int(*)[10])array._M_impl._M_start)
$8 = {41, 18467, 6334, 26500, 19169, 15724, 11478, 29358, 26962, 24464}
like image 1
vbirdchong Avatar answered Oct 19 '22 09:10

vbirdchong


I also faced a similar problem using MinGW for Windows.

Problem was resolved for me when I removed MINGW and installed mingw-w64 instead.

like image 1
Sachin Yadav Avatar answered Oct 19 '22 09:10

Sachin Yadav