MATLAB: How to implement coder.ceval in Simulink

coder.ceval simulink matlab functionEmbedded CoderMATLABsimulink

I have written a function using the editor:
function y = barry_fcn(u) %#codegen
(assert(isa(u,'single')))
if isempty(coder.target)
y = cos(u)
else
y = single(0)
y = coder.ceval('math_cosf',u)
end
end
I can successfully build code using this command:
codegen -config:lib barry_fcn math.c hilton_math.h
However, I wish to be able to generate this code from a simulink model using a MATLAB function block inside this model with the same code, if I try this I end up with the following error:
"c2_eval_cos_code.c(199) : warning C4013: 'math_cosf' undefined; assuming extern returning int
c2_eval_cos_code.c(199) : warning C4244: '=' : conversion from 'int' to 'real32_T', possible loss of data
### Linking ...
link.exe /nologo /dll /OPT:NOREF /export:mexFunction /OUT:eval_cos_code_sfun.mexw64 /map:"eval_cos_code_sfun.map" "C:\Program Files\MATLAB\R2012b\stateflow\c\mex\lib\win64\sfc_mex.lib" "C:\Program Files\MATLAB\R2012b\stateflow\c\debugger\lib\win64\sfc_debug.lib" "C:\Program Files\MATLAB\R2012b\extern\lib\win64\microsoft\libmx.lib" "C:\Program Files\MATLAB\R2012b\extern\lib\win64\microsoft\libmex.lib" "C:\Program Files\MATLAB\R2012b\extern\lib\win64\microsoft\libmat.lib" "C:\Program Files\MATLAB\R2012b\extern\lib\win64\microsoft\libfixedpoint.lib" "C:\Program Files\MATLAB\R2012b\extern\lib\win64\microsoft\libut.lib" "C:\Program Files\MATLAB\R2012b\extern\lib\win64\microsoft\libmwmathutil.lib" "C:\Program Files\MATLAB\R2012b\extern\lib\win64\microsoft\libemlrt.lib" "C:\Program Files\MATLAB\R2012b\lib\win64\libippmwipt.lib" "C:\Program Files\MATLAB\R2012b\extern\lib\win64\microsoft\libmwblascompat32.lib" @eval_cos_code_sfun.mol
Creating library eval_cos_code_sfun.lib and object eval_cos_code_sfun.exp
c2_eval_cos_code.obj : error LNK2019: unresolved external symbol math_cosf referenced in function sf_c2_eval_cos_code
eval_cos_code_sfun.mexw64 : fatal error LNK1120: 1 unresolved externals
NMAKE : fatal error U1077: '"c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\amd64\link.exe"' : return code '0x460'
Stop. "

Best Answer

You need to add the header and source files to the Simulation Target > Custom Code pane of the Configuration Parameters window, and also remember to select the Use the same custom code settings as Simulation Target checkbox on the Code Generation > Custom Code pane.
Related Question