Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何创建和返回C++;来自Rust FFI的结构? 我试图创建并返回一个C++结构。当我尝试编译时,我当前收到一个无法移出原始指针的取消引用的错误。你知道我该怎么做吗 #![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); return 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: *mut octh::root::octave_value_list = ::std::ptr::null_mut(); octh::root::octave_value_list_new(list); std::mem::forget(list); return *list; }_Rust_Ffi - Fatal编程技术网

如何创建和返回C++;来自Rust FFI的结构? 我试图创建并返回一个C++结构。当我尝试编译时,我当前收到一个无法移出原始指针的取消引用的错误。你知道我该怎么做吗 #![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); return 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: *mut octh::root::octave_value_list = ::std::ptr::null_mut(); octh::root::octave_value_list_new(list); std::mem::forget(list); return *list; }

如何创建和返回C++;来自Rust FFI的结构? 我试图创建并返回一个C++结构。当我尝试编译时,我当前收到一个无法移出原始指针的取消引用的错误。你知道我该怎么做吗 #![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); return 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: *mut octh::root::octave_value_list = ::std::ptr::null_mut(); octh::root::octave_value_list_new(list); std::mem::forget(list); return *list; },rust,ffi,Rust,Ffi,我试图创建并返回C++结构体 你不能;C++(如锈)没有稳定的、定义的ABI。Rust中没有办法指定结构具有repr(C++),因此无法创建这样的结构,更不用说返回它了 唯一稳定的ABI是由C表示的。您可以将结构定义为repr(C),以便能够直接返回它们: extern crate libc; use std::ptr; #[repr(C)] pub struct ValueList { id: libc::int32_t, } #[no_mangle] pub extern "C

我试图创建并返回C++结构体

你不能;C++(如锈)没有稳定的、定义的ABI。Rust中没有办法指定结构具有
repr(C++)
,因此无法创建这样的结构,更不用说返回它了

唯一稳定的ABI是由C表示的。您可以将结构定义为
repr(C)
,以便能够直接返回它们:

extern crate libc;

use std::ptr;

#[repr(C)]
pub struct ValueList {
    id: libc::int32_t,
}

#[no_mangle]
pub extern "C" fn hello_world() -> ValueList {
    let list_ptr = ::std::ptr::null_mut();
    // untested, will cause segfault unless list_ptr is set
    unsafe { ptr::read(list_ptr) }
}
但这种方法非常可疑;通常你会把它看作

#[no_mangle]
pub extern "C" fn hello_world() -> ValueList {
    unsafe {
        let mut list = mem::uninitialized();
        list_initialize(&mut list);
        list
    }
}
另见:



<>我鼓励你读我的.< /p>注释:C++还具有一个C FFI,因此C++中任何与C对应的C++类/SUCTRT都可以从C中使用。它现在可以编译了。它可能在工作,但我的八度地狱世界还没有,所以我不是100%确定@CameronTaggart不确定,但您应该防止所有函数上的名称损坏。
Fhelloworld
中是否缺少一个
[no\u mangle]
?@E\u net4我刚刚尝试添加它,但没有成功。我也犯了同样的错误。