Force MATLAB to reload library linked in mex funct

2020-06-16 01:33发布

问题:

I have a Mex-function, say myfunction.mexmaci64 (which is the correct ending on OS X).

Now, myfunction is linked against a library mylibrary.dylib. Both, mex-file and library, reside in the same folder.

Now, whenever I change something in mylibrary, MATLAB does not reload the new library version but instead uses the old one until I do restart MATLAB. That is very anoing when doing development and debugging work. Is there a way to force MATLAB to reload the library without restarting the application?

Note: It would be easy to link the library statically into the mex function. However, as I link the same library across quite a few mex-files, I would prefer to keep my single shared library to reduce compilation times and data redundancy.

Edit:

Concerning the discussion wether the clear mex helps:

[~, loaded_mexes] = inmem('-completenames'); % get canonica

returns a list with all loaded mex-files. This list does not contain the linked library, but only the mex-files itself. Using clear mex successfully empties this list, but does not unload mylibrary - running the mex function again still yields the same output as it did with the old shared library.

回答1:

To clear a library from memory I usually have excellent luck with

bdclose all;

Then if I'm really feeling militant, I'll do:

bdclose all; % clear all libraries out of memory ( supposedly )
clear all;   % clear all workspace variables, mex, etc. ( supposedly )
rehash;      % cause all .m files to be reparsed when invoked again


回答2:

Does clear mex do what you need?



回答3:

You can see what shared libraries are loaded by doing:

version('-modules')

I had success with unloading a mex file and (the shared library it depended on) by doing

version('-modules')  % test.mexa64 and test.so appear
clear test           % clear the mex file
version('-modules')  % both test.mexa64 and test.so no longer appear.


标签: matlab mex