3

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.

Gnattuha
  • 211
  • 3
  • 11

1 Answers1

7

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>
Community
  • 1
  • 1
chappjc
  • 30,359
  • 6
  • 75
  • 132
  • 4
    Including -Dchar16_t=uint16_T in the c flags in the mexopts.ah solved all problems. Thanks, @chappjs. – Gnattuha Mar 28 '14 at 20:35
  • 6
    `-Dchar16_t=UINT16_T` worked for me in `mexopts.sh`. – Francesco Apr 17 '14 at 08:43
  • An anonymous user reported that `#define char16_t UINT16_T` worked. Score 2 for uppercase, 1 for lowercase. – chappjc Apr 21 '14 at 22:48
  • 1
    Another vote for `-Dchar16_t=UINT16_T` in `mexopts.sh` (same as @Francesco said). – Matt Oct 04 '14 at 01:52
  • Please note the uppercase "T" at the end per the comment from @Francesco and others. This worked for me, but leaving it lowercase as in the accepted answer failed for me. – Jed Apr 02 '19 at 15:07