6
$\begingroup$

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

/home/fabio/.Mathematica/SystemFiles/LibraryResources/Linux-x86-64/test.so

through the Mathematica code

<< CompiledFunctionTools`
Needs["CCodeGenerator`"]
Needs["CCompilerDriver`"]
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;
  printf("%20.12e\n",compiledfunction(a));
  return 0;
}

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

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Szabolcs Apr 7 '14 at 16:14
  • $\begingroup$ 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. $\endgroup$ – Fabio Apr 7 '14 at 17:15
  • $\begingroup$ I've tried to compile with (test.so copied in the actual directory) icc test.so test.c $\endgroup$ – Fabio Apr 7 '14 at 17:17
  • $\begingroup$ 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. $\endgroup$ – Szabolcs Apr 7 '14 at 18:21
  • $\begingroup$ 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. $\endgroup$ – Szabolcs Apr 7 '14 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.