Search code examples
c#c++.net.net-corepinvoke

Dllimport/PInvoke binary compatibility


I'm learning about PInvoke to use a C++ library (with C-Style interface) in C#. After reading the documentation and searching Google/StackOverflow for additional information I was wondering about binary compatibility of the native library and .Net. I think I read something somewhere a while ago, but I couldn't find it anymore. And I could not find anything else on this.

When I compile a C/C++ to use in .Net, do I need to use certain configurations like compiler flags to make it compatible for PInvoke? Can I use native libraries made with different compilers in one .Net project, provided these libraries don't depend on each other?

I there anything else I need to know about C/C++ compilation for .Net PInvoke?

I would like to use GCC and CLang with CMake projects to create the native libraries.


Solution

  • P/Invoke has a lot of adaptation capabilities. Also P/Invoke is cross platform (with .NET Core).

    However, it's not binary compatible with C/C++, but if a piece of C/C++ code can be used by P/Invoke, it's not dependent on the C/C++ compiler (MSVC or other), or said in another way, it will not be able to use any C++ construct, but it will see all C++ compilers as equal citizens