How to link a dynamic library in a C/C++ S-Function that also Matlab needs

조회 수: 10(최근 30일)
In a C/C++ S-Function I need functions from a 3rd party dynamic link library. For example I need the function QApplication::aboutQt() from the Qt5Widgets library (Qt5Widgets.dll or libQt5Widgets.so). Matlab/Simulink does also dynamically link to the Qt5Widgets library while delivering that library in e.g. the path matlabroot\bin\win64 or matlabroot/bin/glnxa64. Matlab 2015b and 2016a for example are using Qt version 5.3.0 while I need to use Qt version 5.5.0. Is it possible to use the other Qt libraries?
For example following following S-Function
#define S_FUNCTION_LEVEL 2
#define S_FUNCTION_NAME sfunc_loadlibtest
#include <simstruc.h>
#include <QApplication>
static void mdlInitializeSizes(SimStruct *S)
{
int argc = 1;
char* argv = "test";
QApplication app(argc, &argv);
QApplication::aboutQt();
ssSetNumSFcnParams(S, 0);
}
static void mdlInitializeSampleTimes(SimStruct *S)
{
}
static void mdlOutputs(SimStruct *S, int_T tid)
{
UNUSED_ARG(tid);
UNUSED_ARG(S);
}
static void mdlTerminate(SimStruct *S)
{
UNUSED_ARG(S);
}
// Required S-function trailer
#ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
#include "simulink.c" /* MEX-file interface mechanism */
#else
#include "cg_sfun.h" /* Code generation registration function */
#endif
Compiled with:
if isunix
mex('./src/sfunc_loadlibtest.cpp',...
'-I/opt/Qt/5.7/gcc_64/include/',...
'-I/opt/Qt/5.7/gcc_64/include/QtCore',...
'-I/opt/Qt/5.7/gcc_64/include/QtWidgets',...
'-L/opt/Qt/5.7/gcc_64/lib/',...
'-lQt5Core','-lQt5Widgets');
else
mex('./src/sfunc_loadlibtest.cpp',...
'-IC:\Qt\5.5\msvc2013_64\include',...
'-IC:\Qt\5.5\msvc2013_64\include\QtCore',...
'-IC:\Qt\5.5\msvc2013_64\include\QtWidgets',...
'-LC:\Qt\5.5\msvc2013_64\lib',...
'-lQt5Core','-lQt5Widgets');
end
And run with: sfunc_loadlibtest(0,0,0,0);
Note: the aboutQt() function is only an example. The same problem accures with e.g. the openscenegraph libraries, boost libraries or opencv and many more

답변(1개)

Walter Roberson
Walter Roberson 2016년 11월 15일
Typically it will not work to dynamically link two routines that require different versions of a library.
Sometimes it will work. If an interface has been constructed to be backwards compatible then you might be able to use the newer version, possibly. You are safest using versions where the first two components are the same; for example, 5.3.0 would probably be compatible with 5.3.1 . Whether 5.3 would work with 5.4 or 5.5 is going to depend upon how the authors make use of numbering; for some software authors, incompatibilities are signaled by changes in the first component (so, 5.9 would work with 5.3 but 6.x would not); for others, incompatibilities are signaled by changes in the second component (so 5.3 would not work with 5.4.) And sometimes the numbering is just magical; for example, 5.1 might not work with 5.2 but 5.2 might work with 5.3 or 5.4 but not 5.5, and you cannot always tell from the numbering what is compatible and what is not.
  댓글 수: 2
Walter Roberson
Walter Roberson 2016년 11월 15일
DLL with the same basic name never re-link of they are already linked to a process.
A problem like this is sometimes solved by linking one part of the image with static linking instead of dynamic linking -- but that requires that you have control over the linking, which you do not have.

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

Community Treasure Hunt

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

Start Hunting!

Translated by