0

Running MacOS 13.5 on M1. VS Code v1.80.2 with C/C++ extension. MatLab vR2023a (full install, not runtime only). This is almost certainly an issue with my VS Code setup, and I'd really like to know how to do this properly for this and other projects.

I can create the sample program testFeval.cpp and build the executable using the MatLab command prompt, as described here. As described, I can create a cpp file with their test program code, then build the file from the MatLab prompt with:

>> mex -setup -client engine C++
>> mex -v -client engine testFeval.cpp

This creates an executable that I can run from MacOS terminal with % ./testFeval.cpp. The program runs fine.

However, I would like to develop the C++ in VS Code as described here. According to this page, I need these libraries (where matlabroot = '/Applications/MATLAB_R2023a.app'):

  • Engine library — matlabroot/extern/bin/maci64/libMatlabEngine.dylib

  • MATLAB Data Array library — matlabroot/extern/bin/maci64/libMatlabDataArray.dylib

Per that page, I add this to ~./zshrc:

export DYLD_LIBRARY_PATH="/Applications/MATLAB_R2023a.app/extern/bin/maci64"

I also needs header files from "/Applications/MATLAB_R2023a.app/extern/include"

I add these to c_cpp_properties.json and tasks.json as suggested elsewhere on SO.

When I run the C/C++ File, I see many problems. Mostly missing header files, but also some linkage issues.

For example, VS Code can't find libmwordfilt2.h. But, I can confirm that it's in "/Applications/MATLAB_R2023a.app/extern/include"

All of the "missing" includes should be found with the paths added in "args" of tasks.json. Full tasks.json is:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: clang++ build active file",
            "command": "/usr/bin/clang++",
            "args": [
                "-fcolor-diagnostics",
                "-fansi-escape-codes",
                "-I", // add directory to search paths, e.g., headers
                "/Applications/MATLAB_R2023a.app/extern/include/**",
                "/Applications/MATLAB_R2023a.app/extern/include",
                "/Applications/MATLAB_R2023a.app/simulink/include",
                "-L", // add directory to library search path
                "/Applications/MATLAB_R2023a.app/extern/bin/maci64/libMatlabEngine.dylib",
                "/Applications/MATLAB_R2023a.app/extern/bin/maci64/libMatlabDataArray.dylib",
                "-g", // generate source-level debug info
                "${file}",
                // "-o", // write output to fileBasename (no extension)
                // "${fileDirname}/${fileBasenameNoExtension}",
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger.",
            "dependsOn":["build_lib"]
        }
    ],
    "version": "2.0.0"
}

As far as I can tell, MatLab is using clang++, so I do too in c_cpp_properties.json:

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**",
                "/Applications/MATLAB_R2023a.app/extern/include/**",
                "/Applications/MATLAB_R2023a.app/extern/include",
                "/Applications/MATLAB_R2023a.app/extern/bin/maci64"
            ],
            "defines": [],
            "macFrameworkPath": [
                "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
            ],
            "compilerPath": "/usr/bin/clang++",
            "cStandard": "c11",
            "cppStandard": "c++11",
            "intelliSenseMode": "${default}"
        }
    ],
    "version": 4
}

When I build testFeval.cpp using the MatLab prompt in verbose mode, I get this output which may suggest some missing includes or configs:

>> mex -setup -client engine C++
ENGINE configured to use 'Xcode Clang++' for C++ language compilation.
>> mex -v -client engine testFeval.cpp
Verbose mode is on.
... Looking for compiler 'Xcode Clang++' ...
... Looking for environment variable 'DEVELOPER_DIR' ...No.
... Executing command 'xcode-select -print-path' ...Yes ('/Applications/Xcode.app/Contents/Developer').
... Looking for folder '/Applications/Xcode.app/Contents/Developer' ...Yes.
... Executing command 'which xcrun' ...Yes ('/usr/bin/xcrun').
... Looking for folder '/usr/bin' ...Yes.
... Executing command 'defaults read com.apple.dt.Xcode IDEXcodeVersionForAgreedToGMLicense' ...No.
... Executing command 'defaults read /Library/Preferences/com.apple.dt.Xcode IDEXcodeVersionForAgreedToGMLicense' ...Yes ('14.3.1').
... Executing command '
agreed=14.3.1 
 if echo $agreed | grep -E '[\.\"]' >/dev/null; then 
 lhs=`expr "$agreed" : '\([0-9]*\)[\.].*'` 
  rhs=`expr "$agreed" : '[0-9]*[\.]\(.*\)$'` 
 if echo $rhs | grep -E '[\."]' >/dev/null; then 
 rhs=`expr "$rhs" : '\([0-9]*\)[\.].*'` 
 fi 
 if [ $lhs -gt 4 ] || ( [ $lhs -eq 4 ] && [ $rhs -ge 3 ] ); then 
 echo $agreed 
 else 
 exit 1
 fi 
 fi' ...Yes ('14.3.1').
