Build Simulink S-Functions that link to a shared library

12 ビュー (過去 30 日間)
hj82sh
hj82sh 2022 年 8 月 8 日
回答済み: Avadhoot 2024 年 1 月 15 日
Hello,
I want to build Simulink S-Functions that call some common functions from a shared library. On Windows these shared libraries provide a DLL entry point DllMain() and the exported functions are declared with __desclspec(dllexport). Is there something equivalent required on Linux platform?
For now I did no modifications in the code, I just compiled the *.cpp sources for the shared lib with -fPIC and linked the resulting objects with -shared option:
g++ -c -fPIC -I $IncDir *.cpp
g++ -shared -o libmsecommons.so *.o
I extended LD_LIBRARY_PATH accordingly in Matlab, build the S-Functions (*.mexa64) using the Matlab mex-command and pass the shared libs with -L<libfolder> -l<libname>
Everything seems fine until that point and even ldd output for a mex S-function reports supposedly correct information, e.g.:
>> !ldd ASIDEALGAS01MSEREF_sfn.mexa64 | grep airsys
libmseairsys.so => /home/hj82sh/mseref.lib/mse.airsystems.lib/libmseairsys.so (0x00007fd2f937d000)
But when I run a model, Simulink reports a problem with the shared lib:
Error while obtaining sizes from MEX S-function 'ASIDEALGAS01MSEREF_sfn' in 'Airsystem/ASIDEALGAS01MSEREF'.
Invalid MEX-file '/home/hj82sh/mseref.airsystems.simulink/lib/ASIDEALGAS01MSEREF_sfn.mexa64': libmseairsys.so: cannot open shared object file: No such file or directory
Additional info: I do the build inside a Matlab Docker container with no graphics support.
Thanks in advance

回答 (1 件)

Avadhoot
Avadhoot 2024 年 1 月 15 日
Hi,
I understand that you are trying to compile and run a mex S-function with a shared library in Linux. Regarding your first question, on Linux the shared library files are ".so" files and they do not require an entry point function or an export declaration like in Windows.
Now regarding your second question, it looks like the linking process is successful during compile-time as you are getting the correct output for the "ldd" command. But the system cannot find the file during runtime. There can be several causes of this. Below listed are the main causes which could cause this error:
1. Verify Runtime Library Path: Ensure that the LD_LIBRARY_PATH environment variable is correctly set in the environment where Simulink is running, not just in the terminal. You can check this within MATLAB using the getenv function:
getenv('LD_LIBRARY_PATH')
2. Use full library path : Try using the full library path in the MEX command while building the MEX file.
3. Check for dependencies: Check for dependencies inside the " libmseairsys.so" file which are not being resolved.
4. Ensure correct permissions: Check the permissions of the files. Incorrect read/write permissions in Linux can cause this error.
5. Docker configuration: Ensure that the docker container you are using, has access to the directory in which the shared library is located.
6. Add file to MATLAB path: Make sure that the MEX-file is on the MATLAB path or in the current working directory. If MATLAB cannot find the MEX-file itself, it may give a similar error message.
Hope it helps.

カテゴリ

Help Center および File ExchangeSimulink Functions についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by