How do I create a C - shared library with MATLAB Compiler 3.0 which can be used in other projects?

4 views (last 30 days)
I am using MATLAB Compiler 3.0 or earlier and am interested in creating a C-shared library for use in an IDE such as Microsoft Visual Studio 6.
I would like to create a shared library.
I would like to hand initialize code without invoking the main( ) in my MSVC (or other IDE) project.
I would like to generate the C-files that do not contain the main( ) function.
The objective is to convert the MATLAB file to a C file to be used with MSVC (or other IDE) projects.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 28 Jan 2010
Here is how to generate a C-shared library with MATLAB Compiler 3.0 and how to use it in general in an integrated development environment (IDE), using Microsoft Visual Studio 6 as an example.
Here are the steps to create a C - shared library:
1. Compile your MATLAB files into a DLL (on Windows):
mcc -t -L C -W lib:mylib -T link:lib -h <MATLAB files> 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 (i.e. DLL on a PC).
Specifying libmmfile.mlib 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 mylib.dll, mylib.lib, and mylib.h. You can add the
-g switch to produce a DLL suitable for debugging in MSVC (or your own IDE).
2. Add mylib.lib to your MSVC (or your own IDE) project
3. Make sure to call the initialization and termination routines from your
code before calling any of the MATLAB files that are compiled. You need to call:
mylibInitialize();
Afterwards, you should call the termination routine:
mylibTerminate();
All of the symbols in mylib.dll will also appear in mylib.h.
4. You can call the functions compiled from the MATLAB code by invoking mlfFoo(...), from your C code.
For example:
In MATLAB,
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:foolib -T link:lib foo.m libmmfile.mlib
This will generate the following files:
foo.c
foo.h
foolib.c
foolib.h
foolib.exports
foolib.dll
foolib.exp
foolib.lib
foolib.mlib
In Microsoft Visual C/C++ 6.0
1. Start up the Microsoft Visual C/C++ 6.0 IDE
2. Go to FILE and NEW. Click on Projects Tab. Select Win32 Console Application. In the Project Name field type "test". "Create new workspace" should be filled in. In the "Platforms" field, "Win32" should also be checked. Click OK.
3. Select the File View tab and double click on test files. Select Source Files, select Project menu option, select Add to Project and then click on New. In the Files tab select C++ Source file, check Add to project and type "foowrap.c" in the File name field.
4. Enter the following code in the foowrap.c file:
#include "foolib.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 */
foolibInitialize();
/* Call the implementation function */
y_ptr = mlfFoo(x_ptr);
/* Call the library termination function */
foolibTerminate();
/* 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);
mxDestroyArray(x_ptr);
mxDestroyArray(y_ptr);
}
5. Right click on test files and select Add Files to Project. Select:
foolib.lib
and the following files located in $MATLABROOT\extern\lib\win32\microsoft\msvc60:
libmatlb.lib
libmmfile.lib
libmx.lib
where $MATLABROOT is the MATLAB root directory on your machine, as returned by typing
matlabroot
at the MATLAB Command Prompt.
6. Highlight test files and right click. Select Settings. Click on the C/C++ Tab. In the Category listbox select Code Generation. In the Use Runtime library listbox select Multithreaded DLL. Change the Category listbox to Preprocessor. Add to the Preprocessor definitions MSVC, MSWIND, IBMPC so:
WIN32,_DEBUG,_CONSOLE,_MBCS
changes to:
WIN32,_DEBUG,_CONSOLE,_MBCS,MSVC,MSWIND,IBMPC
Add to the Additional include directories field:
$MATLABROOT\extern\include\cpp;$MATLAB\extern\include
Also add to the Additional include directories field the directory in which foolib.h is located.
Click OK
7. Go to Build and Rebuild All.
8. You should now have built test.exe.
9. You may also need to place foolib.dll in the same location as test.exe.
For information on how to call a MATLAB Compiler generated DLL from a Visual C++ environment, see the following Related Solution.

More Answers (0)

Categories

Find more on C Shared Library Integration in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!