A preliminary study of Windows + Matlab mixed-Mex hybrid programming

Source: Internet
Author: User

    • Description
    • MATLAB Mixed programming
      • Download and install the compiler
        • Microsoft Windows SDK 71CC
        • Tdm-gccgccg
      • Select compiler
      • Writing ccpp files
        • Mexfunction Function Introduction
      • Compiling multiple Ccpp files
    • Problemsolution
      • The compiler or SDK could not be found
        • Problem
        • Solution
        • Attention
      • Error C2143 syntax error
        • Problem
        • Solution

Description
    • MATLAB version: Matlab r2014a 64-bit
    • Operating system version: Windows 8.1 64-bit
    • Compiler:Microsoft Windows SDK 7.1(c + +) and GNU compilers (with Cygwin, MinGW, MinGW-64, TMD-64, and so on, choose TDM-64 here)
Matlab Hybrid programming download and install compiler for Microsoft Windows SDK 7.1 (c + +)

Click this Microsoft Windows SDK for Windows 7 and. NET Framework 4 (ISO) to open the download page, which is the image file, select the 64-bit version, the file size 500m+, and all installation takes up nearly 2G of disk.

After the download is complete, follow the prompts to install, if there is a problem, installation failure, reference:problem&solution section.

TDM-GCC (gcc/g++)

Click this tmd-gcc to open the download page, select the 64-bit version, 40m+, and all installations occupy 400m+ disk space.

Based on previous experience, the installation path is best not to have a space in Chinese.

After the installation is complete, 开始 -> 运行 -> cmd Open the Windows Command window, enter gcc -v , as shown in, display the installation information stating that the installation was successful:

The final battle:
Because MATLAB does not support the GNU compiler, there is something to do, the following method comes from the solution of the Bogdan in the Using GCC (MinGW) as MATLAB ' s MEX compiler:

The following code is the Mexopts.bat file code downloaded from the Bogdan home page, which requires a little bit of change when used.

Find in the following code: set MINGWPATH=p:\mingw64 This code, replace the path to p:\mingw64 your tmd-gcc-64 's installation directory, as mine is: E:\devtools\TDM-GCC-64 , then save and copy the Mexopts.bat file To the following path to the computer (copy the path address below, paste into the Explorer window address bar, enter):

%USERPROFILE%\AppData\Roaming\Mathworks\MATLAB\R2014a\

Here's the code:

@echo off: : Note:This is actually not a proper. bat file executed by Windows. MEX:: Parses it and only understands a very reduced set of commands::: "Set" and "REM" Apparently, everything else is ignored (behaves as:: "REM"), so don ' t does any fancy batch St Uff in here. There is some:: Undocumented special VARs can set here, would trigger MEX:: To do fancy stuff.:: Can use MinGW64 builds (Win32 threads + SEH unwinding) from here::: http://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Personal%20Builds/ mingw-builds/:: Tested with the following::: http://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Personal%20Builds/ mingw-builds/4.9.1/threads-win32/seh/x86_64-4.9.1-release-win32-seh-rt_v3-rev1.7z:: Set this to your Mingw64 top folder, where you extracted the Aboveset mingwpath=p:\mingw64:: Leave these alone unless Y ou know what do you ' re doing.set path=%mingwpath%\bin;%path%set prelink_cmds=echo.>%temp%\mexstaticlibs:: You can have MEX run some commands before calling the linker.:: The examples below would cause GCC to output the ful L path to some:: Static libraries so can link statically to them (see the:: Lingflagspost special var below). You can set any command here, However.rem set PreLink_cmds1=gcc-print-file-name=libwinpthread.a >>%temp%\mexstaticlibsrem set PRELINK_CMDS2=GCC-PRINT-FILE-NAME=LIBQUADMATH.A >>%temp%\mexstaticlibsrem Set PreLink_cmds3= ...:: Can has MEX run some commands also after calling the linker:: (e.g. UPX compress the output. MEX) REM Set Postlink_cmds1=upx-9 "%outdir%%mex_Name%%mex_ext% "rem Set POSTLINK_Cmds2=:: Can change these if you really need to.set compiler=g++set compflags=-c-i "%matlab%\extern\include"-DMATL Ab_mex_Fileset Optimflags=-o3-funroll-loops-dndebugset Debugflags=-gset NAME_object=-oSet Linker=g++set linkflags=-shared-static-libstdc++-static-libgcc-l "%matlab%\bin\win64"-L "%MATLAB%\extern\lib\ Win64\microsoft "-lmex-lmx-leng-lmat-lmwlapack-lmwblasset [email protected]%temp%\mexstaticlibsset NAME_output=-o "%outdir%%mex_Name%%mex_ext% ":: EXAMPLES:: ========:: Can compile simple files using "Mex File.cpp". To support more than 2^32 elements, use:: "Mex-largearraydims file.cpp" ... use the this by default, unless you know ' re doing.:: To-add include Dirs, Lib dirs, or compile/link flags, do::: Mex compflags= "$COMPFLAGS-std=gnu99-ix:/include/dir"linkflags= "$LINKFLAGS-lx:/libs/dir-lmylib"-largearraydims file.cpp
Select compiler
    1. View available compilers: Using the mex -setup view selectable compiler, if you do not have to install it yourself, if you have installed and installed only the compiler for Microsoft Windows SDK 7.1 (c + +), the following results are displayed:

    2. Select compiler: Use mex -setup lang the Select compiler, such as the mex -setup C++ C + + compiler, such as:

    3. If you also configure the compiler for tdm-gcc-64 bits as described in TDM-GCC (gcc/g++) above, you will find the results as follows:

      At first I thought I could not use it, but I tried to compile it.

      DT.C file repeatedly defined infinity, commented out, there is no hint!

