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

相关推荐
ssshooter7 小时前
Tauri 项目实践:客户端与 Web 端的授权登录实现方案
前端·后端·rust
AI智动派10 小时前
《深入 Rust Async/Await:如何实现一个带超时保护与安全沙箱的 LLM Agent 循环》
rust
范特西林2 天前
一次 to_bits() 引发的 Rust 与 C++ 底层思考
rust
冬奇Lab3 天前
一天一个开源项目(第42篇):OpenFang - 用 Rust 构建的 Agent 操作系统,16 层安全与 7 个自主 Hands
人工智能·rust·开源
量子位3 天前
Transformer论文作者重造龙虾,Rust搓出钢铁版,告别OpenClaw裸奔漏洞
rust·openai·ai编程
哈里谢顿3 天前
Rust 语言入门博客
rust
DongLi015 天前
rustlings 学习笔记 -- exercises/06_move_semantics
rust
ssshooter5 天前
Tauri 踩坑 appLink 修改后闪退
前端·ios·rust
布列瑟农的星空5 天前
前端都能看懂的rust入门教程(二)——函数和闭包
前端·后端·rust
蚂蚁背大象6 天前
Rust 所有权系统是为了解决什么问题
后端·rust