How to build a shared library using the mex command on OS-X and Linux?

On Windows I use the mex command to build a shared library suitable for use with loadlibrary using the following commands:
mex -largeArrayDims shrlibsample.c
loadlibrary( 'shrlibsample.mexw64', 'shrlibsample.h' )
libfunctions( 'shrlibsample', '-full' )
unloadlibrary( 'shrlibsample' )
With OS-X and Linux similar commands (with the library file extension suitably modified) fail with loadlibrary complaining:
Warning: The function 'multDoubleArray' was not found in the library
> In /Applications/MATLAB_R2014b.app/toolbox/matlab/general/loadlibrary.p>loadlibrary at 424
One warning for each exported function in the module.
Building my shared libraries with mex in this manner would greatly simplify the process of porting my code across platforms. Here, I have used the example code provided with Matlab as a simple test case. The library of my code that I successfully build on Windows with mex is C++ based and considerably more complex.
Why does it work on Windows and fail on OS-X and Linux? What am I missing?

Answers (1)

Duane - if you look at the function declarations in the header file, do you see something similar to
#define MY_FUNC_API __declspec(dllexport)
extern "C" MY_FUNC_API void myFunction();
Since you have indicated that the code compiles and the functions load for Windows, I suspect that the above is true and so you will have to modify the code for the other operating systems to ensure that the functions declarations are visible.
If you take a look at https://www.ibm.com/developerworks/aix/library/au-porting/ or http://gcc.gnu.org/wiki/Visibility, all you may have to do is replace the above define with
#define MY_FUNC_API __attribute__ ((__visibility__("default")))
Take a look at the examples from the above links to see how you can just add a couple lines of code so that that the header will be suitable for the three operating systems.

3 Comments

Thank you Geoff for your prompt response to my question.
I wish that the solution to my issue were this easy. I have successfully built my shared library using make on OS-X with clang++ and on Linux with g++ so my code handles the exporting of symbols properly, and the library does work with MATLAB. It was a chore to get the C++ code to compile clean on Windows with the Microsoft SDK prescribed by MATLAB, but I got it to work using the mex command only. (Nice because I am not a Windows developer.)
When I went back and tried to build my library with mex on OS-X and Linux I had no luck. So I tried to build the simple example included with MATLAB to no avail. I have tried to coerce mex into building the shared library example on these other platforms but have failed so far.
By the way, mex successfully builds my library sources as a standard mex file, containing a library test suite, on all three platforms.
So. I guess my question should have been something like: "Why does the MathWorks provided shared library example produce a working library using the mex command on Windows, but not on OS-X or Linux?"
If mex could build shared libraries on all platforms it would allow for standard format source distributions that could be easily installed (by non-developer users) across MATLAB platforms, and perhaps even across MATLAB releases.
Duane - that is a good question concerning the issuing of code and libraries that work on one operating system, but not on another. I have a Mac too, and the shrlibsample example does include a shrlibsample.mexmaci64 file that can be loaded successfully, though its usage may leave something to be desired. For examples such as these, I would like to see how The Mathworks built these files - was it with MEX, or mcc, and what was the command to do so.
Duane - you may want to create a service request with The MathWorks to address your issue. Go to http://www.mathworks.com/support/bugreports/ and choose the View Service Requests link. Press the Create Service Request and select the Technical Support option. (I've never done this so am not sure on how this support request is addressed.)

Sign in to comment.

Products

Asked:

on 26 Dec 2014

Commented:

on 2 Jan 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!