From 129a7c875fc89309a2ab2fbbc940268bbf42b024 Mon Sep 17 00:00:00 2001 From: veawor Date: Mon, 21 Oct 2019 20:16:58 +0800 Subject: [PATCH] Make shared libraries be able to link to MSVC static runtime libraries, so that VC runtime is not required. --- cmake/CMakeLists.txt | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index 8e5e68073..0ae543567 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -40,8 +40,6 @@ else (BUILD_SHARED_LIBS) endif (BUILD_SHARED_LIBS) option(protobuf_BUILD_SHARED_LIBS "Build Shared Libraries" ${protobuf_BUILD_SHARED_LIBS_DEFAULT}) include(CMakeDependentOption) -cmake_dependent_option(protobuf_MSVC_STATIC_RUNTIME "Link static runtime libraries" ON - "NOT protobuf_BUILD_SHARED_LIBS" OFF) set(protobuf_WITH_ZLIB_DEFAULT ON) option(protobuf_WITH_ZLIB "Build with zlib support" ${protobuf_WITH_ZLIB_DEFAULT}) set(protobuf_DEBUG_POSTFIX "d" @@ -155,22 +153,22 @@ if (protobuf_BUILD_SHARED_LIBS) set(protobuf_SHARED_OR_STATIC "SHARED") else (protobuf_BUILD_SHARED_LIBS) set(protobuf_SHARED_OR_STATIC "STATIC") - # In case we are building static libraries, link also the runtime library statically - # so that MSVCR*.DLL is not required at runtime. - # https://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx - # This is achieved by replacing msvc option /MD with /MT and /MDd with /MTd - # http://www.cmake.org/Wiki/CMake_FAQ#How_can_I_build_my_MSVC_application_with_a_static_runtime.3F - if (MSVC AND protobuf_MSVC_STATIC_RUNTIME) - foreach(flag_var - CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE - CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO) - if(${flag_var} MATCHES "/MD") - string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}") - endif(${flag_var} MATCHES "/MD") - endforeach(flag_var) - endif (MSVC AND protobuf_MSVC_STATIC_RUNTIME) endif (protobuf_BUILD_SHARED_LIBS) +# In case we are linking the runtime library statically so that MSVCR*.DLL is not required at runtime. +# https://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx +# This is achieved by replacing msvc option /MD with /MT and /MDd with /MTd +# http://www.cmake.org/Wiki/CMake_FAQ#How_can_I_build_my_MSVC_application_with_a_static_runtime.3F +if (MSVC AND protobuf_MSVC_STATIC_RUNTIME) + foreach(flag_var + CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE + CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO) + if(${flag_var} MATCHES "/MD") + string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}") + endif(${flag_var} MATCHES "/MD") + endforeach(flag_var) +endif (MSVC AND protobuf_MSVC_STATIC_RUNTIME) + if (MSVC) # Build with multiple processes add_definitions(/MP)