How do I create a C shared library using MATLAB Compiler 6.5 (R13) and use it with the C application?

19 Ansichten (letzte 30 Tage)
I would like to build a shared library from my MATLAB code and use that with my larger application under UNIX.

Akzeptierte Antwort

MathWorks Support Team
MathWorks Support Team am 5 Okt. 2018
Bearbeitet: MathWorks Support Team am 5 Okt. 2018
The following steps create a C shared library on UNIX:
1. Compile your MATLAB files into a shared library (on UNIX)
mcc -t -L C -W lib:libFile -T link:lib -h <matlab-file> libmmfile.mlib
The -t option tells the MATLAB Compiler to translate the MATLAB code to the target language.
The -L option specifies the target language, which is chosen to be C.
The -W option tells the MATLAB Compiler to build a wrapper for libraries with the name specified after "lib:".
The -T option tellls the compiler what stage should be reached and for what intentions. Here we want to link our application together to build a shared library.
Specifying the libmmfile.mlib file tells the MATLAB Compiler to link against the MATLAB file Math routines that have already been precompiled into shared libraries when necessary.
This step will produce libFile.so, libFile.exports, libFile.h and libFile.mlib. You can add the
-g switch to produce a shared library suitable for debugging.
2. Make sure to call the initialization and termination routines from your code before calling any of the MATLAB Files that are compiled. The initialization routine for the libFile.so file would be:
libFileInitialize();
And the termination routine would be:
libFileTerminate();
All the routines (symbols) defined in 'libFile.so' will be in the libFile.export file and the prototypes for these routines will be libFile.h.
3. You can call the functions compiled from the MATLAB code by invoking mlf<MATLAB-file>(...), from your own C code. Where MATLAB-file represents the name of the original MATLAB-file with the first letter in upper case.
4. Compile your program and link against the MATLAB C Math Libraries as well as the libFile library created above.
Here is an example that illustrates the above steps.
Inside the MATLAB environment:
1. Write a MATLAB file function named foo and saved as foo.m that contains the following code:
function y = foo(x)
y = x+1;
2. Use the MATLAB Compiler to compile foo.m into C code that can be included in a C shared library:
mcc -t -L C -W lib:libFoo -T link:lib foo.m libmmfile.mlib
This will generate the following files:
foo.c
foo.h
libFoo.c
libFoo.exports
libFoo.h
libFoo.mlib
libFoo.so
On certain UNIX platforms, the generated shared library file extension would be ".sl".
External to MATLAB:
1. Create the file called foowrap.c that calls the MATLAB Compiler generated shared library:
/*
* foowrap.c
* Calls a MATLAB Compiler generated shared library.
*
*/
#include "libFoo.h"
#include "matlab.h"
#include <stdio.h>
void main(void)
{
mxArray *x_ptr;
mxArray *y_ptr;
double *y;
double ret;
/* Create an mxArray to input into mlfFoo */
x_ptr = mxCreateDoubleScalar(1);
/* Call the library initialization function */
libFooInitialize();
/* Call the implementation function */
y_ptr = mlfFoo(x_ptr);
/* Call the library termination function */
libFooTerminate();
/* The return value from mlfFoo is an mxArray so we must extract the data from it */
y = mxGetPr(y_ptr);
ret = *y;
/* Print a double precision number*/
printf("The output of foo is %f\n",ret);
}
2. Compile your application using one of the following methods:
With MBUILD (inside MATLAB):
mbuild foowrap.c -L./ -I./ -lFoo
In the above command, it is assumed that you are running the MBUILD command from the same directory in which you created the shared library. If this is not the case, then the -L and -I options should be followed by the locations of the shared library and the include files respectively.
Without MBUILD (outside MATLAB):
Building outside of MATLAB requires that you specify the locations of the MATLAB header files, the locations of the MATLAB shared libraries and the names of the MATLAB Libraries explicitly. With MBUILD this was not necessary because MBUILD finds and uses these files automatically. Actually, a good way to find these libraries would be to run MBUILD with the "-v" flag and use the compile and linker commands from the verbose output. If you do not have the possibility to execute the MBUILD command in MATLAB in verbose mode, please note that the required libraries to build your application outside MATLAB can be found in the following folders, in which "<mcr_root>" is the folder in which MATLAB Compiler Runtime (MCR) is installed on your computer:
<mcr_root>/<mcr_version>/extern/lib/glnx86 (or glnxa64)
<mcr_root>/<mcr_version>/bin/glnx86 (or glnxa64)
<mcr_root>/<mcr_version>/extern/include
The "<mcr_version>" listed above is the version of the installed MCR. For instance, the MCR version related to MATLAB R2014b is 8.4 and therefore:
<mcr_version> --> v84
Please refer to the following link for a complete list of MCR versions:
Below is the command, if you were building using GCC outside MATLAB:
gcc -o foowrap foowrap.c -L/<mcr_root>/<mcr_version>/extern/lib/glnx86
-L/<mcr_root>/<mcr_version>/bin/glnx86 -L./ -I/<mcr_root>/<mcr_version>/extern/include
-I./ -lmmfile -lmatlb -lmx -lut -lm -lFoo
3. Set the search paths for the dynamic loader to find the shared library. The exact command depends on the exact UNIX operating system and the shell you are running. The following command assumes you are running in a csh shell.
setenv LD_LIBRARY_PATH <matlabroot>/bin/<arch>:<matlabroot>/extern/bin/
<arch>:<matlabroot>/sys/os/<arch>/
If the LD_LIBRARY_PATH was previously defined, the command would be:
setenv LD_LIBRARY_PATH <matlabroot>/bin/<arch>:<matlabroot>/extern/bin/
<arch>:<matlabroot>/sys/os/<arch>/:$LD_LIBRARY_PATH
4. Run the executable file, "foowrap" that was created:
[unix]%> foowrap
The output of foo is 2.000000

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB Compiler finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by