
5 Include compiled MATLAB functions to C-code
5.1 Preliminary Notes
C Code only
Generally it is possible to generate C code as well as C++ code from existing
MATLAB files. But there are still some problems in compiling functions that need
the C/C++ Graphics Library (e.g. [9, 11, 12, 13]). Although there exist some
workarounds, there are still some unresolved problems remaining. So we can only
generate C code from our MATLAB functions.
Naming Convention
All MATLAB functions translated to C code follow the same naming conventions.
The name of the C functions start with the prefix mlf followed by original function
name, whereby the first letter is capitalized. For example the translated C function
of the well known function svd (singular value decomposition) is called mlfSvd.
Include Libraries
The simplest way to include MATLAB functions into a C environment is to link
them together into a shared library. The result of the linking process are a shared
library (libname.dll/Windows, libname.so/Linux) and a header file libname.h. To
enable the execution of the library’s source code the library must be initialized.
Therefore the the MATLAB Compiler generated the functions libnameInitialize()
and libnameTerminate(), which can be compared to constructor and destructor
speaking in terms of C++ programming. For an example see: Listing 2.
If no libraries were generated you can link the compiled object files to your ex-
ecutable as well. If so you have to add a name of functionInitialize() and
name of functionTerminate() command for every compiled function you call to
your C main file.
Linking Library Bug
Due a bug in the mcc it is not possible to link more than one mcc generated library to
a C program directly on a Linux system. The variables lib info and reference count
are defined extern for each library. Therefore initializing a second library overrides
the settings of the first one. This causes a segmentation fault at run-time. As a
workaround for this problem, these extern defined variables must be renamed to a
unique name, e.g. by running PERL script within a Makefile.
Generating Unsupported Libraries
Nevertheless linking of libraries against the C/C++ Graphics Library is not offi-
cially supported by Mathworks [9, 12], we were able to create such libraries and link
them to pure C functions and got stable executables.
5.2 Example
The following simple example shows, how to integrate a library generated from MATLAB
functions into a C main-file.
13
Kommentare zu diesen Handbüchern