Clib is unable to load an interface library

35 visualizaciones (últimos 30 días)
Matt el 26 de Abr. de 2023
Comentada: Javier Ros el 20 de Jun. de 2023
I have a c++ shared library that I want to call from matlab using clib. Unfortunately when doing so clim seems to be unable to find some dependent libraries. Seems an easy problem but can't find my way around, so here are the details :
  • I compiled with g++ a library that I want to call from matlab.
  • This library contains (for now) a unique c++ fonction force_camera_restart that look lke this
void force_camera_restart(void);
  • depends on a couple of libraries in /opt/EVT/eSDK/lib
  • Those libraries are in the rpath so I think I should not have to put their path on LD_LIBRARY_PATH. Nevertheless i still do it, just in case, with the method from here
Here how is how I build an interface to my :
%% 01 before launching matlab I type this in a prompt
export LD_LIBRARY_PATH=/opt/EVT/eSDK/lib:$LD_LIBRARY_PAT % should not be usefull but can't hurt
export LD_LIBRARY_PATH=/home/iscat/test:$LD_LIBRARY_PATH % where my is
export LD_LIBRARY_PATH=/home/iscat/test/force_camera_restart:$LD_LIBRARY_PATH % where my will go
% Lauching matlab from the prompt (not sudo)
%% 02 checking LD_LIBRARY_PATH and PATH
split(LD_LIBRARY_PATH,':') % checking : OK
% Adding .so to the path (I don't know why but I saw this in a exemple)
loc =['/opt/EVT/eSDK/include:',... % contains the .h of the following lib
'/opt/EVT/eSDK/genicam/bin/CLProtocol/Test/Linux64_x64:',... % eSDK/lib librairies depends on librairies here so ... why not adding it ?
path = getenv('PATH');
setenv('PATH', [loc path]);
path = getenv('PATH');
split(path,':') % checking : OK
%% 03 Generate Definition & build interface
headerFiles = '/home/iscat/test/force_camera_restart.h';
includePath = '/opt/EVT/eSDK/include';
% putting in a cell format all the lib path :
libPath = '/opt/EVT/eSDK/lib/*.so*';
libraries_dir = dir(libPath);
for ii=1:length(libraries_dir)
libraries_path{ii} = [libraries_dir(ii).folder ,'/', libraries_dir(ii).name ];
libraries_path{end+1} = '/home/iscat/test/';
clibgen.generateLibraryDefinition(headerFiles, "IncludePath", includePath, ...
'Libraries', libraries_path, 'OutputFolder', pwd, 'PackageName', 'force_camera_restart',...
'OverwriteExistingDefinitionFiles',true,'verbose', true);
% Build definition
% add to path
% test
% I get :
% Unable to load interface library:
% '/home/iscat/test/force_camera_restart/'.
% Reason: The specified module could not be found.
% Ensure the C++ dependent libraries for the interface library are added to
% run-time path.
Why is matlab not finding my libraries ?
To help find which are missing I tried to use ldd as explained here and here (in the comments) :
% All librairies are well defined
% All librairies are well defined as well
And finally about the lib itself, it works : I can write a cpp code calling this lib and execute the function force_camera_restart, it works fine.
Thank you !

Respuestas (2)

Suman Sahu
Suman Sahu el 5 de Jun. de 2023
Editada: Suman Sahu el 5 de Jun. de 2023
Hello Matt,
It is possible that the runtime libraries required by your shared library have not been properly resolved by MATLAB when attempting to load the library. Here are a few steps you can follow to troubleshoot the issue:
1. Check the library dependencies: Use the ldd command to check the list of dependencies for your shared library. For example, if your shared library is called, run the following command in the terminal:
ldd /home/iscat/test/
2. Check the library search path: Check the LD_LIBRARY_PATH variable to ensure that it includes the correct paths to the libraries required by your shared library. You can do this by running the following command in MATLAB:
3. Specify library search path explicitly: You can try specifying the library search path explicitly by adding the following line in your MATLAB script:
setenv('LD_LIBRARY_PATH', '/path/to/dependency/libraries');
%Replace "/path/to/dependency/libraries" with the directory path that contains the required libraries.
4. Run MATLAB with elevated privileges: If the library search path and dependencies are correct, try running MATLAB with elevated privileges (using sudo) to see if this resolves the issue.
5. You need to import the library before you can call its functions:
import clib.force_camera_restart.* %will import everything from your library.
Hope that helps.
  1 comentario
Matt el 6 de Jun. de 2023
Editada: Matt el 6 de Jun. de 2023
Hi Suman,
1. Using the ldd command I see that all dependencies are resolved. (I see all of the dependencies memory adress as you can see).
>> system(' ldd /home/iscat/test/ ' ) (0x00007ffc97347000) => /usr/local/MATLAB/R2022a/sys/os/glnxa64/ (0x00007f794723e000) => /usr/lib/x86_64-linux-gnu/ (0x00007f7947033000) => /usr/lib/x86_64-linux-gnu/ (0x00007f7946ee4000)
/lib64/ (0x00007f794741b000) => /usr/local/MATLAB/R2022a/sys/os/glnxa64/ (0x00007f7946eca000)
2. The libraires are on the rpath and on the LD_LIBRARY_PATH, I checked as you can see in the code I posted.
3. I tryed as you showned to add the librairires explicitely to the LD_LIBRARY_PATH, it does not change the error message.
4. I get an error message about my matlab licence when I sudo matlab. edit : i managed to run matlan as root but I still get the same error.
5. Good to know ! I missed this step. So I can import the library, no warning or error, but when I call the lib I still get the same error message.
Two additional remarks :
  • During the clibgen.generateLibraryDefinition() step I get the following warning
Warning: Some C++ language constructs in the files for generating interface file are not
supported and not imported.
Deleted file '/home/iscat/test/defineforce_camera_restart.mlx'.
Deleted file '/home/iscat/test/defineforce_camera_restart.m'.
C++ compiler set to 'g++'.
Definition file defineforce_camera_restart.mlx contains definitions for 1 constructs supported by MATLAB.
- 1 construct(s) are fully defined.
To build the interface, call build(defineforce_camera_restart).
When I have a look at the defineforce_camera_restart.mlx definition generated I see no commented area. So I came to the conclusion that despite the warning everything is fine, but maybe not ?
  • I would be expecting the ldd function to show more dependencies, and especially those located in '/opt/EVT/eSDK/lib' that I know to be necessary. If I write a cpp program, called test_lib, that calls the unique function from my lib force_camera_restart, and check for test_lib dependendies ( ldd test_lib ) I find way more dependencies for it then for

Iniciar sesión para comentar.

Javier Ros
Javier Ros el 20 de Jun. de 2023
I'm in a similar situation, in,linux as well.
I have a library that I compile myself that interfaces a system wide library (/usr/lib/libginac)
Everything goes OK, but same error when trying to call the functions defined in my library.
If I do system('ldd ./ginac_matlab/') or even
system('ldd ./ginac_matlab/')
Everithing seems OK.
But if I add the modifier -d
>> system('ldd -d ./ginac_matlab/') (0x00007ffd98598000) => /home/jros/Sync/Programming_Tricks/ginac_matlab/ (0x00007f1ccb839000) => /home/jros/MATLAB/R2023a/bin/glnxa64/ (0x00007f1ccb81b000) => /home/jros/MATLAB/R2023a/sys/os/glnxa64/ (0x00007f1ccb645000) => /home/jros/MATLAB/R2023a/sys/os/glnxa64/ (0x00007f1ccb62b000) => /lib/x86_64-linux-gnu/ (0x00007f1ccb419000) => /lib/x86_64-linux-gnu/ (0x00007f1ccb2ca000) => /lib/x86_64-linux-gnu/ (0x00007f1ccb2c4000) => /lib/x86_64-linux-gnu/ (0x00007f1ccb2a1000)
/lib64/ (0x00007f1ccb873000)
undefined symbol: _ZN5GiNaC21spinmetric_unarchiverD1Ev (/home/jros/Sync/Programming_Tricks/ginac_matlab/
undefined symbol: _ZN5GiNaC21diracgamma_unarchiverD1Ev (/home/jros/Sync/Programming_Tricks/ginac_matlab/
undefined symbol: _ZN5GiNaC17su3one_unarchiverD1Ev (/home/jros/Sync/Programming_Tricks/ginac_matlab/
undefined sym
I get undefined symbols.
same thing for my library ./ginac_matlab/
These symbold are from GiNaC, the library I'm calling from the library I'm interfacing.
The thing is that I can compile an executable calling my library, but I need to add -lginac to be able to compile it.
If I remove any reference to GiNaC in my library, everything goes OK in MATLAB, proving that my aproach is correct.
I have explicitly added /usr/lib to the LD_LIBRAY_PATH (just in case), restarted matlab recompiles and put everything in the MATLAB PATH.
So maybe in your case, if you add -d modeifier to ldd you will see undefined symbols as well.
It seems that the /usr/lib/ dependence in the interface library made by matlab is not properly included. library generated by clib.
So, I suppose that the answer it to be able to tellto clib, to include a reference to libginac.
But how I do this?.
By the way, my actual invocation is:
I tried diferent combiantions of
parameter without much success.
Anyway, have you had any progress on this?.
MATLAB Support Team. PLEASE: it would be nice to have concrete reproducible example in which we define an interface to our own library, that in turn uses other (system wide or not) library. Idealy for differetn oerating systems (including Linux).
  1 comentario
Javier Ros
Javier Ros el 20 de Jun. de 2023
I've found a solution.
This did the trick for me:
And, yes it was about how to specify the set of Libraries it was dependent on.

Iniciar sesión para comentar.


Más información sobre Build MATLAB Interface to C++ Library en Help Center y File Exchange.





Community Treasure Hunt

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

Start Hunting!

Translated by