MATLAB Answers

Marcell
1

Simulink Coder, add external C++ libraries placed outside the working directory

Asked by Marcell
on 10 Feb 2016
Latest activity Edited by Marcell
on 22 Feb 2016
Following problem: I'm using Simulink Coder, an Arduino Due and a self developed expansion board to control multicopters. I've a Simulink model with all the device drivers (made with s-function builder) to interact with the sensors. Every sensor has an own c++ library which is included in the s-function. All the c++ and header files are currently in the working directory of matlab.
No I have so many device driver blocks, that I made a custom Simulink block library in order to share and use these blocks in multiple Simulink models. However, if I'm using the blocks from my Simulink library the compiler gives an error because it can't find the c++ and header files, as they are not in the current working directory but in the directory of the Simulink library. However the Simulink block library and all the external c++ libraries are included in the Matlab search path (set path).
So does anyone know how to include external c++ libraries when they are called from an s-function in a Simulink block library?
I already tried following steps:
-I tried to include the c++ and header files with following matlab command (doesn't work): set_param('SimulinkModelName','CustomSource',"LibraryName");
-Copy the c++ and header files to the directory of the c++ libraries of the Arduino Simulink support package (doesn't work).
-Copy all the c++ and header files into the directory of the current Simulink model (does work however I don't want to copy all the files every time a new simulink model is created) Does anyone had similar problems so far?

  0 Comments

Sign in to comment.

2 Answers

Answer by Philipp Schaefer on 20 Feb 2016

They include it with a S-function that has no input, and no output. Run renc2cpp('sfcn_include_name') after that. (Read explanation in simulink file for more information)

  0 Comments

Sign in to comment.


Answer by Marcell
on 22 Feb 2016
Edited by Marcell
on 22 Feb 2016

Thank you for your answer. You pointed me in the right direction.
The problem is, that for code generation MATLAB calls the C-compiler, which knows nothing about the MATLAB path. So if the custom libraries are not in the current working path, the compiler can’t find them. After a little research i found following solutions:
Include the custom library with model-wide parameters:
set_param(model, 'CustomSource', [source.cpp’ ‘ source2.cpp’ ‘ ’ ’sourceN.cpp]);
This needs to be done for every Simulink model so its not perfect for sharing it with other users.
The right way to do this is to create a rtwmakecfg.m file and place it into the folder of the library. Inside the rtwmakecfg.m file the path to the custom source and header files can be specified. This will ensure, that any model which uses my s-function blocks will link against the required custom libraries.
Link with the solution:

  0 Comments

Sign in to comment.