3
votes

I am trying to compile developer build for OpenCV (3.0.0), however I cannot configure my system to mex C files. I tried to follow suggestions from stackoverflow and currently I can mex c++ files, but not C ones. What I did was edit mexopts.sh so that correct SDKROOT was used, also I changed deployment targets to 10.9.

My mexopts.sh looks as follows:

    #PATCH: MacOSX10.8 // updated manually to 10.9
        CC='llvm-gcc'
        CXX='llvm-g++'
        SDKROOT='/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/'   

        #SDKROOT='/'
        MACOSX_DEPLOYMENT_TARGET='10.9'
        ARCHS='x86_64'

        # StorageVersion: 1.0
        # CkeyName: GNU C
        # CkeyManufacturer: GNU
        # CkeyLanguage: C
        # CkeyVersion:
        CFLAGS="-fno-common -no-cpp-precomp -arch $ARCHS -isysroot $SDKROOT -mmacosx-version-min=$MACOSX_DEPLOYMENT_TARGET"
        CFLAGS="$CFLAGS  -fexceptions"
        CLIBS="$MLIBS"
        COPTIMFLAGS='-O2 -DNDEBUG'
        CDEBUGFLAGS='-g'

        CLIBS="$CLIBS -lstdc++"
        # C++keyName: GNU C++
        # C++keyManufacturer: GNU
        # C++keyLanguage: C++
        # C++keyVersion: 

        # OLD
        #CXXFLAGS="-fno-common -no-cpp-precomp -fexceptions -arch $ARCHS -isysroot $SDKROOT -mmacosx-version-min=$MACOSX_DEPLOYMENT_TARGET"
        # NEW
        CXXFLAGS="-fno-common -no-cpp-precomp -fexceptions -arch $ARCHS -isysroot $SDKROOT -mmacosx-version-min=$MACOSX_DEPLOYMENT_TARGET -std=c++11"
        CXXLIBS="$MLIBS -lstdc++"
        CXXOPTIMFLAGS='-O2 -DNDEBUG'
        CXXDEBUGFLAGS='-g'

Only after I added -std=c++11 to the CXXFLAGS I was able to compile simple C++ code saved using .cpp extension. Here is the code for the file I want to compile

#include <math.h>
#include <matrix.h>
#include <mex.h>

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
  mexPrintf("Hello World!\n");
}

I save it as test.c and compile. Here is the output i get

mex test.c

In file included from test.c:2: In file included from /Applications/MATLAB_R2012a.app/extern/include/matrix.h:295: /Applications/MATLAB_R2012a.app/extern/include/tmwtypes.h:819:9: error: unknown type name 'char16_t' typedef char16_t CHAR16_T; ^ 1 error generated.

mex: compile of ' "test.c"' failed.

Error using mex (line 206) Unable to complete successfully.

The same error was used when the file was saved as test.cpp without -std=c++11 in the CXXFLAGS. I am not experienced in C/C++ but is there some library or flag I need to add to fix the problem? Any suggestions, links would be helpful.

Thank you.

1
I assume that you have run mex -setup after editing your mexopts.sh file?horchler
Yes, I did. I am using edited version of mexopts.shGnattuha
Another workaround where C++11 is not possible is posted in this updated answer.chappjc

1 Answers

7
votes

When it is not possible to use C++11 via CXXFLAGS, either due to compiler limitation or because the file must be C only, here are some possible workarounds that do not involve hacking the MATLAB installation (tmwtypes.h):

Use the preprocessor to define char16_t as a macro for uint16_t (defined in stdint.h). Either use a #define before #include "mex.h", or just set it on the command line (mex or CFLAGS):

-Dchar16_t=uint16_t

Alternatively, add a typedef, again before including mex.h:

typedef uint16_t char16_t;

To get uint16_t, you need to #include <stdint.h>. Or try UINT16_T (caps).

Last, if using C (compiling .c file), uchar.h may define a char16_t macro, depending on what revision of the the C standard supported by your compiler:

/* In myMexFile.c */
#include <uchar.h>