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

相关推荐
罗政6 小时前
冒险岛079 V8 整合版源码搭建教程+IDEA启动
java·ide·intellij-idea
艾斯比的日常8 小时前
VSCode 实用快捷键
ide·vscode·编辑器
Galaxy_12298 小时前
vscode远程报错:Remote host key has changed,...
ide·vscode·编辑器
彬sir哥8 小时前
VScode运行后出现黑窗口
vscode·运行·黑窗口
武侠编程13 小时前
Rust兵器谱|流星镖:tokio
rust
Hi~晴天大圣13 小时前
Pycharm中断点使用技巧
ide·python·pycharm
且随疾风前行.14 小时前
Android Studio 提示 !Failed to initialize editor
android·ide·android studio
比牛顿更懂PID15 小时前
VSCode本地python包“无法解析导入”
ide·vscode·python
大雄野比17 小时前
rust学习三、基本类型
开发语言·学习·rust
Hello.Reader17 小时前
使用 `cargo install` 安装 Rust 二进制工具
开发语言·后端·rust