How can I compile a code with target C containing function definition inside?
I saw post using With. But this just inserts the function in the appropriate place like macros in C.
Suggestion with With
f = Compile[{{x, _Real}},
g[y] = y^2;
g[x]
, CompilationTarget -> "C"
]
Calls Main:
In[12]:= CompilePrint[f]
1 argument
3 Real registers
Underflow checking off
Overflow checking off
Integer overflow checking on
RuntimeAttributes -> {}
R0 = A1
Result = R2
1 V17 = MainEvaluate[ 2
Function[{x}, g[y] = y ][ R0]]
2 R2 = MainEvaluate[ Hold[g][ R0]]
3 Return
gas a separate function instead of making the definition withinCompile[]? – J. M.'s missing motivation Jun 18 '16 at 23:44MainEvaluate? – Al Guy Jun 19 '16 at 03:21cfunc = With[{gg = g}, Compile[{{x, _Real}}, Evaluate[gg[x]]]];.This falls into the macro category as noted above. I think it was the main way to do this sort of thing prior to the introduction ofInlineExternalDefinitions. – Daniel Lichtblau Jun 19 '16 at 16:17