Can this be done? (C++ interaction and dllimport)


I’m still trying to make a POC to get some bullets to prove that Rust is viable and can improve our lifes in my company, but for that, I need to interact with an existing house (ill-)made SDK which is part C, part C++.

For interacting with the C++, I had to write a C-wrapper because the C++ code is so weird and so Windows specific that bindgen is totally lost. I manage to wrap many many thing and so near from having the final prototype, I’m stuck in a linker problem.

the SDK provide this abstract class:

class __declspec(dllimport) BaseClass
   void Start();
  virtual void toOverride() = 0;

in a C static lib, I’m implementing a concrete class:

class MyClass: public BaseClass {
   void toOverride() override;

MyClass::MyClass() : BaseClass(/*...*/) {}

MyClass::toOverride() {
  // bla bla

and wrapping it in C functions:

extern "C" {
  MyClass* MyClass_new();
  void MyClass_delete(MyClass* ptr);
  void MyClass_toOverride(MyClass* ptr);

Then I’m using bindgen on my CWrapperLib and try to build the POC, which is a DLL (cdylib) to be loaded by the existing platform.

My issue is at linking time:

CWrapperLib.lib : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall BaseClass::BaseClass(/*...*/)

and another one for the Start method, very similar.

I’m assumption is that this __declspec(dllimport) screw things up because I’m implementing it in a static lib that I am intendind to link to my Rust produced DLL.

In pure C++ implementation, I’ve seen that the BaseClass is implemented in a DLL.

So my question is: can I get over this issue or do I need a C++ DLL that load a Rust static lib and not the other way around?

Thank you

Does the SDK include a .lib file to link plugins against? If so, does your build script link against it using appropriate rustc-link-lib and/or rustc-link-search values (documentation)?

Thank you for your answer!

Yes, I’ve located the lib where these symbols are. But I did some println in the instead of putting them in Cargo.toml, I’ll try that Monday!

Thanks again!

I feel dumb: it was a setting in my CWrapper lib that was on “Treat WChar_t As Built in Type” whereas the lib I was linking to was not…

Thanks anyway :smiley: