Click here to Skip to main content
12,254,932 members (57,498 online)
Click here to Skip to main content
Add your own
alternative version


52 bookmarked

MATLAB Shared Library

, 30 Dec 2003
Rate this:
Please Sign up or sign in to vote.
Using MATLAB compiler to build a shared library (DLL) from m-File.

Sample screenshot - Matlab Shared Library


Some times it is required that we build a shared library (DLL) from an m-file. M-files are functions that are written in Matlab editor and can be used from Matlab command prompt. In m-files, we employ Matlab built-in functions or toolbox functions to compute something. In my past articles, I showed you some ways to use Matlab engine (vis. API, C++ class or Matlab engine API) for employing Matlab built-in functions, but what about functions that we develop? How can we use them in VC? Is there any interface? This article shows you an idea to employ your own Matlab functions.

Shared Libraries

Shared libraries or DLLs are files that export some functions. We can use exported functions in any language. Here is a brief instruction to build shared libraries from Matlab m-files:

  1. Compile your m-file into a DLL (from Matlab command prompt):
    mcc -t -L C -W lib:mylib -T link:lib -h <M-file> libmmfile.mlib

    The -t option tells the Matlab compiler to translate the m-file 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 the library with the name specified by "lib:". The -T option tells the compiler what stage should be reached and for what intentions. Here we link our application together to build a shared library (DLL). Specifying libmmfile.mlib tells Matlab compiler to link against Matlab m-file math routine.

    This step will produce mylib.dll, mylib.lib and mylib.h. For debugging purposes, you can add the -g switch to produce a DLL suitable for debugging in MSVC.

    For example, I wrote my own mean function and saved it as MeanFunction.m:

    function y=MeanFunction(x)
    for i=1:n

    and compiled it with mcc:

    mcc -t -L C -W lib:MeanFunctionLib -T link:lib MeanFunction.m libmmfile.mlib
  2. Create your project in VC. In your main CPP file, include your function header file and add the related library. Here I create a simple console application. Make sure to call initialization and termination routines from your code before and after of calling the m-file function.

    #include "stdafx.h"
    #include "matlab.h"
    #include "MeanFunctionLib.h"
    #pragma comment(lib, "libmx.lib")
    #pragma comment(lib, "libmatlb.lib")
    #pragma comment(lib, "libmat.lib")
    #pragma comment(lib, "libmmfile.lib")
    #pragma comment(lib, "MeanFunctionLib.lib")
    int main(int argc, char* argv[])
        mxArray* result;
        mxArray* x;
        double myArray[5]={10.2, 3, 6.3, 5.4, 5.9};
        x=mxCreateDoubleMatrix(1, 5, mxREAL);
        memcpy(mxGetPr(x), myArray, 5 * sizeof(double));
        return 0;
  3. Build your project.

Notice that you must use Matlab C API or Matlab C++ class library to use mxArray or mwArray. For more information, refer to my articles:



This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

A. Riazi
CEO Misbah3Com
Iran (Islamic Republic of) Iran (Islamic Republic of)
I was born in Shiraz, a very beautiful famous city in Iran. I started programming when I was 12 years old with GWBASIC. Since now, I worked with various programming languages from Basic, Foxpro, C/C++, Visual Basic, Pascal to MATLAB and now Visual C++.
I graduated from Iran University of Science & Technology in Communication Eng., and now work as a system programmer for a telecommunication industry.
I wrote several programs and drivers for Synthesizers, Power Amplifiers, GPIB, GPS devices, Radio cards, Data Acqusition cards and so many related devices.
I'm author of several books like Learning C (primary and advanced), Learning Visual Basic, API application for VB, Teach Yourself Object Oriented Programming (OOP) and etc.
I'm winner of January, May, August 2003 and April 2005 best article of month competetion, my articles are:

You can see list of my articles, by clicking here

You may also be interested in...

Comments and Discussions

