Skip to content

Conversation

@charles-lunarg
Copy link

@charles-lunarg charles-lunarg commented Nov 26, 2025

CMake 3.15 added MSVC_RUNTIME_LIBRARY, an official mechanism to configure the runtime library used by targets. Setting the property will properly set the MSVC runtime library regardless of the values in the compiler flags variables.

Fixes #4731


target_compile_features(${name} PUBLIC cxx_std_17)

if (NOT BUILD_SHARED_LIBS AND NOT gtest_force_shared_crt)
Copy link

@mathstuf mathstuf Nov 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can AND NOT DEFINED CMAKE_MSVC_RUNTIME_LIBRARY be added here so that it can be controlled from without using the property's initializer variable?

Copy link
Author

@charles-lunarg charles-lunarg Nov 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see the utility of having CMAKE_MSVC_RUNTIME_LIBRARY be respected when set, since that is the 'standard' way to control runtime library behavior in CMake. I should probably add a blurb to the docs to indicate the gtest_force_shared_crt option being superceeded by CMAKE_MSVC_RUNTIME_LIBRARY.

Initially, I was going to push back on the suggestion, citing that setting CMAKE_MSVC_RUNTIME_LIBRARY would cause gtest_force_shared_crt to no longer function which breaks (what I gather to be) the pattern googletest's CMake uses, which is to isolate as much of the external input as possible. But, gtest_force_shared_crt exists only because CMAKE_MSVC_RUNTIME_LIBRARY didn't exist, so in the future its forseeable that gtest_force_shared_crt could be removed.

Actually, putting it like that I wonder if a better PR is to set CMAKE_MSVC_RUNTIME_LIBRARY based on gtest_force_shared_crt, so the default is correct everywhere, rather than needing to set a property on every target individually. Thoughts? (I haven't investigated this, so do let me know if this is not a good idea).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about deferring to CMAKE_MSVC_RUNTIME_LIBRARY if it is set and deprecating gtest_force_shared_crt's use in conjunction with it?

Something like:

if (NOT DEFINED CMAKE_MSVC_RUNTIME_LIBRARY)
  option(gtest_force_shared_crt …)
  if (some_suitable_condition)
    message(DEPRECATION
      "gtest_force_shared_crt is deprecated in preference to `CMAKE_MSVC_RUNTIME_LIBRARY` instead")
  endif ()
  if (gtest_force_shared_crt)
    set(CMAKE_MSVC_RUNTIME_LIBRARY "…")
  else ()
    set(CMAKE_MSVC_RUNTIME_LIBRARY "…")
  endif ()
elseif (DEFINED gtest_force_shared_crt)
  message(DEPRECATION
    "Since CMAKE_MSVC_RUNTIME_LIBRARY is set, gtest_force_shared_crt settings are ignored")
endif ()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've done you one better and went full on deprecating gtest_force_shared_crt. It was broken before, no sense restoring 'old' behavior that wasn't working. I'd love extra help refining the README.md to make it clear how to 'use CMAKE_MSVC_RUNTIME_LIBRARY' to fix this problem, as setting an option is straightforward while adding a chunk of code or a command line variable with a complex body isn't. It almost makes me think keeping gtest_force_shared_crt around to simplify using the library is preferable.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do something like:

MultiThreaded$<$<CONFIG:Debug>:Debug>$<$<OR:$<BOOL:${gtest_force_shared_crt}>,$<BOOL:${BUILD_SHARED_LIBS}>>:DLL>

Copy link
Author

@charles-lunarg charles-lunarg Dec 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have reverted the change that would deprecate gtest_force_shared_crt and simply add the condition to not set CMAKE_MSVC_RUNTIME_LIBRARY if it is already defined, as you asked.

Would rather this be a "No rock the boat" PR so that it is easy for maintainers to accept.

Edit: Also I reverted the changes to the readme since they were only needed with deprecation.

@charles-lunarg charles-lunarg force-pushed the fix_CMake_option_gtest_force_shared_crt branch 2 times, most recently from 96a33a9 to 572b384 Compare December 3, 2025 21:49
With CMake 3.15, a new mechanism was added to control which CRT
libraries link to. Because the minimum CMake version is above 3.15,
googletest can leverage it in the implementation of
gtest_force_shared_crt.

If CMAKE_MSVC_RUNTIME_LIBRARY is already defined, googletest will
respect its value rather than replace it, allowing for users to
customize the exact CRT used by googletest.
@charles-lunarg charles-lunarg force-pushed the fix_CMake_option_gtest_force_shared_crt branch from 572b384 to 0b65649 Compare December 3, 2025 21:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: gtest_force_shared_crt on Windows no longer selects /MT with static library build option

2 participants