paduraru2009
paduraru2009

Reputation: 615

How to debug external library code in Visual Studio Code?

I'm using Visual Studio Code in a basic C++ project. I link a library built with gcc -g option to include the debug symbols information.

However, when I want to step into a function call from that library it doesn't get in. In Visual Studio this How to debug external class library projects in visual studio? would solve the problem but I don't know how to do it in Visual Studio Code. Probably source files for that library should be specified somewhere? But where can I specify them?

Upvotes: 15

Views: 25901

Answers (4)

Diego Mejia
Diego Mejia

Reputation: 141

I had the same problem for a set of library source code that was being built statically and generating a .a file. This method should work for any library code shared or static. I cracked the case by reading a fair portion of the GDB documentation, the key points of which are linked below.

First make sure that for all executables or libraries you're creating to pass the '-g' option to your compiler to give GDB he hooks needed to link the running software with the source code. Compiling for Debugging

Next in your launch.json file in VS Code you want to point GDB to the set of folders where your source code exists, this includes your application code along with the library code that it links to :

{
"version": "0.2.0"
"configurations":[
    {
        "name": (gdb) Launch",
        "type": "cppdbg",
        "request": "launch",
        ...
        "setupCommands": [
            {
                "description": "Set up compilation directories for GDB",
                "text": "-gdb-set directories /path/to/app/src:/path/to/library/src
            }
        ]
    }
}

These docs show the setting for gdb generically Setting source code search directories for gdb

Finally to make sure that the search paths are being setup appropriately you can check what directories are being searched for by gdb for source code with the command 'show directories'. This can be run inside of VS Code during a debug session by starting a debug session and clicking on the 'Debug Console' terminal tab and executing the command with:

-exec show directories

If everything was setup correctly you should see the directories set in the launch.json file, and should be able to step through the library source code the same as your application code, no assembly required.

Upvotes: 0

For those of you who didn't find the launch.json file, you have at least two options:

1 - Create the .vscode/launch.json in your directory by yourself;

2 - After starting the Debug mode, go into the left pane in the 'Run and Debug' tab and then click on the configurations icon. Not sure what's going to be there for other languages, but for python you're gonna have multiple configuration options. The basic one is 'Python File', and after clicking there it's going to create the .vscode/launch.json with a standard configuration already in it. You can click on the 'Add Configuration...' button at the bottom right corner to add more configurations or just write it from scratch by yourself.

I hope it helps...

Upvotes: 0

A.L.
A.L.

Reputation: 1201

On your launch.json file, specify a path for additionalSOLibSearchPath:

"configurations": [
    {
        "name": "Debug C++",
        "type": "cppdbg",
        ...            
        "additionalSOLibSearchPath": "/path/to/some/dir/from/which/vscode/can/find/the/lib/**"
    },

Upvotes: 0

NobodyImportant
NobodyImportant

Reputation: 347

Debug => Add Configurations...

This will open a launch.json file. It should look like this:

"version": ...,
"configurations": [
    {

    ... bunch of stuff here

    "justMyCode":false          <==== add this line then save
    }
]

Now you should be able to use breakpoints with external files.

Upvotes: 16

Related Questions