如何通过 Rust FFI 调用C++构造函数?

How do I call a C++ constructor via Rust FFI?

本文关键字:C++ 构造函数 调用 FFI 何通过 Rust      更新时间:2023-10-16

我试图通过 Rust 中的 FFI 使用 "xerces-c",但没有成功。在C++中,我将编写以下代码来使用它:

XMLPlatformUtils::Initialize();
{
XercesDOMParser domParser;
ParserErrorHandler parserErrorHandler;
domParser.setErrorHandler(&parserErrorHandler);
domParser.setDoSchema(true);
domParser.setValidationSchemaFullChecking(true);
domParser.parse(xmlFilePath.c_str());
if(domParser.getErrorCount() != 0) {     
// ...
}
}
XMLPlatformUtils::Terminate();

如何在 Rust 中使用这些"复杂"数据类型?我找到了很多导出/创建 FFI 以在其他语言中使用它的示例,但没有一个示例可以在 Rust 中使用复杂类型。

extern crate libc;
#[link(name = "xerces-c")]
extern {
// How do i have to implement the constructor here? 
}

Rust 不支持 FFI 和 C++。 如果你想使用这个库,你必须找到或编写一个为库提供纯C接口的转换层,然后绑定到层。