English 中文(简体)
将C++图书馆转至MATLAB肉类
原标题:Converting a C++ library into MATLAB mex

I have a big C++ code and I want to integrate this into MATLAB so that I can use it inside my matlab code. If it was a single code making its mex file would have been the best option. But since now it s a code that needs to be compiled and built in order to run, I don t know how can I use the functions in this code.
Is making mex files for the whole code the only option or is there any other workaround? Also I would like some help on how can I make mex files for the whole code and then build it.

为了了解更多情况,我试图将这部法律纳入以下网址:http://graphics.stanford.du/Projects/drf/densecrf_v_2_2.zip”rel=“noestlow”http://graphics.stanford. . .du/Projects/drf/densecrf_v_2.zip 。 感谢你们!

最佳回答

首先,需要汇编图书馆(无论是静态的还是动态的链接)。 这里是我在Windows机器上采取的步骤(我有视力演播室2013年作为C++汇编者):

随后修改了以下实例文件:dense_inference.cpp,使之成为MEX-Function。 代替<代码>main功能:

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
..
}

我们不是在<代码>argc//argv上收到论点,而是从输入<代码>mxArray<>/code>中获取参数。 类似:

if (nrhs<3 || nlhs>0)
    mexErrMsgIdAndTxt("mex:error", "Wrong number of arguments");

if (!mxIsChar(prhs[0]) || !mxIsChar(prhs[1]) || !mxIsChar(prhs[2]))
    mexErrMsgIdAndTxt("mex:error", "Expects string arguments");

char *filename = mxArrayToString(prhs[0]);
unsigned char * im = readPPM(filename, W, H );
mxFree(filename);

//... same for the other input arguments
// The example receives three arguments: input image, annotation image,
// and output image, all specified as image file names.

// also replace all error message and "return" exit points
// by using "mexErrMsgIdAndTxt" to indicate an error

Finally, we compile the modified MEX-file (place the compiled LIB in the same example folder):

>> mex -largeArrayDims dense_inference.cpp util.cpp -I. -I../include densecrf.lib

现在,我们从MATLAB内部称为MEX功能:

>> dense_inference im1.ppm anno1.ppm out.ppm

由此产生的部分形象:

“out.ppm”/

问题回答

An alternative approach can be to compile your large C++ code into a shared library (.dll or .so depending on your OS) and then load this library in Matlab using loadlibrary.
Once the library is loaded you can call each and every one of its API functions using calllib.


Example: assuming you are working in Linux environment and you have you c++ code in a file myLib.cpp with a header file myLib.h, then you can use g++ to create a shared library

$ g++ -fPic -c myLib.cpp
$ g++ -shared -o myLib.so myLib.o

现在,在马特拉布,你可以装上图书馆(假设档案和档案都符合你的要求)。

>> loadlibrary(  myLib ,  myLib.h  );




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签