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

相关推荐
belowfrog1 分钟前
Rust 的 Deref 到底为啥这么乱呀!
rust
程序员爱钓鱼2 小时前
Rust 数组 Array 详解:定义、访问、遍历与切片
后端·rust
love530love1 天前
【硬核排障 & 猴子补丁 & 幽灵节点】SageAttention GQA 崩溃与 Flash Attention 兼容性修复全记录
ide·人工智能·windows·comfyui·sageattention·windows cuda
程序员爱钓鱼1 天前
Rust if let 与 while let 详解:简化模式匹配
前端·后端·rust
第一程序员2 天前
Rust trait 入门:把 AI 客户端抽象成可替换接口
python·rust·github
分布式存储与RustFS2 天前
RustFS Beta.10 性能解读:PUT 全面反超 MinIO,Rust 重写对象存储成了?
开发语言·后端·rust
l1t2 天前
测试用rust重写的postgresql: pgrust
开发语言·postgresql·rust
@atweiwei2 天前
Langchainrust:中LLM-as-a-Judge,用 Rust 实现一套 LLM 评估系统
开发语言·后端·ai·rust·llm·rag
浪客川2 天前
idea 技巧 region 的使用
java·ide·intellij-idea
热爱生活的五柒2 天前
如何快速删除vscode项目下所有的http链接
ide·vscode·编辑器