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

3 views (last 30 days)
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)

Geoff Hayes
Geoff Hayes on 27 Dec 2014
Edited: Geoff Hayes on 27 Dec 2014
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
Geoff Hayes
Geoff Hayes on 28 Dec 2014
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.
Geoff Hayes
Geoff Hayes on 2 Jan 2015
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.

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!