C + + call MATLAB programming __linux

Source: Internet
Author: User
Tags visual compiler win32

C + + call Matlab, is really the last resort. Original link:

http://kylen314.blog.com/2012/12/11/matlab_for_cplusplus/This is a very good blog, only a small error occurred.


Content: There are minor modifications.

Bad news, bad news .... Recently itself busy to vomit, what the boss of the monographic study, what face sex recognition, what electric power decomposition algorithm research, itself quickly collapse, then yesterday suddenly said 1.5 before the patent applied for a formula has a problem, I studied for half a day also do not know 1.5 ago why I would write that formula "embarrassed", And then suddenly yesterday, my brother said, the boss asked me to build a platform for a system this week, I go, that system because to get data from the digital Communication analyzer, so with the NI Company's GPIB acquisition card, the driver file provides C + + interface, and then the data i use MATLAB processing, Must be duly completed, the result is now to build a system means that collection and processing to inherit together, I am a, I matlab inside call so many calculation method unknown function, how to transplant to C + + Ah, in fact, the main problem or time is not enough, time enough to say anything ...

So I have to quickly study C + + How to invoke the function of matlab ... Research, the method seems a lot, such as using the Matlab Engine, it can be called in C + + directly using the function of Matlab, but for me this system, it is best to directly call the entire function of MATLAB, although the MATLAB Engine I do not know can, did not delve into, and then studied another method. Summary when the memo. System Configuration:

environment variable path in the Bin directory to add MATLAB: that is, plus x:/xxxxxx/matlab/r2009a/bin/win32

Although, I found as if the installation of MATLAB has been automatically added ... C + + compiler configuration:

Either with VS or VC6.0, the VC6 is open in Tool→options→directories,vs to open Tool→options→project and solution→vc++ directories, Then add the following items: Include files inside add x:/xxxxxx/matlab/r2009a/extern/include include files inside add x:/xxxxxx/matlab/r2009a/extern/ Include/win32 Library files inside add X:/xxxxxx/matlab/r2009a/extern/lib/win32/microsoft

So the C + + end configuration is complete. matlab configuration: command line Input Mbuild-setup, the first selection of Y, the second select your compiler, vs or VC6.0 corresponding number, generally 2, the third select the y command line input mex-setup, the option and the first step. Configure the above things, it can be ~ C + + call Matlab Use method: first write a correct matlab of the M file and save, "amount, not correct I have not tried." "For example is Functin [a b] = function_name (c,d) MATLAB command line input deploytool in deployment tool upper left corner Click Create a new deployment project Select Matl C + + Share Library in AB compiler and enter your project name and path below to add files to the M file you wrote before and click Build Project to complete the compilation. There are two folders under the path of project you selected, Distrib and src,src, open distrib. There are three files that you want to use,. dll and. h files Build your C + + project and add the three files from the previous step to your C + + In the project can be. C + + code writing:

At the beginning of the file, you add a Lib file with the following code:

#pragma comment (lib, "Libdflapack.lib")  #这个文件在2010a里面找不到, to remove
#pragma comment (lib, "Libemlrt.lib")
# pragma comment (lib, "Libeng.lib")
#pragma comment (lib, "Libfixedpoint.lib")
#pragma comment (lib, "Libmat.lib") ")
#pragma comment (lib," Libmex.lib ")
#pragma comment (lib," Libmwlapack.lib ")
#pragma comment (lib," Libmwservices.lib ")
#pragma comment (lib," Libmx.lib ")
#pragma comment (lib," Libut.lib ")
#pragma Comment (lib, "Mclcommain.lib")
#pragma comment (lib, "Mclmcr.lib")
#pragma comment (lib, "Mclmcrrt.lib")

Note that the last three, the version of Matlab is said to be Mclcommain.lib, before the version is Mclco.lib, no research, out of the question you go to add before the Lib path to see it.

Then add your own lib file that you just generated

#pragma comment (lib, "XXX.lib")

Then you must include your own header file.

#include "XXX.h"

As for how to use those functions, a simple explanation, first of all, the following mentioned XXX are you in the fifth step above written in the project name, that is, above the lib and. h xxx.

To initialize the call before calling the function:

Xxxinitialize ();

After testing, the function above is running extremely long ...

Of course, at the end of the call, or at the tail of the program to invoke:

Xxxterminate ();

Call function, is your m file function name, yes, well known m file file name and M file inside the same function name, but the M file function name does not have to be set with the project here, such as the function named MyFunction (), and the project named Test, So what you're generating is test.h,test.dll,test.lib, but you're using myfunction when you call in C + + code.

If your myfunction does not have input and output parameters, then the call will be written directly myfunction (), if there are input and output parameters, the function call format is:

MyFunction (int nargout, mwarray& matrix...,const mwarray& N1 ...);

Direct understanding is the number of output parameters Nargout

Then nargout output parameters, the data type is Mwarray, a little common sense people just see the function call inside & is basically output parameters ~

