As a beginner in MATLAB, I recently looked at how to implement mixed programming between MATLAB and VC, checked a lot of information, and learned a lot of valuable experience and suggestions from the Internet, now I will write down this process of collective wisdom for your reference, hoping to have more exchanges with experts.
Software Version: MATLAB 7.0 VC 6.0
1. Set the matlab c/C ++ Compiler
> Mbuild-setup
Please choose your compiler for building standalone MATLAB applications:
Wocould you like mbuild to locate installed compilers [y]/n?
Select a compiler:
[1] Lcc C version 2.4 in D:/MATLAB 7/sys/KP
[2] Microsoft Visual C/C ++ version 6.0 in C:/Program Files/Microsoft Visual Studio
[0] None
Compiler: 2
Please verify your choices:
Compiler: Microsoft Visual C/C +++ 6.0
Location: C:/Program Files/Microsoft Visual Studio
Are these correct? ([Y]/n): y
Try to update options file: C:/Users/peng/Application Data/MathWorks/MATLAB/R14/compopts. bat
From template:
D:/maid/BIN/WIN32/mbuildopts/msvc60compp. bat
Done...
--> "D:/maid/bin/win32/mwregsvr D:/maid/bin/win32/mwcomutil. dll"
DllRegisterServer in D:/MATLAB 7/bin/win32/mwcomutil. dll succeeded
--> "D:/maid/bin/win32/mwregsvr D:/maid/bin/win32/mwcommgr. dll"
DllRegisterServer in D:/MATLAB 7/bin/win32/mwcommgr. dll succeeded
[Note]: I didn't set it successfully because my VC was installed on the d disk. I re-installed the VC again and put it on the C disk.
2. Compile the *. m file of MATLAB
For example:
Function y = E1 (x)
Y = x. * X-2 * x + x. * x. * x;
Plot (x, y );
3. Compile the *. M file into a DLL that can be called by VC.
> MCC-W libhg: <name of the library file defined by myself, such as e1dll>-T link: Lib E1 (name of M file). m
After the preceding command is complete, a series of files are generated, which are useful: e1dll. lib, e1dll. h e1dll. DLL, e1dll. CRF, copy the above files to the current VC working directory.
4. Set the VC Environment
In tool-> options-> directories: Add <MATLAB>/extern/include/CPP to include files;
Add <MATLAB>/extern/lib/Win32/Microsoft/msvc60 to library files.
In Project-> setting-> links: Add mclmcrrt. lib to object/library modules and the Lib file in this project: e1dll. Lib.
5. Call DLL in VC
Several key functions:
// Initialization:
Res = e1dllinitialize ();
If (! Res)
{
MessageBox ("Incorrect Lib initialization! ");
}
// Destructor
E1DLLTerminate ();
// Call computing
Double x = 0.4; // defines a variable.
MxArray * t; // defines the Input Function
MxArray * y; // defines the output function.
Y = NULL;
T = mxCreateDoubleMatrix (1, 1, mxREAL );
Memcpy (mxGetPr (t), & x, sizeof (double ));
MlfE1 (1, & y, t );
// Calculate the core function. "1" indicates that the function has output. In the middle of E1DLL. h, it is an automatically generated interface.
MclWaitForFiguresToDie (NULL); // wait until the graphic window is closed
MxDestroyArray (t );
MxDestroyArray (y );
[Note]: Do not forget # include "E1DLL. h"
The above is my main process.