How do I create a C shared library with MATLAB Compiler 4.4 that can be used in a Microsoft Visual Studio 2005 win32 Console Application?

1 view (last 30 days)
I would like to convert MATLAB files into a C shared library that can be called from my Microsoft Visual Studio 2005 project. I would like a complete example that lists the steps necessary to do this.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 18 Jan 2010
This solution explains how to generate a C shared library with the MATLAB Compiler 4.4 (R2006a) and call it from a Microsoft Visual Studio 2005 project.
First, the following steps summarize the process of creating a C shared library and calling it from a C program:
1. In MATLAB, select a compiler with the MBUILD command.
mbuild -setup
2. Compile your MATLAB files into a C shared library with the MCC command.
mcc -B csharedlib:mylib <MATLAB files>
The -B csharedlib option is a bundle option that expands into -W lib:<libname> -T link:lib. The -W lib:<libname> option tells the MATLAB Compiler to generate a function wrapper for a shared library and call it libname. The -T link:lib option specifies the target output as a shared library.
3. Add mylib.lib and the appropriate include directories to your MSVC project. Copy the MATLAB generated DLL files to your project directory.
Note: For versions of MATLAB prior to MATLAB 7.6 (R2008a), the CTF file generated as a part of the compilation process must also be added to the project directory. For MATLAB 7.6 (R2008a) and later releases, if you are using the -C option to disable automatic embedding of the CTF archive, you must include the CTF archive in your project directory.
4. Call the MCR (MATLAB Compiler Runtime) and library initialization and termination routines from your project code.
To initialize the shared library and the MCR, call:
mclInitializeApplication(NULL,0);
mylibInitialize();
After all the calls to your shared library functions, call:
mclTerminateApplication();
mylibTerminate();
5. Build and run your project in MSVC.
The following detailed steps create an example C shared library that is called from a Microsoft Visual Studio 2005 project:
1. Save the following MATLAB code to a file called "foo.m":
function y = foo(x)
y = x*10;
2. Execute the MBUILD command at the MATLAB prompt:
mbuild -setup
The version number of the Microsoft Visual Studio 2005 C/C++ Compiler is 8.0. Select this compiler.
3. Generate a C shared library from foo.m with the MCC command:
mcc -B csharedlib:foolib foo.m
This will generate the following files:
foolib.ctf
foolib.h
foolib.dll
foolib.lib
foolib.exp
foolib_mcc_component_data.c
foolib.c
foolib.exports
Note your current directory where these files are placed, as you will need it later on. You can specify the directory where the files will be placed by using the -d option with MCC. For more information about available options to the MCC command, enter the following command at the MATLAB prompt:
web([docroot,'/toolbox/compiler/f0-985134.html'])
4. In Microsoft Visual Studio 2005, go to File->New->Project. In the Project Types field, select Visual C++ Projects. In the Templates field, select Win32 Console Project. In the Name field type test. Click OK. In the Win32 application Wizard, select Application Settings. Under Additional Options, select Empty project. Click Finish.
5. Click on the Solution Explorer tab. Click Source Files, and from the Project menu, select Add New Item. In the Categories pane select C++ Source file, type "foowrap.c" in the File name field and click Add.
6. Enter the following code in the foowrap.c file:
#include "stdio.h"
#include "foolib.h" /* MATLAB-generated header file */
int main()
{
mxArray *x_ptr;
mxArray *y_ptr=NULL;
double *y;
double ret;
/* Call the MCR and library initialization functions */
if( !mclInitializeApplication(NULL,0) )
{
fprintf(stderr, "Could not initialize the application.\n");
exit(1);
}
if (!foolibInitialize())
{
fprintf(stderr,"Could not initialize the library.\n");
exit(1);
}
/* Create an mxArray to input into mlfFoo */
x_ptr = mxCreateDoubleScalar(1);
/* Call the implementation function */
/* Note the second input argument should be &y_ptr instead of y_ptr. */
mlfFoo(1,&y_ptr,x_ptr);
/* The return value from mlfFoo is an mxArray. Use mxGetpr to get a pointer to data it contains. */
y = mxGetPr(y_ptr);
ret = *y;
/* Display the output of the call to mlfFoo. */
printf("The output of foo is %f\n",ret);
/* Call the library termination function */
foolibTerminate();
mxDestroyArray(x_ptr);
mxDestroyArray(y_ptr);
mclTerminateApplication();
return 0;
}
7. Select your project in the Solution Explorer. From Project menu, select Properties. In the Configuration Properties folder, open the Linker folder and select the General category. In the Additional Library Directories field, add the location of foolib.lib, and the following directory:
$MATLAB\extern\lib\win32\microsoft
Note that $MATLAB should be replaced with the MATLAB installation directory, as returned by typing
matlabroot
at the MATLAB command prompt.
8. Also under the Linker catagory, select the Input category. Enter the following libraries in the Additional Dependencies field:
mclmcrrt.lib
foolib.lib
9. Now select the C/C++ category, and select General. To the Additional Include Directories field, add the location of foolib.lib, and the following directory:
$MATLAB\extern\include
replacing $MATLAB with the MATLAB installation directory as above.
10. Also under the C/C++ category, select Code Generation. In the Runtime Library, select Multi-threaded DLL. Click OK to close the Properties window.
11. Go to Build->Build Solution.
12. You should now have built test.exe.
13. Place foolib.dll and foolib.ctf in the same location as test.exe.

More Answers (0)

Categories

Find more on Package MATLAB Functions in Help Center and File Exchange

Products


Release

R2006a

Community Treasure Hunt

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

Start Hunting!