'Configuring protobuf C++ to dynamically link against MSVC runtime library

I would like to configure protobuf to link MSVC runtime library dynamically. While this is supported by protobuf and seems trivial to do, I have not been able to do that.

Here's my CMake command (through bat file, execute from protobuf's source code root directory)

setlocal
@call  "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Auxiliary\Build\vcvars64.bat"

cd cmake
mkdir build & cd build

cmake -G "Visual Studio 16 2019" -DCMAKE_INSTALL_PREFIX=../../install -Dprotobuf_BUILD_SHARED_LIBS=OFF -Dprotobuf_MSVC_STATIC_RUNTIME=OFF -Dprotobuf_BUILD_TESTS=OFF ..
if %ERRORLEVEL% NEQ 0 EXIT /B 1

cmake --build . --config Release
if %ERRORLEVEL% NEQ 0 EXIT /B 1

cmake --install .
if %ERRORLEVEL% NEQ 0 EXIT /B 1

cd ../..
mkdir dist
tar -C install -czf dist/protobuf.tar.gz .
if %ERRORLEVEL% NEQ 0 EXIT /B 1

However, whenever I checked with dumpbin, this is what I got:

dumpbin /nologo /directives libprotobuf.lib

Dump of file libprotobuf.lib

File Type: LIBRARY

   Linker Directives
   -----------------
   /FAILIFMISMATCH:_CRT_STDIO_ISO_WIDE_SPECIFIERS=0
   /FAILIFMISMATCH:_MSC_VER=1900
   /FAILIFMISMATCH:_ITERATOR_DEBUG_LEVEL=0
   /FAILIFMISMATCH:RuntimeLibrary=MT_StaticRelease
   /DEFAULTLIB:libcpmt
   /DEFAULTLIB:LIBCMT
   /DEFAULTLIB:OLDNAMES

Based on /FAILIFMISMATCH:RuntimeLibrary=MT_StaticRelease , I assume this is still linking statically against MSVC? If yes, how should I configure my CMake command in order to achieve what I want?



Solution 1:[1]

To linkt MSVC runtime dynamically, you are looking for protobuf_MSVC_STATIC_RUNTIME=OFF

Building ProtoBuf this way fixes errors like libprotobuf.lib(common.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MT_StaticRelease' doesn't match value 'MD_DynamicRelease' for default CMake settings in Visual Studio.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JaM