0

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=R2017bis 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.

G. B.
  • 528
  • 2
  • 15
  • 1
    The linked Q&A doesn't answer your more technical questions, but it should solve your problem. I think more recent versions of MATLAB (as you found) do forward `std::cout` to the console, at least on some operating systems, but this would require the right compilation settings, which you might not have copied fully in your CMake scripts. Older versions of MATLAB did not do this at all. The linked Q&A shows how to ensure `std::cout` is forwarded to the console. – Cris Luengo Mar 10 '23 at 08:44
  • 1
    The question about R2017b vs R2018a is discribed in the documentation page for `mex` here: https://www.mathworks.com/help/matlab/ref/mex.html#mw_5d9f5fd8-76a3-4406-817f-d4bba493eeb8 It basically is whether the MEX-file wants to receive complex arrays in separate or interleaved formats. – Cris Luengo Mar 10 '23 at 08:45
  • thanks for pointing out the std::streambuf overload, indeed I did not mention that we implemented it correctly. pls. review the classification for duplicate – G. B. Mar 10 '23 at 09:06
  • In that case you need to post a [mre]. There is nothing we can tell you to change if we don’t know what you’re doing. – Cris Luengo Mar 10 '23 at 14:16
  • …if your code were 100% correct, it would mean that `mexPrintf` doesn’t work in a debug build in the newest MATLAB. This is really hard to believe. So there is an issue with your code or with how you build it. Creating a [mre] should make it clear to you where the issue is. I bet once you have that done, you’ll have answered your own question. – Cris Luengo Mar 10 '23 at 14:21

0 Answers0