title: Calling MATLAB from C++ errors: unresolved external symbol
body: <p>I encounter several errors when calling my MATLAB function from C++. The main idea is: firstly compile a MATLAB function and generate DLL file, and then include .h and .lib files in C++. Finally, write .cpp to test and call the function. Here's my detailed steps and please tell me where I'm wrong.</p>
  
  <p>(Using MATLAB 2012b and Visual C++ 2008, Windows 7 64-bit)</p>
  
  <p><strong>In MATLAB:</strong></p>
  
  <ol>
  <li><code>mbuild -setup</code> and <code>mex -setup</code> to set Visual Microsoft Visual C++ 2008 SP1 as the compiler. </li>
  <li><p>Create <code>MyAdd.m</code> in folder <code>C:\Users\WangYudong\Documents\MATLAB\MyAdd_M</code> and the function is like:</p>
  
  <pre><code>function [c] = MyAdd(a, b)
  c = a + b;
  </code></pre></li>
  <li><p><code>mcc -W cpplib:libMyAdd -T link:lib MyAdd</code> to compile <code>MyAdd.m</code> and generate several files including <code>libMyAdd.dll</code>, <code>libMyAdd.h</code>, <code>libMyAdd.lib</code> and other files.</p></li>
  </ol>
  
  <p><strong>In C++</strong></p>
  
  <ol>
  <li><p>Select <strong>VC++ Directories</strong> → <strong>Include files</strong> to add <code>E:\MATLAB\R2012b\extern\include</code>.</p></li>
  <li><p>Select <strong>VC++ Directories</strong> → <strong>Library files</strong> to add
  <code>E:\MATLAB\R2012b\extern\lib\win64\microsoft</code> and
  <code>C:\Users\WangYudong\Documents\MATLAB\MyAdd_M</code>.</p></li>
  <li><p>Select <strong>Linker</strong> → <strong>Input</strong> → <strong>Additional Dependencies</strong> to add new entries:</p>
  
  <pre><code>mclmcr.lib
  mclmcrrt.lib
  libmx.lib
  libmat.lib
  libMyAdd.lib
  </code></pre></li>
  <li><p>Create a new <code>MyAdd_test.cpp</code> and put <code>libMyAdd.dll</code>, <code>libMyAdd.h</code> and <code>libMyAdd.lib</code> in the same folder. Add <code>libMyAdd.h</code> in <strong>Header Files</strong>, <code>libMyAdd.h</code> and <code>libMyAdd.lib</code> in <strong>Resource Files</strong>. </p></li>
  </ol>
  
  <p>Code of <code>MyAdd_test.cpp</code> is like:</p>
  
  <pre><code>#include "mclmcr.h"
  #include "matrix.h"
  #include "mclcppclass.h"
  #include "libMyAdd.h"
  
  int main() {   
      double a = 6;
      double b = 9;
      double c;
      // initialize lib
      if( !libMyAddInitialize()) {
          std::cout &lt;&lt; "Could not initialize libMyAdd!" &lt;&lt; std::endl;
          return -1;
      }
  
      // allocate space
      mwArray mwA(1, 1, mxDOUBLE_CLASS);
      mwArray mwB(1, 1, mxDOUBLE_CLASS);
      mwArray mwC(1, 1, mxDOUBLE_CLASS);  
      // set data
      mwA.SetData(&amp;a, 1);
      mwB.SetData(&amp;b, 1);
      // use function: MyAdd
      MyAdd(1, mwC, mwA, mwB);
      // get data
      c = mwC.Get(1, 1);
      printf("c is %f\n", c); 
  
      // terminate the lib
      libMyAddTerminate();   
      // terminate MCR
      mclTerminateApplication();
      return 0;
  }  
  </code></pre>
  
  <p>At last, the result is</p>
  
  <pre><code>Compiling...
  MyAdd_test.cpp
  Linking...
  MyAdd_test.obj : error LNK2019: unresolved external symbol _mclTerminateApplication_proxy referenced in function _main
  MyAdd_test.obj : error LNK2019: unresolved external symbol _libMyAddTerminate referenced in function _main
  MyAdd_test.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) void __cdecl MyAdd(int,class mwArray &amp;,class mwArray const &amp;,class mwArray const &amp;)" (__imp_?MyAdd@@YAXHAAVmwArray@@ABV1@1@Z) referenced in function _main
  MyAdd_test.obj : error LNK2019: unresolved external symbol _libMyAddInitialize referenced in function _main
  MyAdd_test.obj : error LNK2019: unresolved external symbol _mclGetMatrix referenced in function "public: __thiscall mwArray::mwArray(unsigned int,unsigned int,enum mxClassID,enum mxComplexity)" (??0mwArray@@QAE@IIW4mxClassID@@W4mxComplexity@@@Z)
  MyAdd_test.obj : error LNK2019: unresolved external symbol _mclcppGetLastError referenced in function "public: static void __cdecl mwException::raise_error(void)" (?raise_error@mwException@@SAXXZ)
  MyAdd_test.obj : error LNK2019: unresolved external symbol _mclcppCreateError referenced in function "public: __thiscall mwException::mwException(void)" (??0mwException@@QAE@XZ)
  MyAdd_test.obj : error LNK2019: unresolved external symbol _ref_count_obj_addref referenced in function "public: __thiscall mwException::mwException(class mwException const &amp;)" (??0mwException@@QAE@ABV0@@Z)
  MyAdd_test.obj : error LNK2019: unresolved external symbol _ref_count_obj_release referenced in function "public: virtual __thiscall mwException::~mwException(void)" (??1mwException@@UAE@XZ)
  MyAdd_test.obj : error LNK2019: unresolved external symbol _error_info_get_message referenced in function "public: virtual char const * __thiscall mwException::what(void)const " (?what@mwException@@UBEPBDXZ)
  MyAdd_test.obj : error LNK2019: unresolved external symbol _array_ref_getV_int referenced in function "public: class mwArray __cdecl mwArray::GetPromoted(unsigned int,...)" (?GetPromoted@mwArray@@QAA?AV1@IZZ)
  MyAdd_test.obj : error LNK2019: unresolved external symbol _array_ref_set_numeric_mxDouble referenced in function "public: void __thiscall mwArray::SetData(double *,unsigned int)" (?SetData@mwArray@@QAEXPANI@Z)
  MyAdd_test.obj : error LNK2019: unresolved external symbol _array_ref_get_numeric_mxDouble referenced in function "public: __thiscall mwArray::operator double(void)const " (??BmwArray@@QBENXZ)
  C:\Users\WangYudong\Documents\Visual Studio 2008\Projects\MyAdd_C\Debug\MyAdd_C.exe : fatal error LNK1120: 13 unresolved externals
  </code></pre>
  
  <hr>
  
  <p>Actually, the work above is my test to call a custom MATLAB function from C++. My following work is to convert a MATLAB program to C++, which contains image processing functions like <code>imread</code>, <code>edge</code>, <code>strel</code>, etc. I've tried MATLAB Coder, but it can't convert MATLAB functions. So I try the method above. <strong>Is it an efficient way to convert those functions</strong> or should I implement them using OpenCV?</p>