Here is my goal: I want to call external library functions, written in C (not by me), in Mathematica.

I may have found a way to do it using LibraryLink by following this scheme:

  • First, for each function that I want to call I have to create a wrapper (not sure it's the correct term) in order to give the good format to the C function so Mathematica can use it.
  • Second, use the FindLibrary function where I have to specify the path to find the library.
  • Third, use the LibraryFunctionLoad function to load the function. For this function I have to specify the name of the function in this library, the list of arguments types needed by the C function and the return type of the C function.

I am not at all familiar with all the concepts that I mentioned so my scheme is probably not correct.

I am not sure how to create this wrapper (if it's the correct term) even if I found some information here.

I also found that it is possible to call external libraries using MathLink (or WSTP) or .NET/Link.

Here are my questions:

  • Does my scheme is good or not at all ?
  • Some C functions have pointers as arguments or return pointers, do we have something similar in Mathematica ? I found something called MArgument for union of pointers but I don't know if it allows Mathematica to deal with pointers.
  • Do I have to create special files for the wrapper ? Are they the only files that I need ?
  • Does LibraryLink is the best way of dealing with these C libraries ?

Just to precise I am on MacOs BigSur 11.6.2.

Thanks guys.

  • $\begingroup$ I think the best advice here is to go slow and learn how to use LibraryLink with small projects first. LibraryLink will give you the most flexibility out of all the options, but it will also be the most work to set up. You will need to become comfortable with C programming first, if you aren't already. $\endgroup$
    – Szabolcs
    Commented Jun 14, 2022 at 15:25
  • 1
    $\begingroup$ The data types you can easily transfer using LibraryLink are n-dimensional arrays of integers or reals. With MathLink (which is usable from LibraryLink) you can transfer any kind of Mathematica expression directly. Manipulating it in C is a different matter, and with complex expressions it won't be easy. If you want to interface with a C library that uses custom data structures, you will need to translate those into some form that is representable in Mathematica. This can't be usefully discussed without a concrete example. I suggest to go slow, learn LibraryLink, and ask more focused questions $\endgroup$
    – Szabolcs
    Commented Jun 14, 2022 at 15:27
  • 2
    $\begingroup$ New versions are slowly but steadily bringing in more and more of the new compiler framework, which should eventually make calling external C libraries relatively easy, without external compilation etc. The real pain point here are C data structures and their lifetime management. If you go down that line, the next release might already have much more of the necessary stuff available. $\endgroup$
    – kirma
    Commented Jun 14, 2022 at 15:34
  • 1
    $\begingroup$ To see some recent work in progress in relation to the new compiler framework can be seen at the currently latest compiler-related live CEOing recording from April 2022 at youtu.be/51P-upxqhds . $\endgroup$
    – kirma
    Commented Jun 14, 2022 at 15:47
  • 3
    $\begingroup$ @kirma Re lifetime management: managed library expressions can do this. $\endgroup$
    – Szabolcs
    Commented Jun 14, 2022 at 15:56


Your Answer

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

Browse other questions tagged or ask your own question.