Writing c/cpp files

There are two differences in the writing of regular C files:

    1. Contains the mex.h header file, i.e.: #include "mex.h" ;

    2. Write the mexfunction function.

The following is an example of a summation:

#include "mex.h" //the header file that must be included with the Mex file //C function to perform specific workDoubleAddDoubleXDoubleY) {returnx + y;}//Mex file interface functionsvoidMexfunction (intNlhs,mxarray *plhs[],intNRHS,ConstMxarray *prhs[]) {Double*z;Doublex, y; plhs[0] = Mxcreatedoublematrix (1,1, mxreal); z = MXGETPR (plhs[0]); x = * (MXGETPR (prhs[0])); y = * (MXGETPR (prhs[1])); *z = Add (x, y);}

MATLAB Command Window input mex add.cpp , running results such as:

Mexfunction Function Introduction

The Mexfunction function interface is as follows:

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

Visible, there is no return value, in fact, is passed through the pointer array plhs. It has four parameters , that is, the left output parameters and number and the right input parameters and number, the specific parameters are as follows:

Parameters meaning English Full name type
Nlhs Number of left output parameters Number of left-hand side Integral type (int)
Plhs Pointer to output parameter Pointer of left-hand side Array of pointers
Nrhs Number of input parameters to the right Number of right-hand side Integral type (int)
Prhs Pointer to input parameter Pointer of right-hand side Array of pointers

Detailed reference:

    1. MATLAB Reference-mex Library

    2. Interface Specification for mexfunction functions in MATLAB (reprint)

    3. Application programming interface of MATLAB and C language Program

    4. MATLAB and C/+ + mixed programming –mex

Compiling multiple C/cpp files

Assuming A.c B.c the call, B.c call C.c the
You can compile with the following command:

mex C.cmex B.cmex A.c

Or

mex A.c B.c C.c
Problem&solution cannot find the compiler or Sdkproblem

Tip: "No supported compilers or SDKs found", such as:

Solution

As prompted by Matlab, install: Microsoft Windows SDK for Windows 7 and. NET Framework 4 (ISO), on the download page, there are three ISO files to choose from, as the following table selects the third, i.e. AMD64 .

name version
Grmsdk_en_dvd.iso x86
Grmsdkiai_en_dvd.iso Itanium
Grmsdkx_en_dvd.iso Amd64
Attention

If you install the Microsoft Windows SDK for Windows 7 and. NET Framework 4 (ISO), the following error occurs:

A problem occurred while installing selected the Windows SDK components.

Installation of the "Microsoft Windows SDK for Windows 7" product have reported the following error:please refer to Sample S\setup\html\configdetails.htm document for further information.

Attempt to resolve the problem and then start Windows SDK setup again. If you continue to has problems with this issue, please visit the SDK Team support page at Http://go.microsoft.com/fwlink /? linkid=130245.

Click the View log button to review the installation log.
To exit, click Finish.

Such as:

Workaround :
This problem occurs most likely that your PC is already installed:Microsoft Visual C + +, while installing Microsoft Windows SDK for Windows 7 and. NET Framework 4 (ISO) When the default option is to install Microsoft Visual C + +, such as, caused by a conflict, you can uninstall already installed, Note that both 32 and 64 bits are to be uninstalled .

Uninstall complete after the new installation can!!!

Error C2143:syntax Errorproblem

Hint Error: C2143:syntax error:missing '; ' Before ' type ', as shown:

Solution

This behavior, it is likely that you are using the VC compiler compiled GNU C language code, two compilers in the C standard is not the same.

One way is to change the name of the file suffix to recompile, .c .cpp but there may be other errors.

If the source file is GNU C code, it is recommended to install the appropriate compiler, and then compile, refer to TDM-GCC (gcc/g++)

    • Description
    • MATLAB Mixed programming
      • Download and install the compiler
        • Microsoft Windows SDK 71CC
        • Tdm-gccgccg
      • Select compiler
      • Writing ccpp files
        • Mexfunction Function Introduction
      • Compiling multiple Ccpp files
    • Problemsolution
      • The compiler or SDK could not be found
        • Problem
        • Solution
        • Attention
      • Error C2143 syntax error
        • Problem
        • Solution

A preliminary study of Windows + Matlab mixed-Mex hybrid programming

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.