私はRustでOctave関数をビルドしようとしています。 OctaveのAPIはC++なので、rust-bindgenを使ってバインディングを生成しました。私は現在、bindings that include std::string
を生成しようとしているときに発生する問題を解決しています。 C++ std::string
への不透明で有効なポインタを残すことができればいいと思います。 C++で渡す必要があるときはいつでも、C++側でユーティリティ関数を構築することは可能でしょうかstd::string
?Rust interop with C++ std :: string
私が最初にこれを試みたとき、私は素朴でした。明らかに間違っています。錆std::ffi:CString
はC文字列であり、C++文字列ではありません。私はthis recent blog 2つを比較するときに役立つことが分かった。私の最初の試みは、like thisになります:
#![allow(non_snake_case)]
#![allow(unused_variables)]
extern crate octh;
// https://thefullsnack.com/en/string-ffi-rust.html
use std::ffi::CString;
#[no_mangle]
pub unsafe extern "C" fn Ghelloworld (shl: *const octh::root::octave::dynamic_library, relative: bool) -> *mut octh::root::octave_dld_function {
let name = CString::new("helloworld").unwrap();
let pname = name.as_ptr() as *const octh::root::std::string;
std::mem::forget(pname);
let doc = CString::new("Hello World Help String").unwrap();
let pdoc = doc.as_ptr() as *const octh::root::std::string;
std::mem::forget(pdoc);
octh::root::octave_dld_function_create(Some(Fhelloworld), shl, pname, pdoc)
}
pub unsafe extern "C" fn Fhelloworld (args: *const octh::root::octave_value_list, nargout: ::std::os::raw::c_int) -> octh::root::octave_value_list {
let list_ptr = ::std::ptr::null_mut();
octh::root::octave_value_list_new(list_ptr);
::std::ptr::read(list_ptr)
}
私はoctave_dld_function_create
に文字列として関数名やドキュメントに渡す必要があります。代わりに私が使用できるCppString
があることを願っています。どのように進むべきかについての提案はありますか?
Cを知らない++コンパイラ/ stdlibのベンダーはこれを理解していません。私はRustには期待しないだろう。 ; - ](明らかに 'std :: string'は必須の実装であり必須の実装ではありません。値で_anything_を渡したい場合は少なくともサイズ/レイアウトを知る必要があります) – ildjarn
私は試していますUbuntu LinuxでGNU Octaveと相互運用するコンパイラは 'gcc -dumpversion'のgcc 6.3.0で、stdlibは' ldconfigのlibstdC++ .so.6(libc6、x86-64)=> /usr/lib/x86_64-linux-gnu/libstdc++.so.6です。 -p | grep stdC++ 'を実行します。 https://stackoverflow.com/a/10355215/23059 –
「_GLIBCXX_USE_CXX11_ABI」の有無にかかわらず定義されていますか? ; - ]ポイントは、C++で構築ツールを適切に抽象化していない場合は、他の場所で行うことは非常にスリムです。例えば。私のシステムには、libC++、libstdC++、およびDinkumwareのstdlibがあります。 – ildjarn