2

I work on a mac M1 with MacOS Sonoma and i use Mathematica 12.3.1 and 14.0

I wanted to ask if there is a way to load a .dylib library on Mac with Mathematica, which was compiled without using the Wolfram library (LibraryFunctionLoad) and without having to compile it with Mathematica (CreateLibrary)?

On Windows, I found that there is something called DefineDLLFunction which seems to work for loading .dll files, but I haven’t been able to make it work with .dylib files on macOS.

I use LibraryFunctionLoad and CreateLibrary already but it force me to put wolfram library in my C++ code and i would want to avoid that if possible

Thanks !

Alex
  • 51
  • 2
  • 1
    You merely need to write a wrapper in LibraryLink that calls the function from the external library and makes sure that the results are stored in data types that Mathematica can understand (e.g., in MTensors). Then you combile this wrapper with CreateLibrary and include the according linker commands with the option LinkerOptions -> {"-l<library name>}. You might also have to provide the path to that library with the LibraryDirectories and the path to the included header files with the IncludeDirectories. – Henrik Schumacher Mar 14 '24 at 11:28
  • 1
    Does that make sense to you? I could also show a worked out example if you need it. – Henrik Schumacher Mar 14 '24 at 11:31
  • 1
    Wrap the library and define a C API using extern "C" and __attribute__((visibility("default"))) . You don't necessarily need the Mathematica data types. If the C++ library has a C library interface already you may not have to do this, but otherwise, function names end up mangled in C++. However, even then you could still find those name mangled functions and call those if the signature types are simple ones. – flinty Mar 14 '24 at 12:46
  • 1
    Here is an example of the practice I mentioned before: https://mathematica.stackexchange.com/a/269065/38178 – Henrik Schumacher Mar 14 '24 at 15:04
  • Ah, okay. So, I can create the wrapper directly from Mathematica. I will try this. I didn’t understand what you said, Flinty: Can I load a C++ library without using a wrapper? Can I do this with LibraryFunctionLoad? – Alex Mar 14 '24 at 16:03
  • 1
    @Alex, if you ask me: No it is not possible without a wrapper. You have to translate the data types that Mathematica uses to something that the library can understand: standard C types and pointers to such. – Henrik Schumacher Mar 14 '24 at 22:46
  • Thanks for the explanation and the example (https://mathematica.stackexchange.com/a/269065/38178) i succeeded in implementing it like this – Alex Mar 15 '24 at 09:52

0 Answers0