How to correctly manage DLLs on Matlab path

34 views (last 30 days)
On Windows, I am building a mex function "myFcn_mex" which links to a DLL "myLib.dll" via an import library "myLib.lib". I have the following directory structure:
%MY_PROJECT_BASE%\bin\win\x86_64
myLib.dll
myLib.lib
%MY_PROJECT_BASE%\src\include
myLib.h
%MY_PROJECT%\src\matlab
myFcn.m
My Matlab code looks as follows:
function myFcn
if ~coder.target('MATLAB')
projectBase = getenv('MY_PROJECT_BASE');
includePath = [projectBase filesep 'src' filesep 'include'];
libPath = [projectBase filesep 'bin' filesep 'win' filesep 'x86_64'];
libName = 'myLib.lib';
coder.ceval('myFcn');
coder.cinclude('myLib.h');
coder.updateBuildInfo('addIncludePaths',includePath);
coder.updateBuildInfo('addLinkObjects',libName,libPath,'',true,true);
else
myFcn_mex;
end
end
In Matlab I do this:
[Line 1] >> addpath([getenv('MY_PROJECT_BASE') filesep 'bin' filesep 'win' filesep 'x86_64']);
[Line 2] >> cd(getenv('MY_PROJECT'));
[Line 3] >> codegen myFcn
[Line 4] >> myFcn
So:
  1. DLL and LIB files live in a folder which has been succesfully added to Matlab path [Line 1]
  2. Folder where dll and lib files reside is not the same where I need to create my mex function [Line 2]
  3. Codegen command successfully runs and creates the file myFcn_mex.mexw64 in my working directory [Line 3]
  4. Calling the mex function [Line 4] results in error:
...
Missing dependent shared libraries:
'myLib.dll' required by
...
The function call suceeds if I copy the DLL locally:
>> !cp %MY_PROJECT_BASE%\bin\win\x86_64\myLib.dll .\
I quite clearly remember that in the past just adding the directory where dll file resides to the Matlab path used to work. It no longer seems to be the case. I either have to have this DLL in my current directory or on the OS path, which would require me to exit Matlab, change the OS Path environmant variable, and then restart Matlab.
Questions:
  1. Is there not another way of managing a DLL? I do not want to do either of the two options I described above.
  2. How different would be the way to do that with a myLib.so file on Linux or myLib.dylib on Mac?
Thanks in advance

Accepted Answer

Prashant Nirmal
Prashant Nirmal on 23 Oct 2019
Edited: Prashant Nirmal on 23 Oct 2019
As mentioned in this link you must have dll file either on system path or in the same folder.
In case of Linux and Mac. See "-L" and "-l" flags in "mex" documentation

More Answers (1)

Nikita Visnevski
Nikita Visnevski on 23 Oct 2019
Edited: Nikita Visnevski on 23 Oct 2019
Well, mex function itself is essentially a kind of a dll (or so/dylib, depending on the platform), yet I do not have to have mex files on system path or current directory. There is something that Matlab is doing to manage mex files more robustly than any generic dll. I guess it is what it is. I was just hoping there would be a better way.
Thanks

Categories

Find more on MATLAB Coder in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!