matlab

I have spent a significant amount of time attempting to write a dll in C/C++ and call it in Matlab. An article has already been written on this topic at http://guoliangye.com/2009/01/21/how-to-write-a-dll-in-cc-and-call-it-in-matlab/ but I feel the need to discuss it further in both the Windows and Matlab environments.

In Windows 32 bit with 32 bit MATLAB.

1) Create two files named Test.h and Test.cpp

Test.h

#ifndef MY_API
#define MY_API extern “C” __declspec(dllexport)
#endif
MY_API int add(int a, int b);
MY_API int sub(int a, int b);

Test.cpp

#ifndef MY_API
#define MY_API extern “C” __declspec(dllexport)
#endif
#include “Test.h”
int add(int a,int b)
{
return a + b;
}
int sub(int a,int b)
{
return a – b;
}

2) Compile into a DLL (.dll) file using Visual Studio 2008 Professional or Express Edition. If neither is installed, you will need to download and install. Check the MATLAB documentation to see the list of all of the supported compilers for your version of MATLAB .

Step by step instructions for creating a .dll using Visual Studio can be found at http://msdn.microsoft.com/en-us/library/ms235636(VS.80).aspx

3) You then need to copy the newly created .dll file along with the original .h file to a MATLAB directory search path or you can just include the call to the directory.

Now open MATLAB and type

loadlibrary(‘Test.dll’, ‘Test.h’);

4) You can view the newly loaded function using

libfunctionsview Test;

5) You can also call the functions such as

A = calllib(‘Test’,’add’,5,4);
B = calllib(‘Test’,’sub’,5,4);

6) You can also unload the library using

unloadlibrary Test

In Windows 64 bit with 64 bit MATLAB.

Getting the dll file to compile in the 64 bit Windows version is a bit more tricky. MATLAB wants a C file not a C++ file. So one needs to be careful.

1) Create two files named Test.h and Test.c

Test.h

int sum(int, int);

Test.c

__declspec(dllexport) int sum(int a, int b)
{
return a + b;
}

NOTE: A “.c” file is not a selectable file type in Visual Studio 2008 so you may have to manually enter the file extension. You can select a .cpp file when adding to your project, but make sure to call it ‘Test.c’ so that .cpp is not the extension.

2) Compile into a DLL (.dll) file using Visual Studio 2008 Professional SP1.  Check the MATLAB documentation to see the list of all of the supported compilers for your version of MATLAB .

Step by step instructions for creating a .dll using Visual Studio can be found at http://msdn.microsoft.com/en-us/library/ms235636(VS.80).aspx

However, you need to be careful here. Before building the dll you need to go into properities and then the configuration manager and then choose a new active solution platform. Then you need to select ‘x64’ copy the settings for win32 and click OK.

Now you may or may not see x64. By default the x64 compiler is not installed during the install of Microsoft Visual Studio 2008 Pro. You will need to select it or just choose the full install. Even doing this I had an issue where it would not show up unless I also installed SP1. So make sure you install SP1 and select the x64 compilers if you do not do a full install, they are not default.

These instructions go into more details on getting a x64 .dll to work http://msdn.microsoft.com/en-us/library/9yb4317s.aspx.

3) You then need to copy the newly created .dll file along with the original .h file to a MATLAB directory search path or you can just include the call to the directory.

Now open MATLAB and type

loadlibrary(‘Test.dll’, ‘Test.h’);

4) You can view the newly loaded function using

libfunctionsview Test;

5) You can also call the functions such as

C = calllib(‘Test’,’sum’,5,4);

6) You can also unload the library using

unloadlibrary Test

In Linux 64 bit with 64 bit MATLAB.

1) Create two files named Test.h and Test.c

Test.h

int add(int a, int b);
int sub(int a, int b);

Test.c

#include “Test.h”
int add(int a,int b)
{
return a + b;
}
int sub(int a,int b)
{
return a – b;
}

NOTE: An extra return should be added to the end of both of these files.

2) You will want to open up the Terminal. Then you will want to enter

sudo apt-get install build-essential

This will install gcc/gcc++ which will be needed to compile.

You will be compiling into a .so file. To do this make sure the two files you created in (1) are located in your username directory, if not copy them there. Then in the terminal type the following

gcc -c -fPIC Test.c -o Test.o
gcc -shared -Wl,-soname,Test.so.1 -o Test.so Test.o

3) You then need to copy the newly created .so file along with the original .h file to a MATLAB directory search path or you can just include the call to the directory.

Now open MATLAB and type

loadlibrary(‘/home/username/Test’,’/home/username/Test.h’)

Where username is the username you are on your Linux Install.

4) You can view the newly loaded function using

libfunctionsview Test;

5) You can also call the functions such as

A = calllib(‘Test’,’add’,5,4);
B = calllib(‘Test’,’sub’,5,4);

6) You can also unload the library using

unloadlibrary Test

NOTE:

The above was originally run and tested in December 2009 on a computer running Windows XP 32 bit and  MATLAB 2009 B 32 bit version,  on a computer running Windows 7 64 bit and MATLAB 2009 B 64 bit, and on a computer running Ubuntu 9.04 64 bit computer running MATLAB 2009 A 64 bit version.

This was also tested in November 2010 as an update on a computer running Ubuntu 10.10 64 bit with MATLAB 2010 B 64 bit version.

Refer to the latest MATLAB documentation for updates and what compiler you will need.

MATLAB documentation provides further information of using C shared libraries in MATLAB.

Further you may encounter errors relating to copying and pasting the above directly into files. Therefore I encourage you to directly type out all the files and commands.