Browse Source

Merge pull request #4444 from EYHN/windows-crt

Add USE_STATIC_CRT option
Kim Kulling 3 years ago
parent
commit
784d9615f5
1 changed files with 21 additions and 0 deletions
  1. 21 0
      CMakeLists.txt

+ 21 - 0
CMakeLists.txt

@@ -150,6 +150,27 @@ IF (WIN32)
       # Multibyte character set is deprecated since at least MSVC2015 (possibly earlier)
       ADD_DEFINITIONS( -DUNICODE -D_UNICODE )
     ENDIF()
+
+    # Link statically against c/c++ lib to avoid missing redistriburable such as
+    # "VCRUNTIME140.dll not found. Try reinstalling the app.", but give users
+    # a choice to opt for the shared runtime if they want.
+    option(USE_STATIC_CRT "Link against the static runtime libraries." OFF)
+
+    # The CMAKE_CXX_FLAGS vars can be overriden by some Visual Studio generators, so we use an alternative
+    # global method here:
+    if (${USE_STATIC_CRT})
+      add_compile_options(
+          $<$<CONFIG:>:/MT>
+          $<$<CONFIG:Debug>:/MTd>
+          $<$<CONFIG:Release>:/MT>
+      )
+    else()
+      add_compile_options(
+          $<$<CONFIG:>:/MD>
+          $<$<CONFIG:Debug>:/MDd>
+          $<$<CONFIG:Release>:/MD>
+      )
+    endif()
   ENDIF()
 ENDIF()