Merge pull request #702 from jtattermusch/visual_studio_build_static_runtime

Link runtime statically for VS build
This commit is contained in:
Feng Xiao 2015-08-12 14:11:12 -07:00
commit 38a56ee4b1

View File

@ -43,6 +43,19 @@ endif (HAVE_ZLIB)
if (MSVC)
if (BUILD_SHARED_LIBS)
add_definitions(-DPROTOBUF_USE_DLLS)
else (BUILD_SHARED_LIBS)
# 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
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 (BUILD_SHARED_LIBS)
add_definitions(/wd4244 /wd4267 /wd4018 /wd4355 /wd4800 /wd4251 /wd4996 /wd4146 /wd4305)
endif (MSVC)