Then is each input parameter, the data type is also Mwarray, see Const to know should be input parameter ~

What. You ask me why not write the number of input parameters, a good review of the MATLAB function to ... about Mwarray:

This is the MATLAB generated DLL interface data types, this look at the function call above you should be aware of this, defined in the MATLAB installation directory under the extern/include/under the Mclcppclass.h, interested in their own to see ~

Although there is a data type is Mxarray, but not recommended, and Mwarray itself is the encapsulation of Mxarray. It is not recommended because its memory management is not good, and use a pointer, and mwarray you can directly as an object to operate. "If you want to generate a DLL with an interface of Mxarray, you can also use the statement in the MATLAB command line, but you have said it." Not recommended. 】

MATLAB does not specifically indicate that the data types are double, but in this definition of a mwarray data to describe the type, define the method as follows:

Mwarray matrix (2,2,mxdouble_class);
Mwarray Matri2 (1,m,mxint8_class);

If you want to assign a value or read it, the following methods are:

Mwarray matrix (1,4,mxdouble_class);
Double a[] = {1,2,3,4};
Matrix. SetData (a,4);

Cout<<matrix (1,2) <<endl;

Double *b = new Double[4];
Matrix. GetData (b,4);
cout<<b[0];
problems that may arise:

C + + call Matlab generated lib may appear runtime error R6034 errors, although you can turn off this error, the program can continue to run, completely not in the way ~ but also very annoying, the official said is such a son: Summary

On Windows (XP or Vista) error R6034 are thrown by C runtime. Description

On Windows (XP or Vista) error R6034 was thrown by the C runtime. This is happens when executing standalone applications, C or C + + user applications built using C or C + + shared libraries, MAT LAB Builder JA components, and Matab Builder NE components.

In this case, the user has a PATH directory containing msvcr80.dll. The error is thrown because the version of tbbmalloc.dll in mcrinstallroot|matlabroot\bin\arch  Does not contain a manifest. Workaround

For standalone executables and C + + shared libraries using a supported Microsoft Visual compiler:download the Zip files In the attachment. New option files have been provided for both 32-bit and 64-bit Windows. These new options files create executables with manifests which the operating system uses to locate runtime shared Librari Es in the Windows side X side area. Applications created using the attached options files can be distributed to deployed machines the no change to the TA Rget MCR Install area. Back up matlabroot\bin\arch\mbuildopts by executing the following at the system command prompt: cd  matlabroot\bin\arch & mkdir Mbuildopts.bak & CD mbuildopts & copy *.* ... \mbuildopts.bak where matlabroot is the MATLAB installation directory which can is found by Typing:matlab Root at the MATLAB command prompt and arch is the platform. Unzip Either options_win32.zip or options_win64.zip to the root of your MATLAB installation directory. Execute mbuild-setup. Rebuild your application using the downloaded options files which you-enabled by Executingmbuild-setup.

For standalone executables and C + + shared libraries Using Lcc-win32 compiler:create a manifest file named Ame.exe.manifest (if creating a executable) orapplication_name.dll.manifest (if creating a shared library) with the Follo Wing content:

<?xml version= ' 1.0 ' encoding= ' UTF-8 ' standalone= ' yes '?> <assembly ' xmlns=
: Asm.v1 ' manifestversion= ' 1.0 ' >
<dependency>
<dependentAssembly>
<assemblyidentity Type= ' Win32 ' Name= ' Microsoft.VC80.CRT ' version= ' 8.0.50727.762 ' processorarchitecture= ' x86 ' ' publickeytoken= ' 1fc8b3b9a1e18e3b '/>
</dependentAssembly>
</dependency>
</assembly>

You can create this manifest file by copying the above content into a text file. Place the manifest in the same folder as the executable. For example, if you are have a application called matrixdriver.exe, you would create a manifest file calledmatrixdriver. Exe.manifest and put it in the same folder as the application.

For Matlab Builder for JA and Matlab Builder for NE, perform the following on your development Machine:change folder To Matlabroot\bin\arch Backup Tbbmalloc.dll and rename it to Tbbmalloc.dll.bak Execute the following command at a Visual Studio Command prompt:mt.exe-inputresource:libut.dll #2-outputresource:tbbmalloc.dll #2 Tbbmalloc.dll now contains a Manifest, and must is distributed to the any machine in which you have encountered error R6034 when executing a MATLAB Builder JA or MATLAB Builder NE component. You are should place the modified Tbbmalloc.dll in Mcrroot\bin\arch where Mcrroot is the MATLAB Compiler Runtime (MCR) Install ation directory. Fix

For r2009b, a new version of the Tbbmalloc.dll is distributed that contains a manifest. Attachments options_win64.zip options_win32.zip

But this problem does not necessarily happen, I ran on my computer when the problem occurred, but in another installed on the computer on the MATLAB did not appear, and perhaps the same as online rumors, reloading it can be. But the good trouble to say ...

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.