We are building a matlab executable (.mexw64) from C++ using MSVC compiler (we tested compiler versions 15 and 16) and a cmake toolchain (not with the mex command). The mexw64 contains functionality, that we can call from Matlab, and these functions need to log. My problem is, that when building in debug, the std::cout is not routed to the matlab console (we display log messages this way).
Details:
- if built in release mode, the log messages display correctly in R2017 and R2021
- if build in debug mode, the log messages are only displayed in R2017 and not displayed R2021
- MSVC compiler version 15 and 16 work the same way
What was changed in this regard between R2017 and R2021? How can I solve this issue? Changing the logging from std::cout to something else (e.g. mex logging function or std::cerr) is unfortunately not an option.
Note: I noticed in the build log, that the option -DMATLAB_DEFAULT_RELEASE=R2017b
is set. What does this do? Is it ok, to have this value, even if my Matlab version is R2021b? The option is set in a cmake function:
# For 9.4 (R2018a) and newer, add API macro.
# Add it for unknown versions too, just in case.
if(NOT Matlab_VERSION_STRING VERSION_LESS "9.4"
OR Matlab_VERSION_STRING STREQUAL "unknown")
if(${${prefix}_R2018a})
set(MEX_API_MACRO "MATLAB_DEFAULT_RELEASE=R2018a")
else()
set(MEX_API_MACRO "MATLAB_DEFAULT_RELEASE=R2017b")
endif()
endif()
Edit: I did not mention that we correctly overload the std::streambuf as @Cris Luengo pointed out. I do not copy the code here, because it's long and proprietary, but it works with R2017b and also with R2021b in release mode.