Skip to content

Commit

Permalink
Don't define 'NOMINMAX' in CMake script, but instead make sure it wor…
Browse files Browse the repository at this point in the history
…ks correctly with 'Windows.h'
  • Loading branch information
helmesjo authored and krzysztof-jusiak committed Dec 18, 2023
1 parent 5fd8c5d commit 93cfddb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
1 change: 0 additions & 1 deletion cmake/AddCustomCommandOrTest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ function(ut_add_custom_command_or_test)
include(CMakeParseArguments)
cmake_parse_arguments("${prefix}" "${noValues}" "${singleValues}" "${multiValues}" ${ARGN})
target_link_libraries(${PARSE_TARGET} PRIVATE Boost::ut)
target_compile_definitions(${PARSE_TARGET} PRIVATE NOMINMAX)

if(BOOST_UT_ENABLE_RUN_AFTER_BUILD)
add_custom_command(TARGET ${PARSE_TARGET} COMMAND ${PARSE_COMMAND})
Expand Down
11 changes: 11 additions & 0 deletions include/boost/ut.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ export import std;
#endif

#include <version>
#if defined(_MSC_VER)
#pragma push_macro("min")
#pragma push_macro("max")
#undef min
#undef max
#endif
// Before libc++ 17 had experimental support for format and it required a
// special build flag. Currently libc++ has not implemented all C++20 chrono
// improvements. Therefore doesn't define __cpp_lib_format, instead query the
Expand Down Expand Up @@ -3283,4 +3289,9 @@ __attribute__((constructor)) inline void cmd_line_args(int argc,
// For MSVC, largc/largv are initialized with __argc/__argv
#endif

#if defined(_MSC_VER)
#pragma pop_macro("min")
#pragma pop_macro("max")
#endif

#endif
10 changes: 8 additions & 2 deletions test/ut/win_compat_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@
//

// ensure no conflict between `Windows.h` and `ut.hpp`
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <Windows.h>

#if not defined(min) || not defined(max)
#error 'min' and 'max' should be defined
#endif

#include "boost/ut.hpp"

#if not defined(min) || not defined(max)
#error 'min' and 'max' should still be defined
#endif

namespace ut = boost::ut;

int main() {
Expand Down

0 comments on commit 93cfddb

Please sign in to comment.