In order to call external C/C++ library functions, it is generally recommended to use the mex built-in function to compile the source code into a mex executable file, but the disadvantage is that it is not easy to generate C/C++ code in turn if there are other matlab code mixed in. This example is just to show how to use codegen two-way through this process, not limited to the example itself, is a general method, another advantage is not limited to the code generation of various restrictions, fully integrated with C/C++ programming.
- MATLAB®
- Matlab Coder toolbox™
- Supported and Compatible Compilers
- Your External C/C++ Library(This example uses OpenCV as an example)
In the command window,configure your complier.
mex -setup C++
Prepare your C/C++ code and MATLAB entry-point function.Prepare the implementation of the updateBuildInfo(buildInfo, buildContext)
member function, rewritten according to the path of the external library. (header files and library files).
Generate or compile the files you want with the codegen
command.
For example:
imagePath = "./test_480x640x3.jpg";
input1 = coder.typeof(imagePath);
input1.StringLength=inf;
codegen -config:mex imread_opencv -args {input1} -lang:c++ -report