简体   繁体   English

使用外部库编译 mex function

[英]Compile mex function with external libraries

I'm trying to generate a mex function usigin external libraries.我正在尝试生成一个 mex function 使用外部库。 I'm using Ubuntu 18 and Matlab R2021a.我正在使用 Ubuntu 18 和 Matlab R2021a。

In particular I want to compile my file.cpp that uses my cpp library called model.特别是我想编译我的 file.cpp,它使用我的名为 model 的 cpp 库。 What I did is我所做的是

mex -I<path_library_include> -L<path_library_so_file> -lmodel.so -lboost_system -lstdc++ file.cpp -v

where in -I i put the path where is the include of the library in -L the path in which the libmodel.so is located, then I added 2 more libraries and at the end the source file that I want to compile.在 -I 中,我将包含库的路径放在 -L 中 libmodel.so 所在的路径中,然后我添加了另外 2 个库,最后添加了我要编译的源文件。

In this way I can compile my source but when I try to execute the mex function I get:通过这种方式,我可以编译我的源代码,但是当我尝试执行 mex function 时,我得到:

libmodel.so: cannot open shared object file: No such file or directory libmodel.so:无法打开共享 object 文件:没有这样的文件或目录

I also tested the library outside matlab and works fine, this is the command that I use to compile the library outside Matlab我还测试了 matlab 之外的库并且工作正常,这是我用来编译 Matlab 之外的库的命令

gcc -Wall -I<path_library_include> -L<path_library_so_file> main.cpp -lmodel -lboost_system -lstdc++ -o main

What could be the problem with Matlab? Matlab 可能有什么问题?

Thanks to 273K that gave me the right direction.感谢273K给了我正确的方向。 The problem was that the LD_LIBRARY_PATH was not configured well in fact running /sbin/ldconfig -v my library was not present.问题是 LD_LIBRARY_PATH 配置不正确,实际上运行/sbin/ldconfig -v我的库不存在。 So to add the shared library i created a new file as root in /etc/ld.so.conf.d/ called mylib.conf it is not important the name just the extension.因此,为了添加共享库,我在/etc/ld.so.conf.d/中以 root 身份创建了一个名为mylib.conf的新文件,名称只是扩展名并不重要。 Then I run然后我跑

sudo ldconfig

after that the library was present in fact running之后图书馆实际上正在运行

/sbin/ldconfig -v | grep  model 

where model is the name of my library.其中 model 是我的库的名称。 it is possible to see the output.可以看到 output。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM