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 MainEvaluate
in the compiled function.
As Oleksandr R. suggested, you can also do
g = #^2 &;
f = Compile[{{x, _Real}},
g[x], CompilationTarget -> "C",
CompilationOptions -> {"InlineExternalDefinitions" -> True}]