Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to call a compiled function from a C program. I am generating the static object


through the Mathematica code

<< CompiledFunctionTools`
compiledfunction = Compile[{x, _Real}, x];
LibraryGenerate[compiledfunction, "test"]

then by using the source test.c

#include <stdio.h>

double compiledfunction(double);

int main(){
  double a=2e0;
  return 0;

How I should call the compiler to link with the static object above?

share|improve this question
It's an interesting question, but I think this won't be possible because compiled functions do need to access the Mathematica kernel. Perhaps generating C code with CCodeStringGenerate and extracting the part that doesn't depend on Mma would be better? Or you can access the full Mathematica kernel from C through MathLink. – Szabolcs Apr 7 '14 at 16:14
I would like to compile a stand-alone object file. Surely, I do not need to access the whole M kernel. Let us say for example only the PolyLog function. – Fabio Apr 7 '14 at 17:15
I've tried to compile with ( copied in the actual directory) icc test.c – Fabio Apr 7 '14 at 17:17
Yes, you do need the whole Mathematica kernel to use LibraryLink. Otherwise you need to generate C code, remove the part that depends on the M kernel and use the rest in your project. If you try to link the library as compiled by Mathematica, then you do need the whole kernel and you can't have a standalone project. I might be wrong, but I find it very unlikely that what you are trying to do (just link the library) is possible. – Szabolcs Apr 7 '14 at 18:21
You might be interested in MathCode which should help in compiling into re-usable C++ code. I've never used it myself and it's unfortunately not free. – Szabolcs Apr 7 '14 at 18:45

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.