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 !

  • 1
    $\begingroup$ 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. $\endgroup$ Commented Mar 14 at 11:28
  • 1
    $\begingroup$ Does that make sense to you? I could also show a worked out example if you need it. $\endgroup$ Commented Mar 14 at 11:31
  • 1
    $\begingroup$ 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. $\endgroup$
    – flinty
    Commented Mar 14 at 12:46
  • 1
    $\begingroup$ Here is an example of the practice I mentioned before: mathematica.stackexchange.com/a/269065/38178 $\endgroup$ Commented Mar 14 at 15:04
  • 1
    $\begingroup$ @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. $\endgroup$ Commented Mar 14 at 22:46


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.