119

92

**Update: While at the time of writing the question loading DLLs with .NET/Link seemed easier, now I always use LibraryLink, which I recommend to anyone with a similar problem!**

As of Mathematica 8, what is the minimal effort way to integrate an existing C++ function into Mathematica?

I think we have these:

- MathLink (it was quite long ago I used it last time)
- communication through pipes/files (
`Import`

is slow,`ReadList`

not so much) - LibraryLink (??)
~~Mathematica 8's C code generation features (??)~~_{(apparently this is not relevant)}

The keyword here is *minimal effort*. Which is the most convenient way, including the learning curve for the particular method?

_{I'm mainly interested in doing it on Windows. My particular function computes a long list of real numbers. The function input consists only of a few scalars (int & double).}

_{An answer of the type "You'll likely spend the least time if you use technology X" is useful. A concrete example of how to do it using a small function is even better.}

## Answers

Each and every answer I got is great, and each demonstrates a different technology. It's impossible to choose a definitive one. Here's a small "table of contents" for them:

- MathLink
- Read result from pipes, pass arguments on command line to separate process
- LibraryLink
- Using .NETLink for loading functions from arbitrary DLLs

A presentation about the topic from the Wolfram Technology Conference is **here** (CDF file).

## Experiences

I used two of these methods: the .NETLink method and Library Link. These are my personal experiences:

**.NETLink**This was easy to set up, however, I'd recommend it only for when you already have a compiled DLL from which you need to load functions (e.g. do something like this). If you compile your own DLL: once the DLL is loaded, it is locked and cannot be overwritten. A quick and dirty way to allow overwriting it is realoding NETLink with`ReinstallNET[]`

.Advantages: Very quick and easy, only Mathematica code is needed if the functions are already compiled into a DLL.

Disadvantages: Windows-only, does not parallelize from within Mathematica, and the calculation will not be interruptible.

**Library Link**It's much easier to use this than what it looks like at first. Less setup is needed than in the case of MathLink, and compilation is automated from within Mathematica.Advantages: Also quite easy, but both Mathematica and C code are needed. It is parallelizable, and easily made interruptible. The library can be unloaded (for recompilation) using

`LibraryUnload`

.

Is that a

`List`

of scalars, or a finite number of parameters? – rcollyer – 2011-11-15T18:04:48.637@rcollyer By "scalars" I meant that I have a finite number of parameters, each of which is a number. I wanted to point out that I don't need to pass variable (or fixed...) length vectors/list – Szabolcs – 2011-11-15T18:06:20.393

That's what I thought you meant, I was just trying to be clear. – rcollyer – 2011-11-15T18:11:59.533

`CCompilerDriver/tutorial/Compilation#472267182`

in the Mathematica documentation is probably a good starting point. – Brett Champion – 2011-11-15T18:22:55.1404The C code generation features are about converting Compile expressions to C code for speed or use them outside Mathematica, rather than accessing existing C code from the kernel. – Joel Klein – 2011-11-16T15:45:51.537

@jfklein Thanks for the comment and the indirect help through Arnoud's answer! It wasn't clear to me if it's possible to use this to intermingle hand-written code with generated one. Some of the answers I got do use some related features to automate the process. – Szabolcs – 2011-11-17T08:50:54.733