添加 mexopencv 出了各种问题,最后在作者 github 的一个问答帖和 StackOverFlow 中找到答案。

链接:

https://github.com/kyamagu/mexopencv/issues/110

http://stackoverflow.com/questions/19773916/compiling-mexopencv-in-os-x-10-9-with-xcode-5-and-matlab-r2013b

当然,除了上面这个帖子,我还看了其他大量解答,最终得到这篇博文。上面的帖子中有我解决问题的详细过程,如果在利用这篇文章的过程中遇到任何问题,请到原帖中寻找我的评论。

0、必要环境支持

确保你的电脑有cmake、pkg-config、xcode-select。

1、生成配置文件

>> mex -setup
Warning: Legacy MEX infrastructure is provided for compatibility; it will be removed
in a future version of MATLAB. For more information, consult the MEX release notes
http://www.mathworks.com/help/matlab/release-notes.html. 

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

    Using the 'mexsh -setup' command selects an options file that is
    placed in /Users/haipz/.matlab/R2014b and used by default for 'mexsh'. An options 
    file in the current working directory or specified on the command line 
    overrides the default options file in /Users/haipz/.matlab/R2014b.
 
    To override the default options file, use the 'mexsh -f' command
    (see 'mexsh -help' for more information).

The options files available for mexsh are:

  1: /Applications/MATLAB_R2014b.app/bin/mexopts.sh : 
      Template Options file for building MEX-files
 

  0: Exit with no changes

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

2、修改配置文件

open ~/.matlab/R2014b/mexopts.sh

然后按照顺序进行一下修改:

将所有 10.8 替换成 10.10

将所有 10.7 替换成 10.8

将所有 -lstdc++ 替换成 -lc++

将 MW_SDKROOT_TMP="findxcode-select -print-path-name MacOSX10.7.sdk" 改成 MW_SDKROOT_TMP='/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/'

在 CFLAGS="$CFLAGS -fexceptions" 后一行添加 CFLAGS="$CFLAGS -Dchar16_t=uint16_t"

在 CXXFLAGS="-fno-common -fexceptions -arch $ARCHS -isysroot $MW_SDKROOT -mmacosx-version-min=$MACOSX_DEPLOYMENT_TARGET" 后一行添加 CXXFLAGS="$CXXFLAGS -std=c++11 -stdlib=libc++"

将 MW_SDKROOT=`$MW_SDKROOT_TMP` 改成 MW_SDKROOT='/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/'

将 MLIBS="-L$TMW_ROOT/bin/$Arch -lmx -lmex -lmat" 改成 MLIBS="$TMW_ROOT/bin/$Arch/libmx.dylib $TMW_ROOT/bin/$Arch/libmex.dylib $TMW_ROOT/bin/$Arch/libmat.dylib"

所有修改可以参考一下我的配置文件:

#
# mexopts.sh	Shell script for configuring MEX-file creation script,
#               mex.  These options were tested with the specified compiler.
#
# usage:        Do not call this file directly; it is sourced by the
#               mex shell script.  Modify only if you don't like the
#               defaults after running mex.  No spaces are allowed
#               around the '=' in the variable assignment.
#
# Note: For the version of system compiler supported with this release,
#       refer to the Supported and Compatible Compiler List at:
#       http://www.mathworks.com/support/compilers/current_release/
#
#
# SELECTION_TAGs occur in template option files and are used by MATLAB
# tools, such as mex and mbuild, to determine the purpose of the contents
# of an option file. These tags are only interpreted when preceded by '#'
# and followed by ':'.
#
#SELECTION_TAG_MEX_OPT: Template Options file for building MEX-files
#
# Copyright 1984-2011 The MathWorks, Inc.
#----------------------------------------------------------------------------
#
    TMW_ROOT="$MATLAB"
    MFLAGS=''
    if [ "$ENTRYPOINT" = "mexLibrary" ]; then
        MLIBS="-L$TMW_ROOT/bin/$Arch -lmx -lmex -lmat -lmwservices -lut"
    else  
        MLIBS="$TMW_ROOT/bin/$Arch/libmx.dylib $TMW_ROOT/bin/$Arch/libmex.dylib $TMW_ROOT/bin/$Arch/libmat.dylib"
    fi
    case "$Arch" in
        Undetermined)
#----------------------------------------------------------------------------
# Change this line if you need to specify the location of the MATLAB
# root directory.  The script needs to know where to find utility
# routines so that it can determine the architecture; therefore, this
# assignment needs to be done while the architecture is still
# undetermined.
#----------------------------------------------------------------------------
            MATLAB="$MATLAB"
            ;;
        glnx86)
