嵌套式项目中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(中文直译就是依附到这个程序上,翻译有点傻)。

相关推荐
对象存储与RustFS4 小时前
RustFS 后台扫描器与自愈调参:五档速度、位腐检测周期与并发上限
后端·rust·开源
witton6 小时前
MacOS中在目录或者文件右键菜单中添加“使用VSCode打开”
vscode·macos·文件·目录·服务·automator·右键菜单
wabil7 小时前
[AI-Talk] codx制作xmind文件技巧
ide·ai编程·myeclipse·xmind
q平面人18 小时前
【总工笔记】mindoc平板、桌面端笔记软件,发布到mindoc服务端
rust·tauri·华为平板·mindoc·总工笔记
大衛說18 小时前
02 · 环境搭建:uv 包管理与现代 IDE
ide·uv
MC皮蛋侠客19 小时前
Tauri 2.x 系列(九):安全模型——Capabilities、Permissions、Scope 与 CSP
rust·tauri
Source.Liu20 小时前
【Dioxus】Windows 环境下 Rust + Dioxus 安装配置笔记
windows·rust·dioxus
cndes1 天前
安装Miniconda+Jupyter notebook
ide·python·jupyter
zLLM_Lab1 天前
不装 Python/PyTorch:8.1 MB Rust 程序在 MacBook Air M5 跑多模态大模型
rust
码上暴富1 天前
Cursor / VS Code 自定义文件颜色
前端·vscode