December 2009

I have spent a significant amount of time attempting to write a dll in C/C++ and call it in Matlab. An article was previously written on this topic at http://guoliangye.com/2009/01/21/how-to-write-a-dll-in-cc-and-call-it-in-matlab/ (link no longer working as of 2018) but I feel the need to discuss it further in both the Windows and Linux 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.

I wanted to update everyone on the Northwestern University Men’s Basketball and Football teams and their progress so far as of Christmas 2009. Happy Holidays to all!

Thus far I have been very impressed with both team’s play.

The Northwestern Men’s Basketball team is 10-1. The only loss is a home loss to a ranked Butler team. Big Ten play starts against Illinois on December 30th, 2009.

Two good articles to read on the Basketball team are at http://sports.espn.go.com/chicago/columns/story?columnist=powers_scott&id=4766881 and http://sports.espn.go.com/ncb/columns/story?columnist=katz_andy&id=4766773.

Hopefully this will be the first time the basketball team makes the NCAA march madness tournament as many are predicting at this point.

Andy Katz at ESPN.com writes.

…Northwestern, for the first time ever, will be in the Dance. The Wildcats won’t be on the bubble, either. I predict NU will be in the field by the last week of the regular season.

Moving on to the Northwestern University 2009 Football Team. The Wildcats finished the regular season at 8-4.

The Wildcats will be playing the Auburn University Tigers at the Outback Bowl in Tampa, Florida, on January 1, 2010 at 10 AM CST and shown nationally on ESPN. I plan to be in attendance at the game and cheering on the wildcats for hopefully their first bowl win.

Northwestern is looking for it’s first bowl win since 1949. Here is a picture of the team celebrating a win at Purdue earlier this season.

purdue_northwestern_football_game_09

Recently, I had the privilege of attending a Chicago Bulls basketball game at the United Center on December 17, 2009. The Bulls won 98 to 89 versus the New York Knicks. Here are some pictures from the basketball game. The pictures are mostly of pre-game and half-time warmups. One picture gives respect to Michael Jordan and Scottie Pippin.

bulls_game_4

bulls_game_3

bulls_game_2

bulls_game_1