Home > Software engineering >  Export c code from simulink scheme with s-function
Export c code from simulink scheme with s-function

Time:02-09

I have a simulink working simulink schema in which there are some s-function, I want to export this code in c and compile it. I already tried to export and compile a simple schema and it works, but when i try to compile the exported code of my project I get a lot of errors like

error: #error Unrecognized use   
error: #error Must define one of RT,nNRT, MATLAB_MEX_FILE, SL_INTERNAL, or FIPXT_SHARED_MODULE     
error: #error Unhandled case  
error: ‘mxArray’ does not name a type  mxArray       **dlgParams; /* The S-function parameters   
error: ‘mxArray’ has not been declared  mxArray **) 
error: ‘mxArray’ does  not name a type mxArray     *propVal;  
error: ‘RTWSfcnInfo’ does not name a type; did you mean ‘RTWLogInfo’? RTWSfcnInfo sfcnInfo

The main settings that i used are:

Code generation system target file --> grt.tlc
language --> C  
Pack code and artifact --> <name_zip>
Tool chain GNU gcc/g   | gmake (64-bit Linux)
default parameter behavior --> tuanble
code interface packaging --> nonreusable function
external mode --> chcked
standar math library --> c   03(ISO)
single output/update function --> checked
i used a fixed step size solver

Im using Ubuntu 18.

The source is generated succesfully the problem is when i try to compile the code. Once the code is generate I get a zip folder with all the sources and also some external header file that I include when I make the CMakeLists.txt. I also tryied to compile directly the code with the .mk file but also I get a bounch of errors. The problem is realated with the s-function because I built a simple simulink schema with a source an s-function that implements a dynamical model and an output and when i generate the code and compile it I get the same errors. I also tryied to make a simple schema without s-function and this works. So is there any settings for the s-function to be exportable

CodePudding user response:

At the end I found an answer that I post here in case someone else has my same problem.

The solution that worked form my is:

First of all once you have the generated zip file with all the sources and headers open it and load on the Matlab workspace the file buldinfo.mat then in the Matlab shell run

packNGo(buildInfo)

in this way you generate a new .zip with all the sources and headers, this .zip differs from the previous because is flat, no nested folder.

Inside this folder there is a file called define.txt with a list of variables, all these variables should be passed as flags to the compiler. For example in my case I use a CMakelists.txt so to set these flags I used

set(CMAKE_CXX_FLAGS "-Wall -Wextra -DMODEL=test_sum_gain -DNUMST=2 -DNCSTATES=2 -DRT -DUSE_RTMODEL -DON_TARGET_WAIT_FOR_START=0 -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DTID01EQ=1 -DEXT_MODE=1 -DMAT_FILE=0 -DONESTEPFCN=1 -DTERMFCN=1 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0")

this depends on the way in which you compile the source, but in any case you should set these variables that is I am not wrong are just a directive for the precompiler.

These two things worked for me.

  •  Tags:  
  • Related