C++ and C# PInvoke Mac

I am trying to run .dylib library of mac in c# of mac.

It is all new for me and I know this website Interop with Native Libraries | Mono
and also mcneel guide.

In windows C++ I always decorate PInvoke methods with

extern "C"{
__declspec(dllexport) void MethodName()
}

Is there any alternative in Mac for __declspec(dllexport) decoration, since this wont build on mac?

I decorate my function declarations with defines that are controlled by the build system

Then the initialization function looks like this

With implementation

Hi,

Thanks for the answer. It seems the answer to my question is true, I can use this PInvoke method.
I managed to compile .dylib on Mac and call in C# ConsoleApp in VisualStudio mac by:

C++ Side:


.hpp:
#define PINVOKE extern "C"
PINVOKE int test (int n);

.cpp:
PINVOKE int test (int n){
return n*n;
}

and C#

[DllImport("libsimpleDYLIB.dylib")]
private static extern int test (int n);

I am super suspicious why it is so simple. I did not even had to change anything in default Mac Visual Studio configuration.

@nathanletwory
Is there also a possibility to compile Grasshopper components in Mac Visual Studio?
Or RhinoCommands ? Since it is not a .NET framework
Are there any samples?

This guide might (also) be worth your time.

– Dale

1 Like

Thanks @Dale, I think I will use your documentation since I will get more into Mac development.
I see that you made a tutorial for a Command Line, and do you also have for Grasshopper? Or it is not possible due to .NET?

Both RhinoCommon plug-ins and Grasshopper plug-ins are just .NET assemblies. So the guide (above) should work to build Grasshopper components that call into unmanaged libraries.