Open
Description
I’ve developed a Rust library for some core computations, and I want to expose it so it can be used from a C++ project.
Example
To explore how this integration works, I’m using the following minimal demo.
Rust: src/lib.rs
#[cxx::bridge]
mod ffi {
extern "Rust" {
fn print_message_from_cpp(msg: &str);
}
}
fn print_message_from_cpp(msg: &str) {
println!("Received from C++: {}", msg);
}
C++: src/main.cc
#include "cxx.h"
#include "lib.rs.h"
#include <string>
int main() {
std::string cpp_msg = "Hello from C++!";
rust::String rust_compatible(cpp_msg.c_str());
print_message_from_cpp(rust_compatible);
return 0;
}
Build script: build.rs
fn main() {
cxx_build::bridge("src/lib.rs").compile("rust_cpp_demo");
println!("cargo:rerun-if-changed=src/lib.rs");
}
Compilation
I'm trying to compile the C++ code with:
g++ -std=c++17 src/main.cc -o cpp_runner \
-I target/cxxbridge/cxx_demo/src/ \
-I target/cxxbridge/rust/ \
-L target/debug/build/cxx_demo-<hash>/out \
-L target/debug/build/cxx-<hash>/out \
-lcxxbridge1 -lrust_cpp_demo -lpthread -ldl
(Replacing with the correct build hashes.)
However, this leads to numerous undefined reference errors for symbols defined by cxx, like:
undefined reference to `cxxbridge1$rust_vec$str$set_len`
The Problem
I'm unclear on the correct steps for compiling and packaging the Rust library so that its C++ bindings can be linked and used from an external project. Specifically, how should I build and expose the cxx and Rust-generated symbols correctly?
Metadata
Metadata
Assignees
Labels
No labels