I would like to build up a Mathematica package Pck
which contains compiled functions. However when loading this library I obtain an error. When reduced to a minimal (non-)working example, the notebook Pck.nb
defining my package contains
BeginPackage["Pck`"];
f2::usage = "f2";
f1::usage = "f1";
f::usage = "f";
Begin["Private`"];
f2 = Compile[{{x, _Real}},
1
, CompilationTarget -> "C"];
f1 = Compile[{{x, _Real}},
f2[x]
, CompilationTarget -> "C"];
f = Compile[{{x, _Real}},
f1[x]
, CompilationTarget -> "C"];
End[];
EndPackage[];
As you may note, the functions f
, f1
and f2
are imbricated one into another. I can then save this notebook as a Wolfram Mathematica package Pck.m
. The next step is to load this package in my test notebook using
<< "Pck.m";
However, I obtain systematically obtain an error of the form
CCompilerDriver`CreateLibrary::cmperr: Compile error:
/.../compiledFunction2.c:83:12:
error: static declaration of 'F1' follows non-static declaration >>
Compile::nogen: A library could not be generated from the compiled function. >>
It seems that having imbricated compiled functions leads to this error message.
However, even with this error, I can still successfully evaluate f[1]
within the test notebook. If I get rid of f2
, so that f1
does not require to evaluate f2
, this error disappears. Therefore, my conclusion is that the interweaving of the compiled function is responsible for this bug. (Even if I had the constraints {{f1[_],_Real}}
and {{f2[_],_Real}}
to the definitions of f
and f1
, the bug remains.)
How could I solve this issue ? (In fact, in my real package, I even have a third layer of interweaving f3
...)
CompilationOptions -> {"InlineCompiledFunctions" -> True, "InlineExternalDefinitions" -> True}
. Using these options will also avoid the problem you're seeing. $\endgroup$