#----------------------------------------------------------------------------
echo "Error: Did not imbed 'options.sh' code"; exit 1 #imbed options.sh glnx86 12
#----------------------------------------------------------------------------
            ;;
        glnxa64)
#----------------------------------------------------------------------------
            RPATH="-Wl,-rpath-link,$TMW_ROOT/bin/$Arch"
            # StorageVersion: 1.0
            # CkeyName: GNU C
            # CkeyManufacturer: GNU
            # CkeyLanguage: C
            # CkeyVersion:
            # CkeyLinkerName: GNU ld
            # CkeyLinkerVersion:
            CC='gcc'
            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:
            # C++keyLinkerName: GNU ld
            # C++keyLinkerVersion:  
            CXX='g++'
            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:
            # FortrankeyLinkerName: GNU ld
            # FortrankeyLinkerVersion:  
#
            FC='gfortran'
            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=':'
#----------------------------------------------------------------------------
            ;;
        sol64)
#----------------------------------------------------------------------------
echo "Error: Did not imbed 'options.sh' code"; exit 1 #imbed options.sh sol64 12
#----------------------------------------------------------------------------
            ;;
        mac)
#----------------------------------------------------------------------------
echo "Error: Did not imbed 'options.sh' code"; exit 1 #imbed options.sh mac 12
#----------------------------------------------------------------------------
            ;;
        maci)
#----------------------------------------------------------------------------
echo "Error: Did not imbed 'options.sh' code"; exit 1 #imbed options.sh maci 12
#----------------------------------------------------------------------------
            ;;
        maci64)
#----------------------------------------------------------------------------
            # StorageVersion: 1.0
            # CkeyName: Clang
            # CkeyManufacturer: Apple
            # CkeyLanguage: C
            # CkeyVersion:
            # CkeyLinkerName:
            # CkeyLinkerVersion:
            CC='xcrun  -sdk macosx10.10  clang'
## workaround clang defect temporarily use line below           SDKROOT='/Developer/SDKs/MacOSX10.6.sdk'
# compute SDK root on the fly
# target 10.10
            MW_SDKROOT_TMP='/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/'
			MW_SDKROOT='/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/'
            MACOSX_DEPLOYMENT_TARGET='10.10'
            ARCHS='x86_64'
            CFLAGS="-fno-common -arch $ARCHS -isysroot $MW_SDKROOT -mmacosx-version-min=$MACOSX_DEPLOYMENT_TARGET"
            CFLAGS="$CFLAGS  -fexceptions"
            CFLAGS="$CFLAGS -Dchar16_t=uint16_t"
            CLIBS="$MLIBS"
            COPTIMFLAGS='-O2 -DNDEBUG'
            CDEBUGFLAGS='-g'
#
            CLIBS="$CLIBS -lstdc++"
            # C++keyName: Clang++
            # C++keyManufacturer: Apple
            # C++keyLanguage: C++
            # C++keyVersion:
            # C++keyLinkerName:
            # C++keyLinkerVersion:
            CXX='xcrun  -sdk macosx10.10  clang++'
            CXXFLAGS="-fno-common -fexceptions -arch $ARCHS -isysroot $MW_SDKROOT -mmacosx-version-min=$MACOSX_DEPLOYMENT_TARGET"
            CXXFLAGS="$CXXFLAGS -std=c++11 -stdlib=libc++"
            CXXLIBS="$MLIBS -lstdc++"
            CXXOPTIMFLAGS='-O2 -DNDEBUG'
            CXXDEBUGFLAGS='-g'
#
            # FortrankeyName: GNU Fortran
            # FortrankeyManufacturer: GNU
            # FortrankeyLanguage: Fortran
            # FortrankeyVersion: 
            # FortrankeyLinkerName: 
            # FortrankeyLinkerVersion:
            FC='gfortran'
            FFLAGS='-fexceptions -m64 -fbackslash'
            FC_LIBDIR=`$FC -print-file-name=libgfortran.dylib 2>&1 | sed -n '1s/\/*libgfortran\.dylib//p'`
            FC_LIBDIR2=`$FC -print-file-name=libgfortranbegin.a 2>&1 | sed -n '1s/\/*libgfortranbegin\.a//p'`
            FLIBS="$MLIBS -L$FC_LIBDIR -lgfortran -L$FC_LIBDIR2 -lgfortranbegin"
            FOPTIMFLAGS='-O'
            FDEBUGFLAGS='-g'
