Overwriting default mex compiler flags via Simulink C-Caller block

7 views (last 30 days)
I'm working in a pre-existing Simulink code base in R2022b where we're trying to integrate some custom C++ packages. I've been tinkering with C-Caller blocks using the examples and it all works fine for simple functions. Things start to break down when I try to include our actual C++ packages, which require at least C++ 14.
It seems that the default mex options that ship with Mathworks R2022b set it to C++11:
[i]cat /usr/local/MATLAB/R2022b/bin/glnxa64/mexopts/g++_glnxa64.xml | rg CXXFLAGS
CXXFLAGS="$CFLAGS -std=c++11"
CXXFLAGS="-ansi -pthread -std=c++11"
If I setup mex for myself, it also gets set to C++11:
[i]cat ~/.matlab/R2022b/mex_C++_glnxa64.xml | rg 'std='
<vars CMDLINE1="$CXX -c $DEFINES $INCLUDE $CXXFLAGS $OPTIM $SRC -o $OBJ" CMDLINE2="$LDXX $LDFLAGS $LDTYPE $LINKOPTIM $LINKEXPORTVER $OBJS $CXXLIBS $LINKLIBS -o $EXE" CXX="$GCC" DEFINES="-D_GNU_SOURCE $MATLABMEX" MATLABMEX="-DMATLAB_MEX_FILE " CFLAGS="-fexceptions -fPIC -fno-omit-frame-pointer -pthread" CXXFLAGS="$CFLAGS -std=c++11" INCLUDE="-I&quot;$MATLABROOT/extern/include&quot; -I&quot;$MATLABROOT/simulink/include&quot;" CXXOPTIMFLAGS="-O2 -fwrapv -DNDEBUG" CXXDEBUGFLAGS="-g" LDXX="$CXX" LDFLAGS="-pthread -Wl,--no-undefined " LDTYPE="-shared" LINKEXPORT="-Wl,--version-script,&quot;$MATLABROOT/extern/lib/$ARCH/mexFunction.map&quot;" LINKEXPORTVER="-Wl,--version-script,&quot;$MATLABROOT/extern/lib/$ARCH/c_exportsmexfileversion.map&quot;" LINKLIBS="-lstdc++ -Wl,--as-needed -Wl,-rpath-link,$MATLABROOT/bin/$ARCH -L&quot;$MATLABROOT/bin/$ARCH&quot; -Wl,-rpath-link,$MATLABROOT/extern/bin/$ARCH -L&quot;$MATLABROOT/extern/bin/$ARCH&quot; -lMatlabDataArray -lmx -lmex -lm -lmat" LDOPTIMFLAGS="-O" LDDEBUGFLAGS="-g" MWCPPLIB="&quot;$MATLABROOT/sys/os/$ARCH/orig/libstdc++.so.6&quot;" OBJEXT=".o" LDEXT=".mexa64" SETENV="CC=&quot;gcc&quot;
<mbuild DEFINES="-D_GNU_SOURCE $MATLABMEX -DUNIX -DX11 -DGLNXA64 -DGCC -UMX_COMPAT_SC" CXXFLAGS="-ansi -pthread -std=c++11" INCLUDE="$INCLUDE -I&quot;$MATLABROOT/extern/include/cpp&quot;" LDFLAGS="-pthread -lpthread -Wl,--as-needed -Wl,-rpath-link,$MATLABROOT/bin/$ARCH" LINKEXPORT="" LINKEXPORTVER="" LINKLIBS="$MWCPPLIB $MW_GLIBC_SHIM -L&quot;$MATLABROOT/runtime/$ARCH&quot; -L&quot;$MATLABROOT/extern/bin/$ARCH&quot; -lm -lmwmclmcrrt -lMatlabDataArray -lMatlabCppSharedLib" LDEXT="" MATLABMEX="" LDTYPE=""/>
Things I have tried
  • Setting the compiler flag via Simulink (see screenshot below). This seems to get overwritten by the xml file. Only by deleting std=c++11 from the xml files does this work.
  • Directly modifying the xml files to hard-code the C++ standard to C++14
Problems Moving Forwards
While I have a working system locally, I would like to ensure:
  1. A straightforward setup process for other developers.
  2. A simple way to configure the C++ standard in CI.
In an ideal world, I'm simply missing something with the Simulink compiler flag option that will just work, but I'm currently thinking of the following options:
  • Run a sed command to replace C++11 with C++14
  • Version control my xml file and ensure it gets consumed (locally, via ansible; remotely, via mounting into the docker container)
Would appreciate any help or thoughts on this -- thanks!

Answers (1)

Mark McBroom
Mark McBroom on 13 Sep 2023
One approach you could try is to compile your external C++14 code into it's own library outside of MATLAB/Simulink using g++ with C++14 compiler switch. In the Simulink model in the Custom Code dialog you then remove the .cpp files and instead add the name of the library you created outside of Simulink. Using this approach, Simulink will not try to compile your external C++ code and will only have to compile the content of the C Caller block and then link to your library.


Find more on Simulink Coder in Help Center and File Exchange




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!