Before you begin, make sure that a supported compiler is installed. It will be Visual C ++ and possibly the Windows SDK if you are using the VS Express version on 64-bit Windows. Then you need to configure MATLAB by following these steps at least once:
>> mex -setup >> mbuild -setup
Now the following simple function is given:
Myadd.m
function c = MyAdd(a,b) c = a + b; end
We want to build a common C ++ library using the MATLAB mcc compiler:
>> mcc -N -W cpplib:libMyAdd -T link:lib MyAdd.m -v
This will create a couple of files, including a header file, a DLL, and an import library:
libMyAdd.h libMyAdd.dll libMyAdd.lib
Next, create a C ++ program to test the above library:
Myadd_test.cpp
#include "libMyAdd.h" int main() { // initialize MCR and lib if (!mclInitializeApplication(NULL,0)) { std::cerr << "could not initialize the application" << std::endl; return -1; } if(!libMyAddInitialize()) { std::cerr << "Could not initialize the library" << std::endl; return -1; } try { // create input double a[] = {1.0, 2.0, 3.0, 4.0}; double b[] = {5.0, 6.0, 7.0, 8.0}; mwArray in1(2, 2, mxDOUBLE_CLASS, mxREAL); mwArray in2(2, 2, mxDOUBLE_CLASS, mxREAL); in1.SetData(a, 4); in2.SetData(b, 4); // call function mwArray out; MyAdd(1, out, in1, in2); // show result std::cout << "in1 + in2 = " << std::endl; std::cout << out << std::endl; double c[4]; out.GetData(c, 4); for(int i=0; i<4; i++) { std::cout << c[i] << " " << std::endl; } } catch (const mwException& e) { std::cerr << e.what() << std::endl; return -2; } catch (...) { std::cerr << "Unexpected error thrown" << std::endl; return -3; } // cleanup libMyAddTerminate(); mclTerminateApplication(); return 0; }
We could compile this program directly from inside MATLAB using:
>> mbuild MyAdd_test.cpp libMyAdd.lib -v >> !MyAdd_test
We could also compile it ourselves using Visual Studio. We start by creating our own console application, and then set the project parameters as follows:
- From the menu, select "Project> Properties" and apply the settings to "All configurations" (both for debugging and release purposes)
In the C / C ++ properties, set “Additional Include Directories” by adding to the directory the directory containing the generated header file libMyAdd.h , in addition to the directory containing the MATLAB header files:
$matlabroot\extern\include
Similarly, in the "Linker" section, install "Additional library directories." This will be the same directory as before containing libMyAdd.lib , and also in my case:
$matlabroot\extern\lib\win32\microsoft
Then go to "Linker> Input" and add the following inside the "Additional Dependencies":
libMyAdd.lib mclmcrrt.lib
Finally, in the Debug> Environment section, you can expand the PATH environment variable to include the directory containing the generated libMyAdd.dll file. This way you can directly press F5 to compile running the program directly from inside VS. It will be something like this:
PATH=%PATH%;C:\path\to\output\folder
If you do this often, you can create a property sheet once, which can then be reused in other VC ++ projects. See this answer for an example.