How to load a c++ dll file into Matlab

喜你入骨 提交于 2020-02-23 09:37:09

问题


I have a C++ dll file that uses a lot of other c++ librarys (IPP, Opencv +++) that I need to load into matlab. How can I do this?

I have tried loadlibrary and mex. The load library does not work.

The mex finds the linux things (platform independent library) and tries to include them. And that does not work.

Does anyone have any good ideas?


回答1:


loadlibrary should work. I use it all the time to call functions from dlls written in C++ with C wrappers.

What errors are you getting when you try to use loadlibrary?

Make sure that the exported functions of the dll are C functions, not C++ functions. If not, then write C wrappers.

More info on exactly what you are doing when using loadlibrary would be helpful.




回答2:


As mentioned by others, you should first wrap your C++ library as a C library - mathworks used to advise not to use C++ code directly in mex (dlopening C++ core directly is complicated), maybe it is still true.

Your description is quite unclear, too: what do you mean by "mex finds the linux thing", but that does not work. Saying that it does not work is not helpful: the exact commands and error message are.




回答3:


You could go for the Java approach (since Matlab runs on a JRE and can access Java objects/methods -- just be aware that the Matlab JRE is not as up-to-date as the latest JRE, the one I'm running uses Java 1.5) and use JNA to access your DLL.


Or, if you wrote the top-level DLL, you could go for the COM/ActiveX approach.

I've had good success architecting the interface to my C++ functions as COM/ActiveX libraries -- you don't have to bother with that .h stuff.

See the External Interfaces guide on COM clients, particularly the part about managing/converting data.

It would be extra work to add the COM/ActiveX layer, but would make your library more portable within the Windows world and probably more easily used in MATLAB.


If you have a lot of function calls to your DLL, the COM/ActiveX approach might be faster (not sure), but otherwise I think the JNA approach would be easier.



来源:https://stackoverflow.com/questions/565371/how-to-load-a-c-dll-file-into-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!