필터 지우기
필터 지우기

How can I implement coder.ceval in Simulink?

조회 수: 6 (최근 30일)
Craig Barry
Craig Barry 2012년 10월 25일
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. "

채택된 답변

Kaustubha Govind
Kaustubha Govind 2012년 10월 26일
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
Craig Barry
Craig Barry 2012년 10월 26일
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 2012년 10월 29일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Ryan G
Ryan G 2012년 10월 26일
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.

카테고리

Help CenterFile Exchange에서 Simulink Coder에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by