Page MenuHomePhabricator

usUtils.cpp will not compile with Visual Studio when using the /MD flag
Closed, ResolvedPublic

Description

A colleague compiled MITK using the /MD flag of Visual Studio. This caused a compile error because a function _CrtSetReportMode was not defined.

As far as I understod, the cause is around line 90 in Core/Code/CppMicroServices/src/util/usUtils.cpp:

#if defined(_MSC_VER) && !defined(NDEBUG) && defined(_CRT_ERROR)

int reportMode = _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_WNDW);

There, the code uses some CRT output methods which are only present in the debug runtime libraries. The code tests the availability using the NDEBUG flag. This NDEBUG flag, however, is not enough to ensure availability. Basically, NDEBUG is not defined in debug builds, but a debug build can be mixed with release runtime libraries using the /MD compiler flag.

A sufficient test would also test the _DEBUG flag, which is defined when the debug runtime libraries are used (http://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx).

Patch is attached, could be integrated. I guess this would also be nice for the snapshot release.

Event Timeline

Looks good, thanks.

BTW, is the mixing of release and debug libraries intended? Because there is also the /MDd flag, which could be used in debug builds for consistently using debug runtime libraries with debug builds.

It is to be noted, that I use the /MD flag for BOTH release and debug builds.
For release builds it's the default, for my debug build it is a tweak to have a faster debug build.

For the default flags, the original check for 'NDEBUG' is sufficient.
However, when using custom compiler-flags, this check is inconsistent with the Visual Studio c++ flag handling.

If you want to have a closer look: C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\crtdbg.h is the header to check.

In line 191, the flag '_DEBUG' is used to check for debug mode, while usUtils uses 'NDEBUG'.

Sorry, I unintentionally reset the + for the release fix.

Could you please set it once more?

thanks..

(In reply to comment #2)

It is to be noted, that I use the /MD flag for BOTH release and debug builds.
For release builds it's the default, for my debug build it is a tweak to
have a faster debug build.

For the default flags, the original check for 'NDEBUG' is sufficient.
However, when using custom compiler-flags, this check is inconsistent with
the Visual Studio c++ flag handling.

If you want to have a closer look: C:\Program Files (x86)\Microsoft Visual
Studio 9.0\VC\include\crtdbg.h is the header to check.

In line 191, the flag '_DEBUG' is used to check for debug mode, while
usUtils uses 'NDEBUG'.

Thanks, I understand the reasoning and the fix is certainly needed. Just wanted to make sure the using /MD with a debug build was intentional ;-)

[6ed361]: Merge branch 'bug-12148-md-flag-compiler-error'

Merged commits:

2012-06-08 11:05:24 Daniel Maleike [88fcac]
Test _DEBUG before using _CrtSetReportMode