c++matlablinkermex

Compile mex function with external libraries


I'm trying to generate a mex function usigin external libraries. I'm using Ubuntu 18 and Matlab R2021a.

In particular I want to compile my file.cpp that uses my cpp library called model. 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.

In this way I can compile my source but when I try to execute the mex function I get:

libmodel.so: cannot open shared object file: No such file or directory

I also tested the library outside matlab and works fine, this is the command that I use to compile the library outside 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?


Solution

  • Thanks to 273K that gave me the right direction. The problem was that the LD_LIBRARY_PATH was not configured well in fact running /sbin/ldconfig -v my library was not present. 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. 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. it is possible to see the output.