共享库位置为matlab mex文件: [英] shared library locations for matlab mex files:
问题描述
我正在尝试写一个使用libhdf5的matlab mex函数;我的Linux安装提供了libhdf5-1.8共享库和头文件。不过,我的Matlab版本r2007b在1.6版本中提供了libhdf5.so。 (显然,Matlab .mat
文件bootstrap hdf5)。当我编译mex时,它在Matlab中进行段错误。如果我将我的libhdf5版本降级到1.6(不是长期选项),代码编译并运行良好。
问题:我该如何解决这个问题?如何告诉mex编译过程链接到/usr/lib64/libhdf5.so.6而不是/opt/matlab/bin/glnxa64/libhdf5.so.0?当我在我的编译中使用 -Wl,-rpath-link,/ usr / lib64
执行此操作时,出现如下错误:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.3.4 /../../../../ x86_64-pc- linux-gnu / bin / ld:警告:/opt/matlab/matlab75/bin/glnxa64/libmat.so所需的libhdf5.so.0可能与libhdf5.so.6冲突
/ usr / lib / gcc / x86_64-pc-linux-gnu / 4.3.4 /../../../../ lib64 / crt1.o:函数`_start':
(.text + 0x20):undefined引用`main'
collect2:ld返回1退出状态
mex:'hdf5_read_strings.mexa64'的链接失败。
make:*** [hdf5_read_strings.mexa64]错误1
ack 。最后的办法是下载hdf5-1.6.5头文件的本地副本并完成它,但这不是未来的证明(我将来会进行一次Matlab版本升级)。任何想法?编辑:根据Ramashalanka的出色建议,我
称为 mex -v
来获取3 gcc
命令;最后一个是链接器命令;
B)用 -v
来调用该链接器命令来获取 collect
命令;
collect2 -v -t
和其他标志。
我输出的相关部分:
/ usr / bin / ld:mode elf_x86_64
/usr/lib/gcc/x86_64-pc-linux-gnu/4.3.4/../../../../lib64/ crti.o
/usr/lib/gcc/x86_64-pc-linux-gnu/4.3.4/crtbeginS.o
hdf5_read_strings.o
mexversion.o
-lmx( /opt/matlab/matlab75/bin/glnxa64/libmx.so)
-lmex(/opt/matlab/matlab75/bin/glnxa64/libmex.so)
-lhdf5(/ usr / lib / gcc /x86_64-pc-linux-gnu/4.3.4/../../../../lib64/libhdf5.so)
/lib64/libz.so
-lm(/ usr /lib/gcc/x86_64-pc-linux-gnu/4.3.4/../../../../lib64/libm.so)
-lstdc ++(/ usr / lib / gcc / x86_64 -pc-linux-gnu / 4.3.4 / libstdc ++。so)
-lgcc_s(/usr/lib/gcc/x86_64-pc-linux-gnu/4.3.4/libgcc_s.so)
/ lib64 / libpthread.so.0
/lib64/libc.so.6
/ lib64 / ld-linux- x86-64.so.2
-lgcc_s(/usr/lib/gcc/x86_64-pc-linux-gnu/4.3.4/libgcc_s.so)
/ usr / lib / gcc / x86_64- pc-linux-gnu / 4.3.4 / crtendS.o
/usr/lib/gcc/x86_64-pc-linux-gnu/4.3.4/../../../../lib64/ crtn.o
所以,实际上 libhdf5.so $ c来自
/ usr / lib64
的$ c>被引用。然而,我相信这是由环境变量 LD_LIBRARY_PATH
覆盖的,我的Matlab版本在运行时自动设置,因此它可以找到它自己的版本。 libmex.so
等。
我在考虑 crt_file.c
示例可以使用b / c它不使用我正在使用的函数(
H5DOpen
,它的签名更改从1.6移动到1.8(是,我正在使用 -DH5_USE_16_API
)),或者更不可能的是,b / c并没有达到需要hdf5的Matlab内部部分。 ack。
以下是我的系统:
-
安装hdf5 1.8.4版(您已经这样做了:我安装了源代码并进行了编译,以确保它与我的系统兼容,获得了gcc版本并获得了静态库 - 例如,为我的系统提供的二进制文件是
icc
特定的)。
-
创建一个目标文件。您已拥有自己的文件。我使用 h5_crtfile.c rel = noreferrer>这里(一个好主意,首先从这个简单的文件开始寻找警告)。我用通常的参数将
main
更改为mexFunction
,并包含mex.h $ c
指定要显式加载的 static 1.8.4库(不带-L的完整路径它是必需的),并且在
LDFLAGS
中不包含-lhdf5
。包含-t
选项,以确保没有加载动态hdf5库。您还需要安装-lz
,并安装zlib
。对于达尔文,我们还需要LDFLAGS
中的-bundle
:
mex CFLAGS =' - I / usr / local / hdf5 / include'LDFLAGS =' - t /usr/local/hdf5/lib/libhdf5.a -lz -bundle'h5_crtfile。 c -v
对于linux,您需要一个等效的与位置无关的调用,例如
fPIC
也许-shared
,但是我没有带有matlab许可证的linux系统,所以我可以' t检查:
mex CFLAGS =' - fPIC -I / usr / local / hdf5 / include'LDFLAGS =' - t / usr / local / hdf5 / lib / libhdf5.a -lz -shared'h5_crtfile.c -v
运行
h5_crtfile
mex文件。这在我的机器上运行时没有问题。它只是一个H5Fcreate和H5Fclose在当前目录中创建file.h5,当我调用file file.h5
时,我得到文件。 h5:分层数据格式(版本5)数据
。
在步骤3中上面的 -lhdf5
,然后当我尝试运行可执行文件时(因为它使用了matlab的动态库,这对我来说是1.6.5版本),matlab会中止这绝对是解决我系统上的问题。
感谢您的提问。上面的解决方案对我来说肯定比我之前做的更容易。希望以上的作品适合你。
I am trying to write a matlab mex function which uses libhdf5; My Linux install provides libhdf5-1.8 shared libraries and headers. However, my version of Matlab, r2007b, provides a libhdf5.so from the 1.6 release. (Matlab .mat
files bootstrap hdf5, evidently). When I compile the mex, it segfaults in Matlab. If I downgrade my version of libhdf5 to 1.6 (not a long-term option), the code compiles and runs fine.
question: how do I solve this problem? how do I tell the mex compilation process to link against /usr/lib64/libhdf5.so.6 instead of /opt/matlab/bin/glnxa64/libhdf5.so.0 ? When I try to do this using -Wl,-rpath-link,/usr/lib64
in my compilation, I get errors like:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.3.4/../../../../x86_64-pc-linux-gnu/bin/ld: warning: libhdf5.so.0, needed by /opt/matlab/matlab75/bin/glnxa64/libmat.so, may conflict with libhdf5.so.6
/usr/lib/gcc/x86_64-pc-linux-gnu/4.3.4/../../../../lib64/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status
mex: link of 'hdf5_read_strings.mexa64' failed.
make: *** [hdf5_read_strings.mexa64] Error 1
ack. the last resort would be to download a local copy of the hdf5-1.6.5 headers and be done with it, but this is not future proof (a Matlab version upgrade is in my future.). any ideas?
EDIT: per Ramashalanka's excellent suggestions, I
A) called mex -v
to get the 3 gcc
commands; the last is the linker command;
B) called that linker command with a -v
to get the collect
command;
C) called that collect2 -v -t
and the rest of the flags.
The relevant parts of my output:
/usr/bin/ld: mode elf_x86_64
/usr/lib/gcc/x86_64-pc-linux-gnu/4.3.4/../../../../lib64/crti.o
/usr/lib/gcc/x86_64-pc-linux-gnu/4.3.4/crtbeginS.o
hdf5_read_strings.o
mexversion.o
-lmx (/opt/matlab/matlab75/bin/glnxa64/libmx.so)
-lmex (/opt/matlab/matlab75/bin/glnxa64/libmex.so)
-lhdf5 (/usr/lib/gcc/x86_64-pc-linux-gnu/4.3.4/../../../../lib64/libhdf5.so)
/lib64/libz.so
-lm (/usr/lib/gcc/x86_64-pc-linux-gnu/4.3.4/../../../../lib64/libm.so)
-lstdc++ (/usr/lib/gcc/x86_64-pc-linux-gnu/4.3.4/libstdc++.so)
-lgcc_s (/usr/lib/gcc/x86_64-pc-linux-gnu/4.3.4/libgcc_s.so)
/lib64/libpthread.so.0
/lib64/libc.so.6
/lib64/ld-linux-x86-64.so.2
-lgcc_s (/usr/lib/gcc/x86_64-pc-linux-gnu/4.3.4/libgcc_s.so)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.3.4/crtendS.o
/usr/lib/gcc/x86_64-pc-linux-gnu/4.3.4/../../../../lib64/crtn.o
So, in fact the libhdf5.so
from /usr/lib64
is being referenced. However, this is being overriden, I believe, by the environment variable LD_LIBRARY_PATH
, which my version of Matlab automagically sets at run-time so it can locate its own versions of e.g. libmex.so
, etc.
I am thinking that the crt_file.c
example works either b/c it does not use the functions I am using (H5DOpen
, which had a signature change in the move from 1.6 to 1.8 (yes, I am using -DH5_USE_16_API
)), or, less likely, b/c it does not hit the parts of Matlab internals that need hdf5. ack.
The following worked on my system:
Install hdf5 version 1.8.4 (you've already done this: I installed the source and compiled to ensure it is compatible with my system, that I get gcc versions and that I get the static libraries - e.g. the binaries offered for my system are
icc
specific).Make a target file. You already have your own file. I used the simple
h5_crtfile.c
from here (a good idea to start with this simple file first a look for warnings). I changedmain
tomexFunction
with the usual args and includedmex.h
.Specify the static 1.8.4 library you want to load explicitly (the full path with no -L for it necessary) and don't include
-lhdf5
in theLDFLAGS
. Include a-t
option so you can ensure that there is no dynamic hdf5 library being loaded. You also need-lz
, withzlib
installed. For darwin we also need a-bundle
inLDFLAGS
:mex CFLAGS='-I/usr/local/hdf5/include' LDFLAGS='-t /usr/local/hdf5/lib/libhdf5.a -lz -bundle' h5_crtfile.c -v
For linux, you need an equivalent position-independent call, e.g.
fPIC
and maybe-shared
, but I don't have a linux system with a matlab license, so I can't check:mex CFLAGS='-fPIC -I/usr/local/hdf5/include' LDFLAGS='-t /usr/local/hdf5/lib/libhdf5.a -lz -shared' h5_crtfile.c -v
Run the
h5_crtfile
mex file. This runs without problems on my machine. It just does a H5Fcreate and H5Fclose to create "file.h5" in the current directory, and when I callfile file.h5
I getfile.h5: Hierarchical Data Format (version 5) data
.
Note that if I include a -lhdf5
above in step 3, then matlab aborts when I try to run the executable (because it then uses matlab's dynamic libraries which for me are version 1.6.5), so this is definitely solving the problem on my system.
Thanks for the question. My solution above is definitely much easier for me than what I was doing before. Hopefully the above works for you.
这篇关于共享库位置为matlab mex文件:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!