0

I am using codegen in MATLAB R2016b to generate a .dll file as follows:

codegen -config:dll ex_fun.m -args {0,0,0,0,0,0}

I have tried to include the dll file as a reference using Visual Studio 2015 but nothing works fine and I couldn't use the .lib file as a linker eitherll.

The strange thing is that I could easily use gcc on Linux by compiling the .c script that calls the C function along with the output .so file (which is equivalent to .dll in windows) as follows:

gcc main.c ex_fun.so -Wl,-rpath=$(pwd)

However, I couldn't find any direct method in Windows. How can I use and call a .dll file output by MATLAB in a C main script program?

It should be noted that a .def file is also generated but I don't how to use it along with the output dll file.

Adriaan
  • 17,741
  • 7
  • 42
  • 75

1 Answers1

0

In Visual Studio IDE:

  • Switch platform to "x64"

In Project properties:

  • C++>General>Additional include directories = ^add compilation folder.

  • Linker>General>Additional libraries directories = ^add compilation
    folder.

  • Linker>Input>Additional Dependencies = "ex_fun.lib"

  • In code: add #include "ex_fun.h"
  • After compilation, copy the "ex_fun.dll" to output folder and run.
Mendi Barel
  • 3,350
  • 1
  • 23
  • 24
  • I have tried your suggested steps. However, I am having this error message: LNK1190 invalid fixup found, type 0x0014 ex_fun C:\Users\LAB\Desktop\test_ex_fun\ex_fun\ex_fun\ex_fun.lib(ex_fun.obj) – ahmed deia Aug 21 '17 at 08:57
  • By the way, I am using visual studio 2015 – ahmed deia Aug 21 '17 at 09:07
  • No. for the32 bit platform, I am rather having this error for all the .h files : LNK2019 unresolved external symbol _ex_fun – ahmed deia Aug 21 '17 at 15:27
  • And I am having this warning : LNK4272 library machine type 'x64' conflicts with target machine type 'X86' – ahmed deia Aug 21 '17 at 15:38
  • By the way, I am following up the steps in this link : https://www.mathworks.com/help/coder/ug/using-a-matlab-coder-dynamic-library-in-a-simple-microsoft-visual-studio-project.html?searchHighlight=Use%20a%20C%20Dynamic%20Library%20in%20a%20Microsoft%20Visual%20Studio%20Project&s_tid=doc_srchtitle. However, I am having the error of LNK1190 as previously mentioned. I hope this might help you – ahmed deia Aug 21 '17 at 15:46
  • I have contacted the mathworks team and they have replied that they assume that the compiler used for code generation is visual studio. However When I tried to setup this compiler on matlab using mex -setup, I had this error:No supported compiler or SDK was found although the visual studio c++ compiler is installed from visual studio 2015 – ahmed deia Aug 22 '17 at 11:51
  • my problem is solved by install the visual studio SDK using this url: https://msdn.microsoft.com/en-us/library/mt683786.aspx. Then apply mex -setup on matlab command window and thus, it should be indicated that MEX is configured to microsoft visual c++ 2015. Then by applying the steps mentioned by Mendi Barel or even mentioned in https://www.mathworks.com/help/coder/ug/using-a-matlab-coder-dynamic-library-in-a-simple-microsoft-visual-studio-project.html?searchHighlight=Use%20a%20C%20Dynamic%20Library%20in%20a%20Microsoft%20Visual%20Studio%20Project&s_tid=doc_srchtitle, the problem is solved. – ahmed deia Aug 22 '17 at 15:25