... Executing command 'xcrun -sdk macosx --show-sdk-path' ...Yes ('/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk').
... Executing command 'xcrun -sdk macosx --show-sdk-version | awk 'BEGIN {FS="."} ; {print $1"."$2}'' ...Yes ('13.3').
... Executing command 'clang --version | grep -Eo '[0-9]+\.[0-9]+\.[0-9]'|head -1' ...Yes ('14.0.3').
Found installed compiler 'Xcode Clang++'.
Set INCLUDE = /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1;/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/14.0.3/include;/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include;/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include;/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/System/Library/Frameworks;
Options file details
-------------------------------------------------------------------
    Compiler location: /Applications/Xcode.app/Contents/Developer
    Options file: /Users/phillip_at_work/Library/Application Support/MathWorks/MATLAB/R2023a/ENGINE_C++_maci64.xml
    CMDLINE100 : /usr/bin/xcrun -sdk macosx13.3 clang++ -c -DMATLAB_DEFAULT_RELEASE=R2017b  -DUSE_MEX_CMD   -DMATLAB_MEX_FILE  -I"/Applications/MATLAB_R2023a.app/extern/include" -I"/Applications/MATLAB_R2023a.app/simulink/include" -fno-common -arch x86_64 -mmacosx-version-min=10.15 -fexceptions -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk -fobjc-arc -std=c++14 -stdlib=libc++ -O2 -fwrapv -DNDEBUG "/Users/phillip_at_work/Projects/matlab_cpp_test_v2/testFeval.cpp" -o /var/folders/c5/bhbgscv92tj3_x8y54q8rs8m0000gn/T/mex_67453097271459_5943/testFeval.o
    CMDLINE200 : /usr/bin/xcrun -sdk macosx13.3 clang++ -Wl,-twolevel_namespace -undefined error -arch x86_64 -mmacosx-version-min=10.15 -Wl,-syslibroot,/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk -framework Cocoa  -stdlib=libc++ -O    /var/folders/c5/bhbgscv92tj3_x8y54q8rs8m0000gn/T/mex_67453097271459_5943/testFeval.o   -L"/Applications/MATLAB_R2023a.app/bin/maci64" -lmx -lmex -lmat -L"/Applications/MATLAB_R2023a.app/extern/bin/maci64" -lMatlabDataArray -lMatlabEngine -leng -L"/Applications/MATLAB_R2023a.app/extern/bin/maci64" -lMatlabDataArray -lMatlabEngine -o testFeval
    CC : /usr/bin/xcrun -sdk macosx13.3 clang
    CXX : /usr/bin/xcrun -sdk macosx13.3 clang++
    DEFINES : -DMATLAB_DEFAULT_RELEASE=R2017b  -DUSE_MEX_CMD   -DMATLAB_MEX_FILE 
    MATLABMEX : -DMATLAB_MEX_FILE 
    MACOSX_DEPLOYMENT_TARGET : 10.15
    CFLAGS : -fno-common -arch x86_64 -mmacosx-version-min=10.15 -fexceptions -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk
    CXXFLAGS : -fno-common -arch x86_64 -mmacosx-version-min=10.15 -fexceptions -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk -fobjc-arc -std=c++14 -stdlib=libc++
    INCLUDE : -I"/Applications/MATLAB_R2023a.app/extern/include" -I"/Applications/MATLAB_R2023a.app/simulink/include"
    CXXOPTIMFLAGS : -O2 -fwrapv -DNDEBUG
    CXXDEBUGFLAGS : -g
    LD : /usr/bin/xcrun -sdk macosx13.3 clang
    LDXX : /usr/bin/xcrun -sdk macosx13.3 clang++
    LDFLAGS : -Wl,-twolevel_namespace -undefined error -arch x86_64 -mmacosx-version-min=10.15 -Wl,-syslibroot,/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk -framework Cocoa  -stdlib=libc++
    LDBUNDLE : 
    LINKEXPORT : 
    LINKEXPORTVER : 
    LINKEXPORTCPP : 
    LINKLIBS : -L"/Applications/MATLAB_R2023a.app/bin/maci64" -lmx -lmex -lmat -L"/Applications/MATLAB_R2023a.app/extern/bin/maci64" -lMatlabDataArray -lMatlabEngine -leng -L"/Applications/MATLAB_R2023a.app/extern/bin/maci64" -lMatlabDataArray -lMatlabEngine
    LDOPTIMFLAGS : -O
    LDDEBUGFLAGS : -g
    OBJEXT : .o
    LDEXT : 
    SETENV : CC="/usr/bin/xcrun -sdk macosx13.3 clang"
                CXX="/usr/bin/xcrun -sdk macosx13.3 clang++"
                CFLAGS="-fno-common -arch x86_64 -mmacosx-version-min=10.15 -fexceptions -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk -DMATLAB_DEFAULT_RELEASE=R2017b  -DUSE_MEX_CMD   -DMATLAB_MEX_FILE "
                CXXFLAGS="-fno-common -arch x86_64 -mmacosx-version-min=10.15 -fexceptions -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk -fobjc-arc -std=c++14 -stdlib=libc++ -DMATLAB_DEFAULT_RELEASE=R2017b  -DUSE_MEX_CMD   -DMATLAB_MEX_FILE "
                COPTIMFLAGS="-O2 -fwrapv -DNDEBUG"
                CXXOPTIMFLAGS="-O2 -fwrapv -DNDEBUG"
                CDEBUGFLAGS="-g"
                CXXDEBUGFLAGS="-g"
                LD="/usr/bin/xcrun -sdk macosx13.3 clang"
                LDXX="/usr/bin/xcrun -sdk macosx13.3 clang++"
                LDFLAGS="-Wl,-twolevel_namespace -undefined error -arch x86_64 -mmacosx-version-min=10.15 -Wl,-syslibroot,/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk -framework Cocoa  -stdlib=libc++ -L"/Applications/MATLAB_R2023a.app/bin/maci64" -lmx -lmex -lmat -L"/Applications/MATLAB_R2023a.app/extern/bin/maci64" -lMatlabDataArray -lMatlabEngine -leng -L"/Applications/MATLAB_R2023a.app/extern/bin/maci64" -lMatlabDataArray -lMatlabEngine "
                LDDEBUGFLAGS="-g"
    DEVELOPER_DIR_CHECK : 
    XCODE_DIR : /Applications/Xcode.app/Contents/Developer
    XCRUN_DIR : /usr/bin
    XCODE_AGREED_VERSION : 14.3.1
    ISYSROOT : /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk
    SDKVER : 13.3
    CLANG_VERSION : 14.0.3
    MATLABROOT : /Applications/MATLAB_R2023a.app
    ARCH : maci64
    SRC : "/Users/phillip_at_work/Projects/matlab_cpp_test_v2/testFeval.cpp"
    OBJ : /var/folders/c5/bhbgscv92tj3_x8y54q8rs8m0000gn/T/mex_67453097271459_5943/testFeval.o
    OBJS : /var/folders/c5/bhbgscv92tj3_x8y54q8rs8m0000gn/T/mex_67453097271459_5943/testFeval.o 
    SRCROOT : /Users/phillip_at_work/Projects/matlab_cpp_test_v2/testFeval
    DEF : /var/folders/c5/bhbgscv92tj3_x8y54q8rs8m0000gn/T/mex_67453097271459_5943/testFeval.def
    EXP : "testFeval.exp"
    LIB : "testFeval.lib"
    EXE : testFeval
    ILK : "testFeval.ilk"
    MANIFEST : "testFeval.manifest"
    TEMPNAME : testFeval
    EXEDIR : 
    EXENAME : testFeval
    OPTIM : -O2 -fwrapv -DNDEBUG
    LINKOPTIM : -O
