oOLorOo
oOLorOo

Reputation: 16

Debugging a c++ code with Visual Studio Code Ubuntu

Good evening to everyone, I try to debug this little program in visual studio code in ubuntu:

#include <string>
#include <iostream>

int main(int argc, char* argv[]) 
{
  std::string folder = argv[1];
}

but the debug terminate with this error in the terminal:

"terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_M_construct null not valid "

and this in the debug console:

"Unable to open 'raise.c': Unable to read file (Error: File not found (/build/glibc-4WA41p/glibc-2.30/sysdeps/unix/sysv/linux/raise.c))."

So the question are:

1) Is possible to display the number of the line where the error occurs? (In this case line 6)

2) Why does this error happen, and how to avoid it?

3) For avoiding this problem I can write, for example:

string folder = "/home/lorenzo/Images";

but I don't want to do that. For "running" the program from the terminal I write ./main /home/lorenzo/Images, so I pass the folder to the program in this way. Is possible to do the same thing when debbuging, without writing the folder directly in the program, or using cin?

Thanks in advance!

Upvotes: 0

Views: 4478

Answers (1)

sweenish
sweenish

Reputation: 5202

If you want to debug with VS Code, there's a setup that you have to do for each project, but after the setup is complete, it's straightforward.

Install gdb if you haven't already. You then need to choose a configuration in the debug panel. Instructions can be found here. Compile your program with -g at a minimum. I prefer also adding in -O0 to minimize optimizations.

Once you get set up, you're now ready to debug with VS Code. Now, to [hopefully] answer your questions.

  1. gdb can do this for some segmentation faults; generally you'll want to learn how to move through the code yourself.
  2. I attempted to compile and run your program, and it worked just fine. Is the name of your executable main? I compiled on Debian using gcc 5.5. I didn't name my executable, so my invocation looked like this:
    ./a.out /home/sweenish/tmp. Since mine didn't fail, I can't offer much help here. But your compiler is saying that a file doesn't exist. Did you install the build-essential package?
  3. Yes, you can automate the extra argument by adding the option to your launch.json file for the VS Code project.

Here's a short example:

#include <string>
#include <iostream>

int main(int argc, char* argv[]) 
{
  std::string folder = argv[1];
  std::cout << folder << '\n';  // Set a breakpoint here
}

I added an extra line of code to your example. Set a breakpoint by clicking left of the line number, a red circle will appear.

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558 
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "g++ build active file",
            "command": "g++",
            "args": [
                "-Wall",
                "-std=c++17",
                "-g",
                "-O0",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "/home/linuxbrew/.linuxbrew/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build"
        }
    ]
}

This is a slightly modified auto-generated task. I added -Wall, -std=c++17, and -O0. The file is tasks.json. If you don't create it before attempting to execute the debug, it will ask prompt you to generate it.

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++ build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [
                "/home/lorenzo/Images"
            ],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "g++ build active file",
            "miDebuggerPath": "gdb"
        }
    ]
}

This is the auto-generated launch.json. Notice that I added the path argument. The debugger will always invoke with that argument, saving you some typing.

I then hit the Play button in the debugging panel while my C++ file is active, and it will compile and start the debugger for me. From the debug console, running: -exec print argv[1] prints the file path that I am using as an argument to the program.

Upvotes: 3

Related Questions