How to compile shrlibsample in R2012a

1 view (last 30 days)
Steven
Steven on 11 Mar 2013
Folks,
I see good documentation on how to use the compiled example that ships with R2012a, but I don't see how to compile it, specifically the shrlibsample. Can someone point me to some documentation or answer how one should compile it? I'm asking this also because I can't seem to get structures to work the way they're supposed to, and doing things the "Matlab way" might make things work.

Answers (1)

Konrad Malkowski
Konrad Malkowski on 14 Mar 2013
You can compile the code in two ways: - using gcc - using mex (which uses gcc internally)
Here's how I compiled the code using gcc.
  1. Navigate to matlabroot/extern/examples/shrlib
  2. Compile the library
  3. % gcc -c shrlibsample.c -shared -I matlabroot/extern/include -fPIC -o shrlibsample.o
  4. % gcc -shared -Wl,-soname,libshrsample.so.1 -o libshrsample.so.1.0.1 shrlibsample.o
Now you will have a file names libshrsample.so.1.0.1 in the directory
Start MATLAB. Navigate to matlabroot/extern/examples/shrlib
hfile = fullfile(pwd,'shrlibsample.h')
>> loadlibrary('libshrsample.so.1.0.1',hfile)
Warning: The library name is not a valid name.
The library will be named "libshrsample0x2Eso0x2E10x2E0".
> In loadlibrary at 174
>> struct.p1 = 4; struct.p2 = 7.3; struct.p3 = -290
struct =
p1: 4
p2: 7.3000
p3: -290
>> [res, st] = calllib('libshrsample0x2Eso0x2E10x2E0', 'addStructByRef', struct)
res =
-279
st =
p1: 5.5000
p2: 1234
p3: 12345678
>>
(For more info on this example see the "calllib" help page).
You can also compile the code using mex.
>> cd ../extern/examples/shrlib/
>> hfile = fullfile(pwd,'shrlibsample.h')
hfile =
<matlabroot>/extern/examples/shrlib/shrlibsample.h
>> mex -v shrlibsample.c LDFLAGS="-shared -Wl"
**************************************************************************
Warning: Neither -compatibleArrayDims nor -largeArrayDims is selected.
Using -compatibleArrayDims. In the future, MATLAB will require
the use of -largeArrayDims and remove the -compatibleArrayDims
option. For more information, see:
http://www.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html
**************************************************************************
-> mexopts.sh sourced from directory (DIR = $MATLAB/bin)
FILE = <matlabroot>/bin/mexopts.sh
----------------------------------------------------------------
-> MATLAB = <matlabroot>
-> CC = gcc
-> CC flags:
CFLAGS = -ansi -D_GNU_SOURCE -fexceptions -fPIC -fno-omit-frame-pointer -pthread
CDEBUGFLAGS = -g
COPTIMFLAGS = -O -DNDEBUG
CLIBS = -Wl,-rpath-link,<matlabroot>/bin/glnxa64 -L<matlabroot>/glnxa64 -lmx -lmex -lmat -lm -lstdc++
arguments = -DMX_COMPAT_32
-> CXX = g++
-> CXX flags:
CXXFLAGS = -ansi -D_GNU_SOURCE -fPIC -fno-omit-frame-pointer -pthread
CXXDEBUGFLAGS = -g
CXXOPTIMFLAGS = -O -DNDEBUG
CXXLIBS = -Wl,-rpath-link,<matlabroot>/glnxa64 -L<matlabroot>/bin/glnxa64 -lmx -lmex -lmat -lm
arguments = -DMX_COMPAT_32
-> FC = gfortran
-> FC flags:
FFLAGS = -fexceptions -fbackslash -fPIC -fno-omit-frame-pointer
FDEBUGFLAGS = -g
FOPTIMFLAGS = -O
FLIBS = -Wl,-rpath-link,<matlabroot>/bin/glnxa64 -L<matlabroot>/bin/glnxa64 -lmx -lmex -lmat -lm
arguments = -DMX_COMPAT_32
-> LD = gcc
-> Link flags:
LDFLAGS = -shared -Wl
LDDEBUGFLAGS = -g
LDOPTIMFLAGS = -O
LDEXTENSION = .mexa64
arguments =
-> LDCXX =
-> Link flags:
LDCXXFLAGS =
LDCXXDEBUGFLAGS =
LDCXXOPTIMFLAGS =
LDCXXEXTENSION =
arguments =
----------------------------------------------------------------
-> gcc -c -I<matlabroot>/extern/include -I<matlabroot>/simulink/include -DMATLAB_MEX_FILE -ansi -D_GNU_SOURCE -fexceptions -fPIC -fno-omit-frame-pointer -pthread -DMX_COMPAT_32 -O -DNDEBUG "shrlibsample.c"
-> gcc -O -shared -Wl -o "shrlibsample.mexa64" shrlibsample.o -Wl,-rpath-link,<matlabroot>/bin/glnxa64 -L<matlabroot>/bin/glnxa64 -lmx -lmex -lmat -lm -lstdc++
>> loadlibrary('shrlibsample',hfile)
>> struct.p1 = 4; struct.p2 = 7.3; struct.p3 = -290
struct =
p1: 4
p2: 7.3000
p3: -290
>> [res, st] = calllib('shrlibsample', 'addStructByRef', struct)
res =
-279
st =
p1: 5.5000
p2: 1234
p3: 12345678
>>
>>
  1 Comment
Philip Borghesani
Philip Borghesani on 14 Mar 2013
I like the shared library versioning in the first example but hate the generated name try:
loadlibrary('libshrsample.so.1.0.1',hfile,'alias','sample')
[res, st] = calllib('sample', 'addStructByRef', struct)

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!