扩展 C++ 节点
本章在 Rust 基础工程上接入 C++ 节点能力。
1. 安装构建依赖
2. 新建 build.rs
use roplat_build::{BuildOrchestrator, NativeBackend, NativeBuildConfig};
fn main() {
if std::env::var("ROPLAT_PHASE").as_deref() == Ok("EXTRACT") {
return;
}
let backend = NativeBackend::from_env("ROPLAT_NATIVE_BACKEND").unwrap_or(NativeBackend::Cc);
let native_build = NativeBuildConfig::new()
.backend(backend)
.library_name("roplat_nodes_cpp")
.include_dir("cpp/src");
BuildOrchestrator::new()
.native_build(native_build)
.build()
.expect("Code generation failed");
}
3. 新建 src/puppet.rs
use crate::msg::{MotorCommand, SensorData};
#[roplat::node(
lang = "cpp",
input(sensor_input, SensorData),
output(motor_output, MotorCommand),
)]
pub struct CppController {
pub gain: f64,
pub offset: f64,
}
impl CppController {
pub fn new() -> Self {
Self { gain: 1.0, offset: 0.0, ..Self::default() }
}
}
4. 挂到 main.rs
在顶部增加:
并在 main 增加:
let mut cpp = puppet::CppController::new();
cpp.gain = 0.5;
cpp.offset = -1.0;
println!("CppController gain={} offset={}", cpp.gain, cpp.offset);
5. 生成后目录(tree)
执行 cargo build 后,重点检查:
rust_starter/
├─ cpp/
│ ├─ roplat_gen/
│ │ ├─ sensor_data.h
│ │ ├─ motor_command.h
│ │ ├─ cpp_controller_base.h
│ │ └─ cpp_controller_ffi.cpp
│ └─ src/
│ └─ cpp_controller.h
├─ src/
│ └─ puppet.rs
└─ build.rs
6. 构建
通过后进入 扩展 Python 节点。