How to compile shrlibsample in R2012a
1 次查看(过去 30 天)
显示 更早的评论
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.
回答(1 个)
Konrad Malkowski
2013-3-14
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.
- Navigate to matlabroot/extern/examples/shrlib
- Compile the library
- % gcc -c shrlibsample.c -shared -I matlabroot/extern/include -fPIC -o shrlibsample.o
- % 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
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 个评论
Philip Borghesani
2013-3-14
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)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Call C from MATLAB 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!