Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

rust

ffi

I am trying to use "xerces-c" via FFI in Rust without success. In C++, I would write the following code to use it:

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();

How can I use these "complex" data types within Rust? I found many examples to export/create an FFI to use it in other languages but none to use complex types within Rust.

extern crate libc;

#[link(name = "xerces-c")]
extern {
    // How do i have to implement the constructor here? 
}
like image 401
kpalatzky Avatar asked Mar 08 '23 10:03

kpalatzky


1 Answers

Rust doesn't support FFI with C++. If you want to use this library, you will have to find or write a translation layer that provides a pure C interface to the library, then bind to that.

like image 178
DK. Avatar answered Mar 15 '23 13:03

DK.