Windows 64 + Matlab 64 MEX hybrid programming preliminary, Matlab MEX

Source: Internet
Author: User

Windows 64 + Matlab 64 MEX hybrid programming preliminary, Matlab MEX

  • Description
  • Matlab hybrid programming
    • Download and install the Compiler
      • Microsoft Windows SDK 71CC
      • TDM-GCCgccg
    • Select Compiler
    • Compile a ccpp File
      • Introduction to the Mexican Function
    • Compile multiple ccpp files
  • ProblemSolution
    • The compiler or SDK cannot be found.
      • Problem
      • Solution
      • Note:
    • Error C2143 syntax error
      • Problem
      • Solution

Description
  • Matlab version: Matlab R2014a 64-bit
  • Operating system version: Windows 8.1 64-bit
  • Compiler:Microsoft Windows sdks 7.1(C/C ++) andGNU Compiler(CygWin, MinGW, MinGW-64, TMD-64, etc., here choose TDM-64)
Download and install the compiler Microsoft Windows SDK 7.1 (C/C ++) using Matlab hybrid programming)

Click Here Microsoft Windows SDK for Windows 7 and. NET Framework 4 (ISO) Open the download page, which is an image file. Select 64-bit. The file size is 500 MB +, and the installation takes up to 2 GB of disk space.

After the download is complete, follow the prompts to install it. If the installation fails, refer:Problem & Solution.

TDM-GCC (gcc/g +++)

Click tmd-gcc to go To the download page. Select the 64-bit version, which is 40 M +. All the installations occupy 400 M + disk space.

Based on previous experience, it is recommended that the installation path do not contain spaces in Chinese.

After the installation is complete,Start> RUN> cmdOpen the Windows command window and entergcc -v, As shown in, the installation information is displayed, indicating that the installation is successful:

Last battle:
Because Matlab does not support the GNU compiler, you have to do something. The following method comes from Bogdan in Using GCC (MinGW) as MATLAB's MEX compiler:

The following code downloads the mexopts. bat file code from the Bogdan homepage. You need to make a few changes when using it.

Find in the following code:set MINGWPATH=p:\mingw64In this Code, the pathp:\mingw64Replace it with the installation directory of your TMD-GCC-64, as my is:E:\devtools\TDM-GCC-64, And then save and paste the mopts. bat fileCopyGo to the following path on your computer (copy the following path address, paste it in the address bar of the Resource Manager window, and press Enter ):

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

The following code is used:

@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 do any fancy batch stuff in here. There are some::       undocumented special vars you can set here that will trigger MEX::       to do fancy stuff.:: You 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 you know what 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 two examples below will cause gcc to output the full path to some:: static libraries so you 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=...:: You can have 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=...:: You can change these if you really need to.set COMPILER=g++set COMPFLAGS=-c -I"%MATLAB%\extern\include" -DMATLAB_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 LINKFLAGSPOST=@%TEMP%\mexstaticlibsset NAME_OUTPUT=-o "%OUTDIR%%MEX_NAME%%MEX_EXT%":: EXAMPLES:: ========:: You can compile simple files using "mex file.cpp". To support more than 2^32 elements, use:: "mex -largeArrayDims file.cpp" ... use this by default, unless you know what you'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 the compiler to compile the c/cpp File

There are two differences with the compiling of common C files:

The following is an example of summation:

# Include "mex. h "// use the header file that must be included in the MEX file // The C function that executes the specific work double add (double x, double y) {return x + y ;} // MEX file interface function void Mexico function (int nlhs, mxArray * plhs [], int nrhs, const mxArray * prhs []) {double * z; double x, 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 Inputmex add.cppThe running result is as follows:

Introduction to the Mexican Function

The following table lists the functions of the mexFunction interface:

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

It can be seen that there is no return value, which is actually passed through the pointer array plhs. It has a totalFour ParametersThat is, the output parameter and number on the left and the input parameter and number on the right. The specific parameter meanings are as follows:

Parameters Meaning Full name Type
Nlhs Number of output parameters on the left Number of left-hand side INTEGER (int)
Plhs Pointer to the output parameter Pointer of left-hand side Pointer Array
Nrhs Number of input parameters on the right Number of right-hand side INTEGER (int)
Prhs Pointer to input parameter Pointer of right-hand side Pointer Array

For details, refer:

Compile multiple c/cpp files

HypothesisA.cCallB.c,B.cCallC.c
You can use the following command to compile:

mex C.cmex B.cmex A.c

Or

mex A.c B.c C.c
Problem & Solution cannot find the compiler or SDKProblem

Tip: "The supported compiler or SDK is not found", for example:

Solution

Install: Microsoft Windows SDK for Windows 7 and. NET Framework 4 (ISO). On the download page, you can select three ISO files.Amd64.

Name Version
GRMSDK_EN_DVD.iso X86
GRMSDKIAI_EN_DVD.iso Itanium
GRMSDKX_EN_DVD.iso Amd64
Note:

If the following error occurs when installing Microsoft Windows SDK for Windows 7 and. NET Framework 4 (ISO:

A problem occurred while installing selected Windows SDK components.

Installation of the "Microsoft Windows SDK for Windows 7" product has reported the following error: Please refer to Samples \ Setup \ HTML \ ConfigDetails.htm document for further information.

Please attempt to resolve the problem and then start Windows SDK setup again. If you continue to have 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.

For example:

Solution:
This problem may occur because your PC has been installed:Microsoft Visual C ++ 2010When installing Microsoft Windows SDK for Windows 7 and. NET Framework 4 (ISO ),Microsoft Visual C ++ 2010For example, you can uninstall the installed,Note that both 32-bit and 64-bit data must be uninstalled..

After the uninstallation is complete, you can re-install it !!!

Error C2143: syntax errorProblem

Error message: error C2143: syntax error: missing '; 'before' type', as shown in:

Solution

This is probably because you are using the VC compiler to compile the code written in gnu c language. The C standards in the two compilers are different.

One way is to append the file name.c, Changed.cpp, Re-compile, 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 the TDM-GCC (gcc/g ++)

  • Description
  • Matlab hybrid programming
    • Download and install the Compiler
      • Microsoft Windows SDK 71CC
      • TDM-GCCgccg
    • Select Compiler
    • Compile a ccpp File
      • Introduction to the Mexican Function
    • Compile multiple ccpp files
  • ProblemSolution
    • The compiler or SDK cannot be found.
      • Problem
      • Solution
      • Note:
    • Error C2143 syntax error
      • Problem
      • Solution

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.