1

I have a C++ header file and the corresponding static library (.lib file). No source file. The code comprises several classes, each with functions of its own.

I would like to be able to call these functions from MATLAB.

Would anyone be kind enough to point me in the right direction? Examples would be appreciated.

Thanks.

EDIT: Would it be a good idea to write a DLL to act as an interface between the static library and MATLAB? Then I can access the functions in my static library from the DLL and use calllib to access the DLL from MATLAB. If so, how would I call the class member functions in the static library from a DLL?

Update So I've decided to write a DLL to which I will link the static library and call the functions from MATLAB through that using calllib The problem is, the static library is already compiled and I do not have the source. I try to link the .lib file (presumably built using Visual Studio) and build my DLL using MINGW32 but this doesn't work possibly due to (lack of?) interoperability of libraries created by different compilers. This is the MinGW command I use to link: g++ -c -o -DBUILDING_INTERMEDIATE_DLL intermediate_dll.cpp -TaccClient.lib

intermediate_dll.cpp is the source file for my DLL and TaccClient.lib is the static library I'm using.

Does anyone have any suggestions as to how this could work, or will I have to wait and try it on visual studio later?

Ali Haroon
  • 198
  • 1
  • 1
  • 11
  • Take a look at [`calllib`](http://www.mathworks.com/help/matlab/ref/calllib.html). You'll probably need a wrapper to call class functions. – buzjwa Nov 17 '14 at 12:17
  • @Naveh is calllib not only for shared libraries? I have a static library and the corresponding header file only - no source file. – Ali Haroon Nov 17 '14 at 12:43
  • Your DLL approach is the same as people discussed here :) http://www.mathworks.com/matlabcentral/answers/137241-using-static-library-in-matlab and an example is here http://stackoverflow.com/questions/24704251/linking-and-loading-static-lib-with-mex – Kostya Nov 17 '14 at 14:33
  • @AliHaroon true, silly me :P Go with the mex approach then – buzjwa Nov 17 '14 at 17:05
  • Thanks for the suggestions! I'm writing a DLL to which I will link the static library and call the functions from MATLAB through that using `calllib` – Ali Haroon Nov 18 '14 at 10:52

1 Answers1

0

MATLAB offers MATLAB coder functionality that allows you to create MEX files from your C/C++ source and library files. There is also an option to called C shared libraries when building the MEX file for your application. I have used SIMULINK (with SIMULINK Coder) and Embedded Coder to do my library linking and it helps you to do it via GUI, so very neat job. However, you can still do it with MATLAB as long as you have a supported C/C++ compiler.

Using the C shared library option, you should be able to load, call, view the library and functions. For more details on what other functionlities are available, see this - http://uk.mathworks.com/help/matlab/using-c-shared-library-functions-in-matlab-.html

A similar question was answered here. May be you will find it useful?

Community
  • 1
  • 1
ha9u63a7
  • 6,233
  • 16
  • 73
  • 108
  • Unfortunately I do not have the source file. I only have the static library and header file. I did come across that question but did not find it particularly useful, since I lack the source file. – Ali Haroon Nov 17 '14 at 12:44