-------------------------------------------------------------------
Building with 'Xcode Clang++'.
/usr/bin/xcrun -sdk macosx13.3 clang++ -c -DMATLAB_DEFAULT_RELEASE=R2017b  -DUSE_MEX_CMD   -DMATLAB_MEX_FILE  -I"/Applications/MATLAB_R2023a.app/extern/include" -I"/Applications/MATLAB_R2023a.app/simulink/include" -fno-common -arch x86_64 -mmacosx-version-min=10.15 -fexceptions -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk -fobjc-arc -std=c++14 -stdlib=libc++ -O2 -fwrapv -DNDEBUG "/Users/phillipcurtsmith/Projects/matlab_cpp_test_v2/testFeval.cpp" -o /var/folders/c5/bhbgscv92tj3_x8y54q8rs8m0000gn/T/mex_67453097271459_5943/testFeval.o
/usr/bin/xcrun -sdk macosx13.3 clang++ -Wl,-twolevel_namespace -undefined error -arch x86_64 -mmacosx-version-min=10.15 -Wl,-syslibroot,/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk -framework Cocoa  -stdlib=libc++ -O    /var/folders/c5/bhbgscv92tj3_x8y54q8rs8m0000gn/T/mex_67453097271459_5943/testFeval.o   -L"/Applications/MATLAB_R2023a.app/bin/maci64" -lmx -lmex -lmat -L"/Applications/MATLAB_R2023a.app/extern/bin/maci64" -lMatlabDataArray -lMatlabEngine -leng -L"/Applications/MATLAB_R2023a.app/extern/bin/maci64" -lMatlabDataArray -lMatlabEngine -o testFeval
MEX completed successfully.

