Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking MATLAB to a DLL library

Tags:

dll

matlab

I am trying to execute some example code from a MATLAB toolkit, 'oscmex'. This toolkit allows for communication using the OSC protocol over MATLAB. I presume this question is non-specific; it should apply to any toolkit that is set-up in the manner that this one is.

Reasons aside, I'm having some simple trouble getting the toolkit up and running. The toolkit comes with no documentation whatsoever; just a set of six DLL files (in one directory), and a set of four MATLAB '.m' example code files (in another directory). Every toolkit I've used in the past has either been a built-in kit or has had an intuitive (semi-automated) install procedure.

After downloading the toolkit, the first thing I tried was to simply run one of the '.M' example codes. This failed as the first line of the code contained the function osc(), which is not (currently) recognised by MATLAB.

So, I figured maybe I need to move the '.M' files into the same folder as the DLLs; perhaps MATLAB would see the functions inside the DLLs. No dice.

So, I realise that I have to somehow link MATLAB to the DLLs on startup. I tried adding the DLLs to a folder and adding an entry to that in the 'pathdef.m' file. This also failed.

I've read somewhere I can load a DLL file by using the loadlibrary() function. So, I tried doing this for the DLL files. This failed on the first file:

>> loadlibrary('osc_free_address.dll')

Error using loadlibrary>lFullPath (line 587)
Could not find file osc_free_address.h.

I'm starting to run out of options... How can I get this set of DLLs up and running?

like image 452
CaptainProg Avatar asked Nov 12 '22 11:11

CaptainProg


2 Answers

Browsing this library's web page it would seems these DLLs are just old form of mex files.
Therefore, they should not be used in the context of shared library (e.g., using loadlibrary and calllib), but rather compiled directly to mex files.

To do so, I would suggest the following steps:

  1. Make sure you have a working mex compiler configured for your Matlab.
    In matlab, type:

    >> mex -setup
    

    this will guide you through the configuration process. I understand that you are working on a windows machine, I usually work with visual studio compiler - works best for me.

  2. This library's README file suggests that OSC

    requires liblo 0.22 or later. See http://plugin.org.uk/liblo/

    Make sure you have this library and it is located in you LD_LIBRARY_PATH (see e.g., this question for details, or the mex docs).

  3. Get the source code for OSC library from their repository.

  4. Compile the sources in matlab using

    >> mex -O -largeArrayDims osc_free_address.c
    >> mex -O -largeArrayDims osc_free_server.c
    

    and so on for all 7 c source files. After mex-ing the c files you'll have mex files that you can run from Matlab as if they were regular functions.
    You may find it useful to use the library's make file, as suggested by Andrew Mao.

Good luck,

like image 121
Shai Avatar answered Nov 15 '22 08:11

Shai


If you look at the build for that software, it is compiling mex files, not DLLs (shared libraries): http://sourceforge.net/p/oscmex/code/4/tree/trunk/src/osc_make.m.

I would try using the mex commands instead of the dll commands (perhaps the files are just misnamed.) Even better, I would compile the files yourself with mex using the build file in source.

Note that the instructions also say that you need liblo-0.22 in order to run the library, so make sure you have that accessible as well.

like image 37
Andrew Mao Avatar answered Nov 15 '22 07:11

Andrew Mao