C ++ calls the matlab function and calls the matlab function

Source: Internet
Author: User

C ++ calls the matlab function and calls the matlab function

C ++ and matlab Mixed Programming -- C ++ calls MATLAB Functions

I recently engaged in an MFC-related project, requiring that the algorithm implemented by Matlab be used to create a small application through the application of MFC. Some of them require the maximum value of a multivariate function. Through the network, I found two C ++ optimization libraries, dlib and MIDACO_Project. However, the application methods of the optimization functions in these two libraries cannot be integrated into the MFC programming framework, therefore, we thought of using matlab and C ++ mixed programming to implement related computing. I checked a lot of information on the network to solve the problem. Next I will summarize the content of the previous materials.

1. Compile matlab Functions

In the matlab command line Input window, enter the "mex-setup" command to open the configuration options for mex, according to the configuration on your computer, the lcc-win32 compiler and other C compilers that come with matlab are displayed. My computer is 32-bit, the matlab version is R2014a, and the c compiler is the Visual studio2013 compiler. As shown in, two compilers on my computer are displayed. You can click here to select the "Microsoft Visual C ++ 2013 Professional (c)" option, in some versions of matlab, you will be prompted to enter characters in the command line for selection. In either case, other compiler options cannot be displayed in matlab: 1. If matlab is installed before visual studio, enter the "mex-setup" command, the visual studio compiler cannot be displayed in matlab. A simple solution is to reinstall visual studio to ensure that it is installed on the computer after matlab; 2. The current matlab version does not support compilers on the computer. For example, the matlab version does not support VS2015. There are various versions of matlab support for compilers on this website. Download the corresponding compilers according to the instructions on the website.

 

Next, we need to create the function to be compiled in matlab. Many examples on the website are simple functions, such as the sum of two numbers, I once thought that I could only compile some simple functions. As shown in the function I wrote, the content of the function is complex. The @ keyword in matlab is used to obtain the function, and many other functions are called in the objrmse function, put all m files called by this function in the current directory.

In the command line Input window, enter "mcc-W cpplib: WPCtriForTwo-T link: lib WPCtriForTwo. m ", where the previous WPCtriForTwo is the generated database name, you can specify it yourself, WPCtriForTwo. m is the matlab function file to be compiled. After a while, you can compile the function and generate many compiled files in the current directory. You need to use three files: WPCtriForTwo. h, WPCtriForTwo. dll, WPCtriForTwo. lib. Copy the three files to the project. The file with the suffix dll must be placed in the debug directory of the MFC project. files with the suffix h must be added to the project, right-click the project and choose "add"> "existing items". Select the file from the file manager and put it into the project. Many functions compiled from matlab are applied in the project. In my project, create a folder named matlab functionlib to place the compiled lib file and put the WPCtriForTwo. lib file.

Ii. Configure the MFC project

After the matlab program is compiled into a C ++ program, many libraries of C ++ provided by matlab are required, these libraries and their corresponding header files are stored in the extern directory under the installation directory of MATLBA. Since not every computer is installed with matlab, I chose to copy the content to the MFC project and reference it in a relative path. Copy the include directory and lib directory in the "matlab installation directory/extern" directory to the MFC project, as shown in the red box.

 

Next, set the project reference for the above content (including those generated through compilation and copied from matlab.

Right-click the Properties window of the project and select the VC ++ directory option. You need to modify the contents including the Directory and library directory, as shown in. Apply the relative path syntax in "include directory" (Note: In VS, the relative path with the suffix vcxproj file is used) to add the include directory copied from matlab, add a ".. /include "information. Add the directory of the relevant library files in the lib directory copied from matlab in "library directory". Because the MFC project is set to 32-bit, therefore, you need to add a directory containing 32-bit libraries. lib \ win32 \ microsoft ".

Select the "linker"-> "general"-> "additional library directory", and add the relative location of the MATLAB functionlib directory to it, in this way, the library files generated by the matlab function compilation can be found during project compilation.

 

Finally, in the "linker"-> "input"-> "Additional dependency", enter the following content:

Mclmcrrt. lib
Libmx. lib
Libmat. lib
Libeng. lib
WPCtriForTwo. lib

Here, WPCtriForTwo. lib is the compiled lib file, and the rest are the lib files in matlab. In this way, the VS configuration of mixed programming is completed.

III. C ++ calls the compiled matlab function

Add the include in the file to call the function. For my project, it is # include "WPCtriForTwo. h ". You can refer to the example in this blog to learn how to call matlab functions.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.