MATLAB Compiler | ![]() ![]() |
C Shared Library
The MATLAB Compiler allows you to build a shared library from the files created in the previous section, "C Libraries." To build the shared library, libtimefun.ext
, in one step, use
This example uses the csharedlib
bundle file
The bundle file option, -B <filename>:[<a1>,<a2>,...,<an>]
, replaces the entire expression on the mcc
command line with the contents of the specified file and it allows you to use replacement parameters. This example uses the csharedlib
bundle file and replaces the expression
The -t
option tells the Compiler to generate C code from each of the listed M-files. The -T link:lib
option tells the Compiler to compile and link a shared library. The -h
option tells the Compiler to include any other M-functions called from those listed on the mcc
command line, i.e., helper functions.
mlib Files
Shared libraries, like libraries, let you compile a common set of functions once and then compile other M-functions that depend on them without compiling them again.You accomplish this using mlib
files, which are automatically generated when you generate the shared library.
Creating an mlib File. When you create a library wrapper file, you also get a .mlib
file with the same base name. For example,
The last file, libtimefun.
ext
, is the shared library file for your platform. For example, on the PC, the shared library is
Using an mlib File. This example uses two functions, tic
and toc
, that are in the shared library. Consider a new function, timer
, defined as
Prior to mlib
files, if you compiled timer
using
both tic
and toc
would be recompiled due to the implicit -h
option included in the -m
macro. Using mlib
files, you would use
At compile time, function definitions for tic
and toc
are located in the libtimefun.mlib file, indicating that all future references to tic
and toc
should come from the mlib
files's corresponding shared library. When the executable is created, it is linked against the shared library. For example, on the PC, the executable timer.exe
is created and it is linked against libtimefun.dll
.
An advantage of using mlib
files is that the generated code is smaller because some of the code is now located in the shared library.
Note
On the mcc command line, you can access any mlib file by including the full path to the file. For example: mcc -m timer /pathname/libtimefun.mlib
|
lib
.
mlib
file must be in the same directory at compile time.
mlib
file present when running the executable that links to the shared library.
![]() | C Libraries | C++ Libraries | ![]() |