#
            LD="$CC"
            LDEXTENSION='.mexmaci64'
            LDFLAGS="-arch $ARCHS -Wl,-syslibroot,$MW_SDKROOT -mmacosx-version-min=$MACOSX_DEPLOYMENT_TARGET"
            LDFLAGS="$LDFLAGS -bundle -Wl,-exported_symbols_list,$TMW_ROOT/extern/lib/$Arch/$MAPFILE"
            LDOPTIMFLAGS='-O'
            LDDEBUGFLAGS='-g'
#
            POSTLINK_CMDS=':'
#----------------------------------------------------------------------------
            ;;
    esac
#############################################################################
#
# Architecture independent lines:
#
#     Set and uncomment any lines which will apply to all architectures.
#
#----------------------------------------------------------------------------
#           CC="$CC"
#           CFLAGS="$CFLAGS"
#           COPTIMFLAGS="$COPTIMFLAGS"
#           CDEBUGFLAGS="$CDEBUGFLAGS"
#           CLIBS="$CLIBS"
#
#           FC="$FC"
#           FFLAGS="$FFLAGS"
#           FOPTIMFLAGS="$FOPTIMFLAGS"
#           FDEBUGFLAGS="$FDEBUGFLAGS"
#           FLIBS="$FLIBS"
#
#           LD="$LD"
#           LDFLAGS="$LDFLAGS"
#           LDOPTIMFLAGS="$LDOPTIMFLAGS"
#           LDDEBUGFLAGS="$LDDEBUGFLAGS"
#----------------------------------------------------------------------------
#############################################################################

3、将 pkg-config 添加到 MATLAB 中

setenv('PATH', [getenv('PATH') ':/usr/local/bin']);

你可以选择将这段代码写入 ~/Documents/MATLAB/startup.m

4、执行以下代码转移 libopencv

mkdir /Applications/MATLAB_R2014b.app/bin/maci64/libopencv
mv /Applications/MATLAB_R2014b.app/bin/maci64/libopencv_*  Applications/MATLAB_R2014b.app/bin/maci64/libopencv

5、重置编译,并编译 mexopencv 直到一切完成

mexopencv.make('clean', true);
mexopencv.make

6、添加以下内容到 ~/.bash_profile 。

export PKG_CONFIG_PATH="/usr/X11/lib/pkgconfig:/usr/local/Cellar/cairo/1.14.2/lib/pkgconfig:/usr/local/Cellar/opencv/2.4.11/lib/pkgconfig"

export PYTHONPATH="/usr/local/lib/python2.7/site-packages:"

export DYLD_INSERT_LIBRARIES="/usr/local/lib/libtiff.5.dylib:/usr/local/Cellar/opencv/2.4.11/lib/libopencv_calib3d.dylib:/usr/local/Cellar/opencv/2.4.11/lib/libopencv_contrib.dylib:/usr/local/Cellar/opencv/2.4.11/lib/libopencv_core.dylib:/usr/local/Cellar/opencv/2.4.11/lib/libopencv_features2d.dylib:/usr/local/Cellar/opencv/2.4.11/lib/libopencv_flann.dylib:/usr/local/Cellar/opencv/2.4.11/lib/libopencv_gpu.dylib:/usr/local/Cellar/opencv/2.4.11/lib/libopencv_highgui.dylib:/usr/local/Cellar/opencv/2.4.11/lib/libopencv_imgproc.dylib:/usr/local/Cellar/opencv/2.4.11/lib/libopencv_legacy.dylib:/usr/local/Cellar/opencv/2.4.11/lib/libopencv_ml.dylib:/usr/local/Cellar/opencv/2.4.11/lib/libopencv_nonfree.dylib:/usr/local/Cellar/opencv/2.4.11/lib/libopencv_objdetect.dylib:/usr/local/Cellar/opencv/2.4.11/lib/libopencv_ocl.dylib:/usr/local/Cellar/opencv/2.4.11/lib/libopencv_photo.dylib:/usr/local/Cellar/opencv/2.4.11/lib/libopencv_stitching.dylib:/usr/local/Cellar/opencv/2.4.11/lib/libopencv_superres.dylib:/usr/local/Cellar/opencv/2.4.11/lib/libopencv_video.dylib:/usr/local/Cellar/opencv/2.4.11/lib/libopencv_videostab.dylib"

并且执行一下代码即时更新系统环境

source ~/.bash_profile

7、移除 libtiff.5.dylib 避免版本重复

mv /Applications/MATLAB_R2014b.app/bin/maci64/libtiff.5.dylib /Applications/MATLAB_R2014b.app/bin/maci64/libtiff.5.dylib.bak
转载保留版权:http://haipz.com/blog/i/6492 - 海胖博客