嵌套式项目中Vscode中调试Rust的一些问题解决

嵌套式项目指定Rust目录

由于是嵌套式项目,比如C++代码中对Rust进行了Bind,Rust目录就在项目子目录中,为了让vscode中安装的rust-analyzer识别到正确的rust项目目录,在.vscode/settings.json中添加如下

json 复制代码
    "rust-analyzer.linkedProjects": [
        "xxx/yyy/Cargo.toml" 
    ]

Vscode debug Rust

比如rust项目中有个test程序,test_demo。在vscode中,需要安装LLDB的插件,可以生成launch.json文件,以及选择LLDB的选项生成不同Rust调试配置,比如cargo test, cargo build, attach, launch等。

配置项如下:

json 复制代码
{
    "version": "0.2.0",
    "configurations": [

        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug executable 'hello_world'",
            "cargo": {
                "args": [
                    "test",
                    "-p",
                    "gpt4-rs",
                    "--test",
                    "test_create_model",
                    "--no-run",
                    "--manifest-path=${workspaceFolder}/xxx/rust/Cargo.toml"
                ]
                // "filter": {
                //     "name": "hello_world",
                //     "kind": "bin"
                // }
            },
            "program": "${cargo:program}",
            "args": [],
            "cwd": "${workspaceFolder}/gpt4all-bindings/rust",
            "env": {
                "LD_LIBRARY_PATH": "/home/ken/Codes/xxx/build:/home/ken/Codes/yyy/build"
            }
        }
       ]
}

包括了如下细节:

(1)设置环境变量env;

(2)--manifest-path=${workspaceFolder}/xxx/Cargo.toml设置rust项目位置,不然识别不了;

(3)设置test目标名称:test_demo,设置rust package name:gpt4-rs。

rust binding调试c++ so

如果要为c++或者c代码绑定rust code,c++编译生成动态链接库。这时要调试so,选择上面的launch方式,cargo run 或者 cargo test运行rust代码。

然后将so编译成为debug模式,在vscode的c++代码中打上断点,启动程序调试。直接就会断点到so内部对应的代码行。

两个要点:

  • 编译动态链接库debug版本
  • 按照上一节配置rust vscode调试启动程序

cmake项目编译debug so:

复制代码
cmake -B build -S . -DCMAKE_BUILD_TYPE=DEBUG
cmake --build build

attach是用于已经启动了一个持续运行的程序(比如启动了一个server)(如果程序一下子就结束了,这种方式使用launch调试),这个程序加载了so,然后调试so就选择attach(中文直译就是依附到这个程序上,翻译有点傻)。

相关推荐
大卫小东(Sheldon)18 分钟前
GIM 1.5发布了! 支持Windows系统了
git·ai·rust
寻月隐君1 小时前
告别竞态条件:基于 Axum 和 Serde 的 Rust 并发状态管理最佳实践
后端·rust·github
kiss strong1 小时前
好用的自带AI功能的国产IDE
ide
成遇9 天前
在Vscode中安装Sass并配置
vscode·rust·sass
止观止9 天前
Rust智能指针演进:从堆分配到零复制的内存管理艺术
开发语言·后端·rust
学無芷境9 天前
Cargo 与 Rust 项目
开发语言·后端·rust
API开发10 天前
苹果芯片macOS安装版Homebrew(亲测) ,一键安装node、python、vscode等,比绿色软件还干净、无污染
vscode·python·docker·nodejs·openssl·brew·homebrew
微风好飞行10 天前
Vite 打包 vscode 扩展遇到的模块问题
javascript·vscode·vite
Python私教10 天前
源滚滚AI编程:我为什么选择Rust开发博客项目
开发语言·rust·ai编程