Search code examples
visual-studio-coderustcodelldb

Cannot open shared library when debugging with CodeLLDB


I am working on a proof-of-concept app, written in Rust, with the end goal being to produce a shared library (.dll/.so) callable via C ABI from a number of other languages (C++, C#, etc). I have two simple components; poc is a Rust console app, which references poclib which exposes some simple functions. The app itself builds and runs fine so far, but I am stuck on how to debug it in VSCode using CodeLLDB.

I have a top level "workspace" like this:

[workspace]

members = [
    "poc",
    "poclib"
]

poc/cargo.toml looks like this:

[package]
name = "poc"
version = "0.1.0"
edition = "2018"

[dependencies.poclib]
path = "../poclib"

[dependencies]

And poclib/cargo.toml looks like this:

[package]
name = "poclib"
version = "0.1.0"
edition = "2018"

[lib]
crate_type = ["cdylib"]

[dependencies]
unicode-segmentation = "1.7.1"

My launch.json looks like this:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug executable 'poc'",
            "cargo": {
                "args": [
                    "build",
                    "--bin=poc",
                    "--package=poc"
                ],
                "filter": {
                    "name": "poc",
                    "kind": "bin"
                }
            },
            "args": [ ],
            "cwd": "${workspaceFolder}"
        }        
    ]
}

When I try to launch and debug in VSCode with the CodeLLDB extension installed, the app builds but then raises an error: /home/username/src/rustpoc/target/debug/poc: error while loading shared libraries: libpoclib.so: cannot open shared object file: No such file or directory. If I just do cargo run instead, it builds and runs fine, and I can verify that libpoclib.so is being built and placed in the ./target/debug folder.

If I comment out the crate_type option in the poclib/cargo.toml, it launches fine and I can hit breakpoints, but the shared library is no longer created.

I've tried adding an LD_LIBRARY_PATH setting to the launch.json, like this:

"env": {
    "LD_LIBRARY_PATH": "${workspaceFolder}/target/debug"
},

That doesn't fix anything, but it does change the error message - with that setting I get /home/username/src/rustpoc/target/debug/poc: error while loading shared libraries: libstd-0a9489cf400f65e4.so: cannot open shared object file: No such file or directory

How can I enable debugging Rust in VSCode while still producing shared libraries?


Solution

  • I don't understand why it worked at all initially, but the solution was to fix the crate_type option so that I'm producing both C ABI libraries and native Rust libraries.

    crate_type = ["cdylib","lib"]
    

    With that setting the build output contains both a libpoclib.so for use from C, and a libpoclib.rlib which the poc binary can link statically against, and LLDB debugging works as expected.