I starting adding things to the json files based on the MatLab build output, but now I'm just guessing. Any insight on proper setup procedure?

  • Try configuring with `"-I/Applications/MATLAB_R2023a.app/extern/include/**"`, and one `-I` for each directory added. – Cris Luengo Jul 31 '23 at 03:03
  • `DYLD_LIBRARY_PATH` doesn’t work any more in macOS, AFAIK. But also you shouldn’t need it, the binary will have the full path to the linked libraries in it. – Cris Luengo Jul 31 '23 at 03:06
  • Just tried `..."-I", "/Applications/MATLAB_R2023a.app/extern/include/**", "-I", "/Applications/MATLAB_R2023a.app/extern/include",...` with the same results. When I try `..."-I/Applications/MATLAB_R2023a.app/extern/include/**", "-I/Applications/MATLAB_R2023a.app/extern/include",...`, I get fewer errors, but those errors seem more problematic. For example, I get `expected function body after function declarator` for some includes. I confirm that the includes inside `testFeval.cpp` can be found (right click -> go to definition actually turns up content). – phillip_at_work Aug 01 '23 at 14:33
  • [That new error message seems to be a syntax error in your code somewhere](https://www.google.com/search?q=%22expected+function+body+after+function+declarator%22+site:stackoverflow.com). Please read [mre], it's hard to debug a problem with only partial information. – Cris Luengo Aug 01 '23 at 14:46
  • The code I'm running is a 1:1 test program from MathWorks. That same program (the same cpp file, actually) compiles from the Matlab command prompt. This program: https://www.mathworks.com/help/matlab/matlab_external/test-your-build-environment.html – phillip_at_work Aug 01 '23 at 16:11
  • I don't know what could be causing this then. Maybe you're missing the `-std=c++14` flag in `tasks.json`? (I see it in the MEX output, but I have no idea how `c_cpp_properties.json` and `task.json` are used by VSCode.) You also have a `-L` option not followed by a path, you should remove that (though that is not the cause of this particular problem). – Cris Luengo Aug 01 '23 at 16:37
  • OK, `c_cpp_properties.json` is only for IntelliSense, it is not used for compiling, so let's take that one out of the equation. You need to add the C++ standard version flag to `task.json`. Using an older standard (which might be the default still?) could lead to the code having syntax errors. – Cris Luengo Aug 01 '23 at 16:40
  • Progress. Added `"-std=c++14"`. Building runs longer and fails with fewer errors. Most errors are for the same include. `rtwtypes.h` cannot be found. I confirm that this is NOT in `/Applications/MATLAB_R2023a.app/extern/include`. One error for missing `MatlabCppSharedLib.hpp`, also not in `/include`. All remaining errors report `templates must have C++ linkage`, `explicit specialization of undeclared template struct 'char_traits'`, `expected ';' or '>' in template-parameter-list`, originating from `cmath` and `iosfwd`. Errors end in `gcc [Ln ..., Col ...]`. Thought I was using clang++? – phillip_at_work Aug 01 '23 at 17:24

0 Answers0