转载 2006年05月30日 00:36:00

Technical Solutions

Solution Number: 1-18CBI

Date Last Modified: 18 Oct 2004
Product:   MATLAB® Compiler
Reported in Release:   R12
Fixed in Release:    
Platform:   Windows
Operating System:   Windows Any



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

Problem Description

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.

How do I create a shared library?

How do I hand initialize code without invoking the main( ) in my MSVC (or other IDE) project?
How do I generate the C-files which do not contain the main( ) function?

The objective is to convert the M-file to a C file to be used with MSVC (or other IDE) projects.


This solution explains 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 M-files into a DLL (on Windows):

mcc -t -L C -W lib:mylib -T link:lib -h libmmfile.mlib

The -t option tells the MATLAB Compiler to translate the M 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 M-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 M-Files that are compiled. You need to call:


Afterwards, you should call the termination routine:


All of the symbols in mylib.dll will also appear in mylib.h.

4. You can call the functions compiled from the M-code by invoking mlfFoo(...), from your C code.

For example:


1. Write a M-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:


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"

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 */

/* Call the implementation function */
y_ptr = mlfFoo(x_ptr);

/* Call the library termination function */

/* 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);


5. Right click on test files and select Add Files to Project. Select:


and the following files located in $MATLAB/extern/lib/win32/microsoft/msvc60:


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:


changes to:


Add to the Additional include directories field:


where $MATLAB is the MATLAB installation directory such as d:/matlabR12.

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:


最近在Matlab中写了一个算法,需要封装成dll供VC++调用,按现有帖子的方法没有成功(也许和我的系统环境不同),但经过一番折腾终于搞定,特重写了帖子,与大家交流。 参考资料: htt...
  • willzhang1982
  • willzhang1982
  • 2014年03月13日 16:04
  • 1001


方法1 mcc 生成cpp文件方式 1、设置项目编译选项 首先建立一个新的项目,或者打开一个已有的项目,然后选择菜单: Project-> Settings-> C/C++ C...
  • rayna00
  • rayna00
  • 2016年07月17日 19:38
  • 1903


转载地址:调用matlab R2006版中定义的.m文件中的函数的实例此版本与6.0版不大一样,与7.0版...
  • u010177286
  • u010177286
  • 2015年04月21日 17:23
  • 1555


第1步: 打开Matlab2013a,新建add.m文件function data = add(a,b,count) % a = 1; % b = 2; % count = 1000000000; f...
  • u011475134
  • u011475134
  • 2017年06月03日 14:51
  • 1026


MATLAB Coder可以从MATLAB代码生成独立的、可读性强、可移植的C/C++代码。使用MATLAB Coder产生代码的3个步骤: 1. 准备用于产生代码的MATLAB算法; 2. 检查...
  • u011475134
  • u011475134
  • 2017年06月03日 15:24
  • 1443

MATLAB 函数导出c/c++代码被vs2010调用

MATLAB 函数导出c/c++代码被vs2010调用 在用形态学的时候,发现Matlab中的函数 bwmorph()很好用,比如 “thicken” "thin" ,源码还没看懂,想要通过 Matl...
  • yiqiudream
  • yiqiudream
  • 2016年12月14日 17:14
  • 951


VC与Matlab混合编程之调用动态链接库dll—— 1、是先建立 matlab 的 m 文件。 ellipsefit.m 其包含:function [Xc,Yc,A,B,Phi,P]=e...
  • u010177286
  • u010177286
  • 2015年04月27日 20:29
  • 1052


转自: 本文主要介绍VC与Matlab混合编程的两种方法,并详细介绍了VC向Matlab传递复杂数据:结构体的方法,有详细代码说明。 Matlab 主要面对科学计算、可视化...
  • top_along
  • top_along
  • 2014年11月04日 17:15
  • 1326


使用VC6.0与VS2008,在配置上面都是类似的,唯一的区别在与用蓝色标出的部分 一. MATLAB Compiler 配置MATLAB编译器,主要是在MATLAB的命令窗口先后输入两条...
  • u013018721
  • u013018721
  • 2014年08月04日 09:25
  • 653


转载声明: VC++与Matlab混合编程之引擎操作详解 (1) (2) ht...
  • feichizhongwu888
  • feichizhongwu888
  • 2015年08月04日 15:29
  • 2824