CCodeStringGenerate on functions defined in notebook

Hello,

we are currently evaluating Mathematica as an platform for implementation of our calculations. Thus I am interested in the compilation feature for C.

I have a notebook which contains functions defined in the usual way:

In[210]:= func1[a_, b_] := a*b

In[211]:= func1[2, 3]

Out[211]= 6

I would like to compile this function into C code:

In[212]:= << CCodeGenerator`

In[213]:= cFun = Compile[{a, b}, func1];
CCodeStringGenerate[cFun, “bla”]

During evaluation of In[213]:= CCodeGenerate::wmreq: The expression Function[{a,b},func1] requires Mathematica to be evaluated. The function will be generated but can be expected to fail with a nonzero error code when executed. >>

if I put a*b directly into the Compile[] statement it works.
Why is that? is there a way around that or do I have to repeat all the functions defined in the notebook to get them into C?

Kind regards,
Mathias

Compile has attributes HoldAll. Try Compile[{a,b}, Evaluate @ func1[a,b]]. That will give you a function compiled to the Wolfram Virtual Machine. You can then use CCodeGenerate on that.

That seems to work. Thank You!