title: How to import a C++ dll in Matlab
body: <p>I'm struggling with the importation of C++ (2017) dlls in Matlab (2019a).</p>
  
  <p>In particular, since I'm not familiar with C++, I tried to create the MathLibrary defined in <a href="https://github.com/MicrosoftDocs/cpp-docs/blob/master/docs/build/walkthrough-creating-and-using-a-dynamic-link-library-cpp.md" rel="nofollow noreferrer">https://github.com/MicrosoftDocs/cpp-docs/blob/master/docs/build/walkthrough-creating-and-using-a-dynamic-link-library-cpp.md</a>.
  I created the library according to the guide, compiled both Debug and Release (x86), and the import with the MathClient described works fine.</p>
  
  <p>However, I tried to import the library in Matlab (after adding the needed folders to the current path) with:</p>
  
  <pre><code>        if libisloaded('MathLibrary') == 0
             handle = loadlibrary('MathLibrary.dll','MathLibrary.h');
          end
  </code></pre>
  
  <p>and it throws a tons of errors:</p>
  
  <pre><code>        Warning: Message from C preprocessor:
          cl : command line warning D9027: source file 'C:\Users\miche\source\repos\MathLibrary\Release\MathLibrary.lib' ignored
          cl : command line warning D9021 : no operation performed
  
          &gt; In loadlibrary 
          Error loading library intermediate output follows.
          The actual error is at the end of this output.
          *********
          Unescaped left brace in regex is deprecated here (and will be fatal in Perl 5.30), passed through in regex; marked by &lt;-- HERE in m/struct([^;,{(]*){ &lt;-- HERE (.*?)\}([^;])*;/ at D:\Program Files\MATLAB\R2019a\toolbox\matlab\general\private\prototypes.pl line 912.
          Warning no reference to header 'MathLibrary' added with addheader was found in source.
          *********
          Error using loadlibrary
          Building MathLibrary_thunk_pcwin64 failed.  Compiler output is:
          cl -I"D:\Program Files\MATLAB\R2019a\extern\include" /Zp8  /W3
          /nologo  -I"D:\Program Files\MATLAB\R2019a\bin"
          -I"C:\Users\user\source\repos\MathLibrary\Release"
          "MathLibrary_thunk_pcwin64.c" -LD
          -Fe"MathLibrary_thunk_pcwin64.dll"
          MathLibrary_thunk_pcwin64.c
          C:\Users\miche\source\repos\MathLibrary\Release\MathLibrary.lib(1):
          error C2059: syntax error: '!'
          C:\Users\miche\source\repos\MathLibrary\Release\MathLibrary.lib(2):
          error C2018: character '0x60' unknown
  </code></pre>
  
  <p>and then it just goes on like this, with a lot of character errors.
  What is wrong with my code? How do I solve this?</p>