Error using Custom C Code Simulation Library with Simulink and Stateflow
12 views (last 30 days)
Show older comments
I am using Matlab Stateflow to model specific processes and these processes are suppose to interact with an existing C++ library. To allow Matlab Stateflow to interact with the C++ library, I wrote a simple C interface for the library and imported the header file and the library in the Simulation Target options. Using this simple process, I was able to load the custom library in Stateflow under MacOS X and have it interact as exepected with the library.
Now I wanted to replicate this under linux, but have unfortunately had a lot of trouble and ultimately failed to get Stateflow to load the library successfully. I then created a new, proof-of-concept C library for debugging the issue only containing a single test function and was also unable to get Stateflow to load this shared library.
The error thrown by Stateflow every time:
Failed to load custom code simulation library:/home/davidk/Desktop/slprj/_slcc/qxnWz5tNfQfFLosY9yYvpC/qxnWz5tNfQfFLosY9yYvpC_cclib.so
This is the code for the proof-of-concept C library, which Stateflow also refused to load.
Header File - test.h
#ifndef TEST_H
#define TEST_H
extern void test();
#endif
C File - test.c
#include "test.h"
#include <stdio.h>
void test(){
printf("test");
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.13)
project(test C)
set(CMAKE_BUILD_TYPE Debug)
file(GLOB SRC "*.c")
add_library(wrapper SHARED ${SRC})
add_executable(main "main.c")
target_link_libraries(main wrapper)
The executable in the CMakelists is not important, it was just a test to see, if i could successfully use the library for linking.
The folder structure is simply:
build
test.c
test.h
CMakeLists.txt
The code listed above compiles just fine and I can also run the executable without any issues.
I am using the following simulation target settings to load the library:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/289034/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/289035/image.png)
And this is how the function is being called from Stateflow.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/289036/image.png)
More information about my setup:
I compiled and installed GCC 6.5 from source and verified that it is being used for the compilation.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/289037/image.png)
I also know that the latest supported compiler version was GCC 6.3, however after a couple of tries of installing v6.3 I could not get it to work. I applied a patch detailed here: Stackoverflow, which solved the first issue and then had to apply this "Hack" (even called a Hack by the author) Github. Which also did not fix the issues I was having installing GCC 6.3.
I am also using Matlab Version R2019a but can also try a newer Version should that have a chance of helping.
This leads me to two questions:
Firstly, is it truly required to use GCC 6.3 and if that is the case, how am I expected to install this thing ?
Secondly, how do I debug Stateflows unwillingness to load my library ? In my tests, the generated library appears to be valid and I do not know how I would proceed to debug this issue ?
Thank you, David
1 Comment
Martijn
on 25 Aug 2020
This is not Stateflow's unwillingness to load the library; the behavior which you see is standard dynamic loader behavior on Linux. The directory containing this libwrapper.so will have to be on your LD_LIBRARY_PATH for the dynamic loader to be able to load it.
Answers (0)
See Also
Categories
Find more on Introduction to Installation and Licensing 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!