Search code examples
c#c++interopc++-climixed-mode

How to compile a C++/CLI program into .lib and link it with a pure unmanaged C++ program?


I'm following this http://www.codeproject.com/Articles/10020/Using-managed-code-in-an-unmanaged-application

The example consist of 3 binaries:

  1. C# code
  2. C++/CLI code
  3. C++ code

The C++ code calls on the C++/CLI code, which then calls on the C# code, to achieve a way of running C# codes from C++.

The problem is, the C++/CLI is compiled as a .dll, when I tried to compile it into .lib, so that the C++ code and be linked together with the C++/CLI code, it fails.

So that the end result will only consist of 2 binaries.

  1. C# code
  2. C++ --LINK-- C++/CLI code

The error

Error   1   error LNK2019: unresolved external symbol "__declspec(dllimport) public: static void __cdecl IMessageBoxWrapper::Destroy(class IMessageBoxWrapper *)" (__imp_?Destroy@IMessageBoxWrapper@@SAXPAV1@@Z) referenced in function _main  C:\Users\Zero\Desktop\UmanagedApp\UmanagedApp\main.obj  UmanagedApp

Error   2   error LNK2019: unresolved external symbol "__declspec(dllimport) public: static class IMessageBoxWrapper * __cdecl IMessageBoxWrapper::CreateInstance(void)" (__imp_?CreateInstance@IMessageBoxWrapper@@SAPAV1@XZ) referenced in function _main C:\Users\Zero\Desktop\UmanagedApp\UmanagedApp\main.obj  UmanagedApp

Solution

  • Since you no longer have a DLL, you shouldn't be using __declspec(dllimport) at all.

    Actually, I strongly discourage using that on classes in any case, it's very fragile.

    So just remove DLLAPI from all the class definitions.