Questiondo we have something like matlab framework? Pin
adnanmn29-Mar-08 2:14
memberadnanmn29-Mar-08 2:14 
AnswerRe: do we have something like matlab framework? Pin
adnanmn31-Mar-08 0:38
memberadnanmn31-Mar-08 0:38 
Generalmcc of Matlab 7.0 Pin
Junaid Jameel13-Mar-08 4:21
memberJunaid Jameel13-Mar-08 4:21 
GeneralRe: mcc of Matlab 7.0 Pin
harbujel27-Jul-08 23:22
memberharbujel27-Jul-08 23:22 
Generalmatlab .net builder Pin
Paul Stephen10-Feb-08 17:59
memberPaul Stephen10-Feb-08 17:59 
QuestionI have some problem using m files in C++ , can you help me? Pin
Vahid - Rostami17-Dec-07 16:16
memberVahid - Rostami17-Dec-07 16:16 
Questioncan i use matlab inbuild function .... Pin
vishal33421-Jul-07 2:21
membervishal33421-Jul-07 2:21 
AnswerRe: can i use matlab inbuild function .... Pin
ElvedinHamzagic21-Oct-07 3:37
memberElvedinHamzagic21-Oct-07 3:37 
QuestionCalling a dll without header in matlab Pin
ahad4451-Jul-07 21:32
memberahad4451-Jul-07 21:32 
GeneralMatlab dll in Borland C++ Builder 6.0 Pin
Elvedin809-Jun-07 5:59
memberElvedin809-Jun-07 5:59 
GeneralRe: Matlab dll in Borland C++ Builder 6.0 Pin
ElvedinHamzagic11-Jun-07 7:30
memberElvedinHamzagic11-Jun-07 7:30 
GeneralRe: Matlab dll in Borland C++ Builder 6.0 Pin
henriqueamorim8-Oct-07 4:19
memberhenriqueamorim8-Oct-07 4:19 
AnswerRe: Matlab dll in Borland C++ Builder 6.0 Pin
ElvedinHamzagic21-Oct-07 3:19
memberElvedinHamzagic21-Oct-07 3:19 
GeneralRe: Matlab dll in Borland C++ Builder 6.0 Pin
ElvedinHamzagic21-Oct-07 4:05
memberElvedinHamzagic21-Oct-07 4:05 
Generalmcc Compiler error Pin
kumar Shwetaketu21-May-07 3:54
memberkumar Shwetaketu21-May-07 3:54 
QuestionTo access a Matlab code in my C program Pin
Sindhu Krishnana E P10-May-07 21:25
memberSindhu Krishnana E P10-May-07 21:25 
GeneralUsing M-files within C++ code Pin
xxpatanaxx8-May-07 11:39
memberxxpatanaxx8-May-07 11:39 
Generalerror compile Pin
mehdiing5-May-07 7:56
membermehdiing5-May-07 7:56 
GeneralRe: error compile Pin
A. Riazi6-May-07 2:24
memberA. Riazi6-May-07 2:24 
GeneralRe: error compile Pin
kumar Shwetaketu20-May-07 1:57
memberkumar Shwetaketu20-May-07 1:57 
QuestionEAccess Violation with mxCreate* Pin
NowC4-May-07 12:31
memberNowC4-May-07 12:31 
Questioni want to import a C code in matlab what i should do Pin
faizi ji3-May-07 20:00
memberfaizi ji3-May-07 20:00 
AnswerRe: i want to import a C code in matlab what i should do Pin
Sindhu Krishnana E P10-May-07 21:07
memberSindhu Krishnana E P10-May-07 21:07 
QuestionHow ca I do the download of the Pin
giovanitonel24-Nov-06 0:37
membergiovanitonel24-Nov-06 0:37 
AnswerRe: How ca I do the download of the Pin
A. Riazi24-Nov-06 0:45
memberA. Riazi24-Nov-06 0:45 
QuestionHow to access MATLAB functions in C# Pin
Shakeel Mumtaz28-Oct-06 19:05
memberShakeel Mumtaz28-Oct-06 19:05 
AnswerRe: How to access MATLAB functions in C# Pin
Sindhu Krishnana E P10-May-07 3:09
memberSindhu Krishnana E P10-May-07 3:09 
GeneralRe: How to access MATLAB functions in C# Pin
Shakeel Mumtaz11-May-07 7:16
memberShakeel Mumtaz11-May-07 7:16 
QuestionRe: How to access MATLAB functions in C# Pin
kumar dhruva15-Jun-11 23:48
memberkumar dhruva15-Jun-11 23:48 
GeneralMCC: Compiling a Matlab function that gets integer arguments Pin
amitkagian16-Oct-06 6:00
memberamitkagian16-Oct-06 6:00 
QuestionCompatibility between a Matlab dll and Delphi Pin
PititeBato11-Oct-06 22:39
memberPititeBato11-Oct-06 22:39 
AnswerRe: Compatibility between a Matlab dll and Delphi Pin
ElvedinHamzagic21-Oct-07 3:52
memberElvedinHamzagic21-Oct-07 3:52 
QuestionCalling MatLab DLL from VB6 "Can't find DLL entry point..." error Pin
icesktr122-Aug-06 7:53
membericesktr122-Aug-06 7:53 
AnswerRe: Calling MatLab DLL from VB6 "Can't find DLL entry point..." error Pin
Kashif Ishaq13-Sep-07 20:05
memberKashif Ishaq13-Sep-07 20:05 
Generalusing dll c++ through matlab Pin
smurffeddine19-Jun-06 23:53
membersmurffeddine19-Jun-06 23:53 
QuestionI cant merge the DLL with the C++ Pin
schone24-May-06 6:17
memberschone24-May-06 6:17 
GeneralMCC!!!!!???????????????????!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Pin
lamionne18-Mar-06 1:56
memberlamionne18-Mar-06 1:56 
GeneralRe: MCC!!!!!???????????????????!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Pin
kumar Shwetaketu21-May-07 18:58
memberkumar Shwetaketu21-May-07 18:58 
Generalplease help me Pin
lamionne18-Mar-06 1:42
memberlamionne18-Mar-06 1:42 
Generalstudent needsc help in matlab mcc Pin
lamionne15-Mar-06 23:22
memberlamionne15-Mar-06 23:22 
QuestionMat lab .dll and connect it with the C# project?? Pin
Whirl_777-Dec-05 9:28
memberWhirl_777-Dec-05 9:28 
GeneralMATLAB 2 C Pin
Rafid97419-Nov-05 7:50
memberRafid97419-Nov-05 7:50 
General0xC0000005: Access Violation Pin
vadimpl31-Aug-05 18:27
membervadimpl31-Aug-05 18:27 
GeneralRe: 0xC0000005: Access Violation Pin
jhwurmbach23-Nov-05 6:34
memberjhwurmbach23-Nov-05 6:34 
GeneralRe: 0xC0000005: Access Violation Pin
Bonta Marian S23-Jun-07 22:11
memberBonta Marian S23-Jun-07 22:11 
GeneralRe: 0xC0000005: Access Violation [modified] Pin
jhwurmbach1-Jul-07 22:18
memberjhwurmbach1-Jul-07 22:18 
AnswerRe: 0xC0000005: Access Violation Pin
Bonta Marian S4-Jul-07 6:55
memberBonta Marian S4-Jul-07 6:55 
GeneralNew Library Pin
Anonymous27-Jun-05 9:51
sussAnonymous27-Jun-05 9:51 
GeneralUsing COMObject from Matlab to C# Pin
Anonymous30-Apr-05 11:00
sussAnonymous30-Apr-05 11:00 
GeneralRe: Using COMObject from Matlab to C# Pin
Anonymous4-Aug-05 6:16
sussAnonymous4-Aug-05 6:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160426.1 | Last Updated 31 Dec 2003
Article Copyright 2003 by A. Riazi
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid