How can I implement coder.ceval in Simulink?

1 view (last 30 days)
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. "

Accepted Answer

Kaustubha Govind
Kaustubha Govind on 26 Oct 2012
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.
  2 Comments
Craig Barry
Craig Barry on 26 Oct 2012
This was very helpful. I have now succesfully generated code from within a Simulink model.
However, the code I want to generate actually sits inside a simulink library, (rather than a model). It appears impossible to include the required headers specified in the custom code pane when generating for a library, can anyone help?
Kaustubha Govind
Kaustubha Govind on 29 Oct 2012
I'm not sure if there is a standard/recommended solution in case of library blocks (please confirm with MathWorks Tech Support if you don't see a response here), but you could use the library block's CopyFcn callback to add to the new model's Configuration Parameters settings - although not an elegant solution, it should do the trick.
Or perhaps you can create a subsystem containing your MATLAB Function block, a Model Source block and a Model Header and add the subsystem to your library, so that everywhere that the MATLAB Function block is used, the Model Source and Header files are added. However, this will only use the build rules for code-generation and not for the simulation target.

Sign in to comment.

More Answers (1)

Ryan G
Ryan G on 26 Oct 2012
From the error and based on the line you use to generate the code, it looks like it can't find the libraries you utilized, math.c and hilton_math.h. Hence, it does not know where this function belongs.
I would have to look a little deeper to find out how to include the h-files, but don't have the time right this moment, I will edit this answer if I figure it out.

Categories

Find more on Simulink Coder in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!