zoukankan      html  css  js  c++  java
  • Ubuntu Linux Matlab mex

    环境说明:Ubuntu 10.04 (LTS) AMD64 + Matlab 2011b

    1. 安装gcc g++

    参考:http://stefaanlippens.net/cpp_mex_ubuntu804

    zhang@ubuntu-desktop:~$ sudo apt-get install gcc-4.3 g++-4.3

    2.  At the MATLAB Command Window prompt, type: 

    >> mex -setup

    Options files control which compiler to use, the compiler and link command
    options, and the runtime libraries to link against.

    Using the 'mex -setup' command selects an options file that is
    placed in /home/zhang/.matlab/R2011b and used by default for 'mex'. An options
    file in the current working directory or specified on the command line
    overrides the default options file in /home/zhang/.matlab/R2011b.

    To override the default options file, use the 'mex -f' command
    (see 'mex -help' for more information).

    The options files available for mex are:

    1: /usr/local/MATLAB/R2011b/bin/mexopts.sh :
    Template Options file for building gcc MEX-files


    0: Exit with no changes

    Enter the number of the compiler (0-1):
    1

    Overwrite /home/zhang/.matlab/R2011b/mexopts.sh ([y]/n)?
    y

    /usr/local/MATLAB/R2011b/bin/mexopts.sh is being copied to
    /home/zhang/.matlab/R2011b/mexopts.sh


    **************************************************************************
    Warning: The MATLAB C and Fortran API has changed to support MATLAB
    variables with more than 2^32-1 elements. In the near future
    you will be required to update your code to utilize the new
    API. You can find more information about this at:
    http://www.mathworks.com/support/solutions/en/data/1-5C27B9/?solution=1-5C27B9
    Building with the -largeArrayDims option enables the new API.
    **************************************************************************

    3. 警告

    >> mex test.c

    Warning: You are using gcc version "4.4.4-14ubuntu5)".  The version
    currently supported with MEX is "4.3.4".
    For a list of currently supported compilers see:
    http://www.mathworks.com/support/compilers/current_release/

    如果出现上述警告和错误,说明当前的Matlab版本不支持gcc 4.4。解决办法如下。

    4. 编辑文件

    zhang@ubuntu-desktop:~$ sudo gedit /usr/local/MATLAB/R2011b/bin/mexopts.sh

    mexopts.sh
    #----------------------------------------------------------------------------
    ;;
    glnxa64)
    #----------------------------------------------------------------------------
    RPATH="-Wl,-rpath-link,$TMW_ROOT/bin/$Arch"
    # StorageVersion: 1.0
    # CkeyName: GNU C
    # CkeyManufacturer: GNU
    # CkeyLanguage: C
    # CkeyVersion:
    CC='gcc-4.3'
    CFLAGS='-ansi -D_GNU_SOURCE'
    CFLAGS="$CFLAGS -fexceptions"
    CFLAGS="$CFLAGS -fPIC -fno-omit-frame-pointer -pthread"
    CLIBS="$RPATH $MLIBS -lm"
    COPTIMFLAGS='-O -DNDEBUG'
    CDEBUGFLAGS='-g'
    CLIBS="$CLIBS -lstdc++"
    #
    # C++keyName: GNU C++
    # C++keyManufacturer: GNU
    # C++keyLanguage: C++
    # C++keyVersion:
    CXX='g++-4.3'
    CXXFLAGS='-ansi -D_GNU_SOURCE'
    CXXFLAGS="$CXXFLAGS -fPIC -fno-omit-frame-pointer -pthread"
    CXXLIBS="$RPATH $MLIBS -lm"
    CXXOPTIMFLAGS='-O -DNDEBUG'
    CXXDEBUGFLAGS='-g'
    #
    # FortrankeyName: gfortran
    # FortrankeyManufacturer: GNU
    # FortrankeyLanguage: Fortran
    # FortrankeyVersion:
    #
    FC='gfortran-4.3'
    FFLAGS='-fexceptions -fbackslash'
    FFLAGS="$FFLAGS -fPIC -fno-omit-frame-pointer"
    FLIBS="$RPATH $MLIBS -lm"
    FOPTIMFLAGS='-O'
    FDEBUGFLAGS='-g'
    #
    LD="$COMPILER"
    LDEXTENSION='.mexa64'
    LDFLAGS="-pthread -shared -Wl,--version-script,$TMW_ROOT/extern/lib/$Arch/$MAPFILE -Wl,--no-undefined"
    LDOPTIMFLAGS='-O'
    LDDEBUGFLAGS='-g'
    #
    POSTLINK_CMDS=':'
    #----------------------------------------------------------------------------

    绿色为原始部分;-4.3为增加部分。

    参考:

    https://help.ubuntu.com/community/MATLAB

    http://ubuntuforums.org/showthread.php?p=7737533

    重启matlab

    5. 测试

    参见博文《C-mex实例》。 增加了写mat文件操作的.c文件如下:

    addtiontest.c
     1 #include <mex.h>
    2
    3 double addtiontest(double x, double y);
    4 double doublesize(double a, double b);
    5
    6 void mexFunction(int nlhs, mxArray *plhs[], int nrhs,const mxArray *prhs[])
    7 {
    8 double *a;
    9 double b, c;
    10
    11 /******Output******/
    12 plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
    13 a = mxGetPr(plhs[0]);
    14
    15 /******Input******/
    16 b = *(mxGetPr(prhs[0]));
    17 c = *(mxGetPr(prhs[1]));
    18 *a = addtiontest(b, c);
    19 }
    20
    21 double addtiontest(double x, double y)
    22 {
    23 double z;
    24 z = doublesize(x,y);
    25 return z;
    26 }
    doublesize.c
     1 #include <stdio.h>
    2 #include <string.h> /* For strcmp() */
    3 #include <stdlib.h> /* For EXIT_FAILURE, EXIT_SUCCESS */
    4 #include <mex.h>
    5 #include "mat.h"
    6
    7 double doublesize(double a, double b)
    8 {
    9 MATFile *pmat;
    10 const char *file = "mattest.mat";
    11
    12 mxArray *mat_data;
    13 mat_data = mxCreateDoubleMatrix(1,1,mxREAL);
    14 memcpy((void *)(mxGetPr(mat_data)), (void *) &a, sizeof(&a));
    15 printf("\n%f\n",*mxGetPr(mat_data));
    16
    17 int status, num;
    18 pmat = matOpen(file, "w4");
    19 for(num=1; num<4; num++) {
    20 char pnum[] = {(char)(num+48), '\0'};
    21 char pLD[13] = "LocalDouble";
    22 status = matPutVariable(pmat, strcat(pLD,pnum), mat_data);
    23 }
    24 if (matClose(pmat) != 0) {
    25 printf("Error closing file %s\n",file);
    26 return(EXIT_FAILURE);
    27 }
    28
    29 mxDestroyArray(mat_data);
    30
    31 double c;
    32 c = 2*(a+b)+100;
    33 return c;
    34
    35 }

    在matlab command window中操作如下:

    >> mex addtiontest.c doublesize.c
    
    >> addtiontest(7.5,15)
    
    7.500000
    
    ans =
    
       145
    
    >> load('mattest.mat')
    
  • 相关阅读:
    《计算机网络 自顶向下方法》整理(二)应用层
    《计算机网络 自顶向下方法》整理(一)计算机网络和因特网
    《深入理解C#》整理10-使用async/await进行异步编程
    STM32 HAL库之串口详细篇
    .Net微服务实战之负载均衡(下)
    面试官:来,年轻人!请手撸5种常见限流算法!
    工具用的好,下班回家早!iTerm2使用技巧指北!
    Java编程规范(命名规则)
    Go语言快速安装手册
    Educational Codeforces Round 6 620E. New Year Tree(DFS序+线段树)
  • 原文地址:https://www.cnblogs.com/xfzhang/p/2233486.html
Copyright © 2011-2022 走看看