Skip to main content
2 of 2
added 204 characters in body
vapor
  • 7.9k
  • 2
  • 23
  • 56

First define the function normally:

g[y_] := y^2;

Then substitute the definition in:

f = ReleaseHold[Hold[Compile[{{x, _Real}},
 g[x], CompilationTarget -> "C"]] /. DownValues[g]]

And there is no longer MainEvaluatein the compiled function.

As Oleksandr R. suggested, you can also do

g = #^2 &;
f = Compile[{{x, _Real}},
  g[x], CompilationTarget -> "C", 
  CompilationOptions -> {"InlineExternalDefinitions" -> True}]
vapor
  • 7.9k
  • 2
  • 23
  • 56