Reputation: 572
I would like to use some native (unmanaged) C++ in C#. What I found out is that I have two options, explicit and C++ Interop (implicit pinvoke).
Microsoft's documentation recommends implicit over explicit. But there is not much information about what I can and cannot do this way. This said in 2014 that implicit does not work with Mono, and would therefore not work on Linux systems. But what is the situation today?
I want to use the C++ library on any platform (win,mac,linux,android,ios) from my C# applications. It's a header-only template library written in C++14. I would like to know if I can use the recommended C++ Interop to do this or whether I have to write a C-Style API and use explicit PInvoke for this.
Best regards
Thorsten
Upvotes: 0
Views: 978
Reputation: 127603
Looking at Unity's documentation for Native dll files they show you should use explicit interop when doing P/invoke with Unity. Here is their example from the docs:
using UnityEngine;
using System.Runtime.InteropServices;
class SomeScript : MonoBehaviour {
#if UNITY_IPHONE
// On iOS plugins are statically linked into
// the executable, so we have to use __Internal as the
// library name.
[DllImport ("__Internal")]
#else
// Other platforms load plugins dynamically, so pass the name
// of the plugin's dynamic library.
[DllImport ("PluginName")]
#endif
private static extern float FooPluginFunction ();
void Awake () {
// Calls the FooPluginFunction inside the plugin
// And prints 5 to the console
print (FooPluginFunction ());
}
}
Do note you can't do all of the platforms you want to do with a single c++ dll. You will need to compile a dll per platform, you can put the files in seperate subfolders so all dll files can use the same name to make your C# code easier. You then can use the PluginInspector to set which platform which dll should be used at runtime.
Upvotes: 1