导入 Protobuf 时出现 RuntimeLibrary 不匹配错误(Win64、CMake)

RuntimeLibrary mismatch error when importing Protobuf (Win64, CMake)

我有一个项目,架构如下:

├── CMakeLists.txt
├── projects
│   ├── CMakeLists.txt
│   ├── io
│   │   ├── CMakeLists.txt
│   │   ├── data.proto
│   │   └── generated
│   │       ├── data.pb.cc
│   │       └── data.pb.h
│   ├── service
│   │   ├── CMakeLists.txt
│   │   └── main.cpp
│   ├── dll_project_1
│   └── dll_project_2
└── third_party
    └── protobuf
        ├── bin
        ├── cmake
        ├── include
        └── lib

projects 目录包含我所有的项目(显然):

third_party/protobuf 包含列出的所有 protobuf 安装文件 here. Note that, to install protobuf, I followed all the instructions here(使用 Visual Studio 进行编译和安装,在 Debug x64 和发布 x64).

CMakeLists.txt如下:

./CMakeLists.txt

set(THIRD_PARTY ${CMAKE_CURRENT_SOURCE_DIR}/third_party)
set(PROTOBUF ${THIRD_PARTY}/protobuf)

set(Protobuf_USE_STATIC_LIBS ON)

include(${PROTOBUF}/cmake/protobuf-config.cmake)
include(${PROTOBUF}/cmake/protobuf-module.cmake)
include(${PROTOBUF}/cmake/protobuf-options.cmake)
include(${PROTOBUF}/cmake/protobuf-targets.cmake)

find_package(Protobuf REQUIRED HINTS ${PROTOBUF})
include_directories(${Protobuf_INCLUDE_DIRS})
link_libraries(${Protobuf_LIBRARIES})

./projects/CMakeLists.txt

add_subdirectory(io)
add_subdirectory(dll_project_1)
add_subdirectory(dll_project_2)

add_subdirectory(service)

./projects/io/CMakeLists.txt

set(PROTO
    data.proto
)

set(PROTO_GENERATED
    generated/data.pb.cc
    generated/data.pb.h
)

source_group(Proto FILES ${PROTO})
source_group(Generated FILES ${PROTO_GENERATED})

add_library(IO STATIC ${PROTO} ${PROTO_GENERATED})

./projects/service/CMakeLists.txt

add_executable(Service main.cpp)

target_link_libraries(Service
    IO
    Dll1
    Dll2
)

现在 cmake 可以构建解决方案,而我只是想做的是:

#include "../io/generated/data.pb.h"

int main()
{
    Data::SomeData d;
    d.set_value(0.0);
}

但是当我尝试编译时,出现了很多 LNK2038 和 LNK2005 错误:

6>main.cpp
6>libprotobufd.lib(common.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libprotobufd.lib(common.obj) : warning LNK4099: PDB 'libprotobuf.pdb' was not found with 'libprotobufd.lib(common.obj)' or at 'C:\dev\MyProject\build\Debug\libprotobuf.pdb'; linking object as if no debug info
6>libprotobufd.lib(coded_stream.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libprotobufd.lib(coded_stream.obj) : warning LNK4099: PDB 'libprotobuf.pdb' was not found with 'libprotobufd.lib(coded_stream.obj)' or at 'C:\dev\MyProject\build\Debug\libprotobuf.pdb'; linking object as if no debug info
6>libprotobufd.lib(arena.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libprotobufd.lib(arena.obj) : warning LNK4099: PDB 'libprotobuf.pdb' was not found with 'libprotobufd.lib(arena.obj)' or at 'C:\dev\MyProject\build\Debug\libprotobuf.pdb'; linking object as if no debug info
6>libprotobufd.lib(message_lite.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libprotobufd.lib(message_lite.obj) : warning LNK4099: PDB 'libprotobuf.pdb' was not found with 'libprotobufd.lib(message_lite.obj)' or at 'C:\dev\MyProject\build\Debug\libprotobuf.pdb'; linking object as if no debug info
6>libprotobufd.lib(parse_context.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libprotobufd.lib(parse_context.obj) : warning LNK4099: PDB 'libprotobuf.pdb' was not found with 'libprotobufd.lib(parse_context.obj)' or at 'C:\dev\MyProject\build\Debug\libprotobuf.pdb'; linking object as if no debug info
6>libprotobufd.lib(generated_message_util.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libprotobufd.lib(generated_message_util.obj) : warning LNK4099: PDB 'libprotobuf.pdb' was not found with 'libprotobufd.lib(generated_message_util.obj)' or at 'C:\dev\MyProject\build\Debug\libprotobuf.pdb'; linking object as if no debug info
6>libprotobufd.lib(unknown_field_set.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libprotobufd.lib(unknown_field_set.obj) : warning LNK4099: PDB 'libprotobuf.pdb' was not found with 'libprotobufd.lib(unknown_field_set.obj)' or at 'C:\dev\MyProject\build\Debug\libprotobuf.pdb'; linking object as if no debug info
6>libprotobufd.lib(generated_message_reflection.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libprotobufd.lib(generated_message_reflection.obj) : warning LNK4099: PDB 'libprotobuf.pdb' was not found with 'libprotobufd.lib(generated_message_reflection.obj)' or at 'C:\dev\MyProject\build\Debug\libprotobuf.pdb'; linking object as if no debug info
6>libprotobufd.lib(message.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libprotobufd.lib(message.obj) : warning LNK4099: PDB 'libprotobuf.pdb' was not found with 'libprotobufd.lib(message.obj)' or at 'C:\dev\MyProject\build\Debug\libprotobuf.pdb'; linking object as if no debug info
6>libprotobufd.lib(reflection_ops.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libprotobufd.lib(reflection_ops.obj) : warning LNK4099: PDB 'libprotobuf.pdb' was not found with 'libprotobufd.lib(reflection_ops.obj)' or at 'C:\dev\MyProject\build\Debug\libprotobuf.pdb'; linking object as if no debug info
6>libprotobufd.lib(wire_format.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libprotobufd.lib(wire_format.obj) : warning LNK4099: PDB 'libprotobuf.pdb' was not found with 'libprotobufd.lib(wire_format.obj)' or at 'C:\dev\MyProject\build\Debug\libprotobuf.pdb'; linking object as if no debug info
6>libprotobufd.lib(status.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libprotobufd.lib(status.obj) : warning LNK4099: PDB 'libprotobuf.pdb' was not found with 'libprotobufd.lib(status.obj)' or at 'C:\dev\MyProject\build\Debug\libprotobuf.pdb'; linking object as if no debug info
6>libprotobufd.lib(int128.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libprotobufd.lib(int128.obj) : warning LNK4099: PDB 'libprotobuf.pdb' was not found with 'libprotobufd.lib(int128.obj)' or at 'C:\dev\MyProject\build\Debug\libprotobuf.pdb'; linking object as if no debug info
6>libprotobufd.lib(stringpiece.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libprotobufd.lib(stringpiece.obj) : warning LNK4099: PDB 'libprotobuf.pdb' was not found with 'libprotobufd.lib(stringpiece.obj)' or at 'C:\dev\MyProject\build\Debug\libprotobuf.pdb'; linking object as if no debug info
6>libprotobufd.lib(strutil.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libprotobufd.lib(strutil.obj) : warning LNK4099: PDB 'libprotobuf.pdb' was not found with 'libprotobufd.lib(strutil.obj)' or at 'C:\dev\MyProject\build\Debug\libprotobuf.pdb'; linking object as if no debug info
6>libprotobufd.lib(zero_copy_stream.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libprotobufd.lib(zero_copy_stream_impl_lite.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libprotobufd.lib(zero_copy_stream.obj) : warning LNK4099: PDB 'libprotobuf.pdb' was not found with 'libprotobufd.lib(zero_copy_stream.obj)' or at 'C:\dev\MyProject\build\Debug\libprotobuf.pdb'; linking object as if no debug info
6>libprotobufd.lib(zero_copy_stream_impl_lite.obj) : warning LNK4099: PDB 'libprotobuf.pdb' was not found with 'libprotobufd.lib(zero_copy_stream_impl_lite.obj)' or at 'C:\dev\MyProject\build\Debug\libprotobuf.pdb'; linking object as if no debug info
6>libprotobufd.lib(zero_copy_stream_impl.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libprotobufd.lib(zero_copy_stream_impl.obj) : warning LNK4099: PDB 'libprotobuf.pdb' was not found with 'libprotobufd.lib(zero_copy_stream_impl.obj)' or at 'C:\dev\MyProject\build\Debug\libprotobuf.pdb'; linking object as if no debug info
6>libprotobufd.lib(structurally_valid.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libprotobufd.lib(structurally_valid.obj) : warning LNK4099: PDB 'libprotobuf.pdb' was not found with 'libprotobufd.lib(structurally_valid.obj)' or at 'C:\dev\MyProject\build\Debug\libprotobuf.pdb'; linking object as if no debug info
6>libprotobufd.lib(repeated_field.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libprotobufd.lib(repeated_field.obj) : warning LNK4099: PDB 'libprotobuf.pdb' was not found with 'libprotobufd.lib(repeated_field.obj)' or at 'C:\dev\MyProject\build\Debug\libprotobuf.pdb'; linking object as if no debug info
6>libprotobufd.lib(wire_format_lite.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libprotobufd.lib(wire_format_lite.obj) : warning LNK4099: PDB 'libprotobuf.pdb' was not found with 'libprotobufd.lib(wire_format_lite.obj)' or at 'C:\dev\MyProject\build\Debug\libprotobuf.pdb'; linking object as if no debug info
6>libprotobufd.lib(extension_set.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libprotobufd.lib(extension_set.obj) : warning LNK4099: PDB 'libprotobuf.pdb' was not found with 'libprotobufd.lib(extension_set.obj)' or at 'C:\dev\MyProject\build\Debug\libprotobuf.pdb'; linking object as if no debug info
6>libprotobufd.lib(descriptor.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libprotobufd.lib(descriptor.obj) : warning LNK4099: PDB 'libprotobuf.pdb' was not found with 'libprotobufd.lib(descriptor.obj)' or at 'C:\dev\MyProject\build\Debug\libprotobuf.pdb'; linking object as if no debug info
6>libprotobufd.lib(arenastring.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libprotobufd.lib(arenastring.obj) : warning LNK4099: PDB 'libprotobuf.pdb' was not found with 'libprotobufd.lib(arenastring.obj)' or at 'C:\dev\MyProject\build\Debug\libprotobuf.pdb'; linking object as if no debug info
6>libprotobufd.lib(extension_set_heavy.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libprotobufd.lib(extension_set_heavy.obj) : warning LNK4099: PDB 'libprotobuf.pdb' was not found with 'libprotobufd.lib(extension_set_heavy.obj)' or at 'C:\dev\MyProject\build\Debug\libprotobuf.pdb'; linking object as if no debug info
6>libprotobufd.lib(map_field.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libprotobufd.lib(map_field.obj) : warning LNK4099: PDB 'libprotobuf.pdb' was not found with 'libprotobufd.lib(map_field.obj)' or at 'C:\dev\MyProject\build\Debug\libprotobuf.pdb'; linking object as if no debug info
6>libprotobufd.lib(io_win32.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libprotobufd.lib(io_win32.obj) : warning LNK4099: PDB 'libprotobuf.pdb' was not found with 'libprotobufd.lib(io_win32.obj)' or at 'C:\dev\MyProject\build\Debug\libprotobuf.pdb'; linking object as if no debug info
6>libprotobufd.lib(implicit_weak_message.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libprotobufd.lib(implicit_weak_message.obj) : warning LNK4099: PDB 'libprotobuf.pdb' was not found with 'libprotobufd.lib(implicit_weak_message.obj)' or at 'C:\dev\MyProject\build\Debug\libprotobuf.pdb'; linking object as if no debug info
6>libprotobufd.lib(stringprintf.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libprotobufd.lib(stringprintf.obj) : warning LNK4099: PDB 'libprotobuf.pdb' was not found with 'libprotobufd.lib(stringprintf.obj)' or at 'C:\dev\MyProject\build\Debug\libprotobuf.pdb'; linking object as if no debug info
6>libprotobufd.lib(text_format.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libprotobufd.lib(text_format.obj) : warning LNK4099: PDB 'libprotobuf.pdb' was not found with 'libprotobufd.lib(text_format.obj)' or at 'C:\dev\MyProject\build\Debug\libprotobuf.pdb'; linking object as if no debug info
6>libprotobufd.lib(descriptor.pb.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libprotobufd.lib(descriptor.pb.obj) : warning LNK4099: PDB 'libprotobuf.pdb' was not found with 'libprotobufd.lib(descriptor.pb.obj)' or at 'C:\dev\MyProject\build\Debug\libprotobuf.pdb'; linking object as if no debug info
6>libprotobufd.lib(tokenizer.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libprotobufd.lib(tokenizer.obj) : warning LNK4099: PDB 'libprotobuf.pdb' was not found with 'libprotobufd.lib(tokenizer.obj)' or at 'C:\dev\MyProject\build\Debug\libprotobuf.pdb'; linking object as if no debug info
6>libprotobufd.lib(descriptor_database.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libprotobufd.lib(descriptor_database.obj) : warning LNK4099: PDB 'libprotobuf.pdb' was not found with 'libprotobufd.lib(descriptor_database.obj)' or at 'C:\dev\MyProject\build\Debug\libprotobuf.pdb'; linking object as if no debug info
6>libprotobufd.lib(dynamic_message.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libprotobufd.lib(dynamic_message.obj) : warning LNK4099: PDB 'libprotobuf.pdb' was not found with 'libprotobufd.lib(dynamic_message.obj)' or at 'C:\dev\MyProject\build\Debug\libprotobuf.pdb'; linking object as if no debug info
6>libprotobufd.lib(substitute.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libprotobufd.lib(substitute.obj) : warning LNK4099: PDB 'libprotobuf.pdb' was not found with 'libprotobufd.lib(substitute.obj)' or at 'C:\dev\MyProject\build\Debug\libprotobuf.pdb'; linking object as if no debug info
6>libprotobufd.lib(strtod.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libprotobufd.lib(strtod.obj) : warning LNK4099: PDB 'libprotobuf.pdb' was not found with 'libprotobufd.lib(strtod.obj)' or at 'C:\dev\MyProject\build\Debug\libprotobuf.pdb'; linking object as if no debug info
6>libprotobufd.lib(any_lite.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libprotobufd.lib(any_lite.obj) : warning LNK4099: PDB 'libprotobuf.pdb' was not found with 'libprotobufd.lib(any_lite.obj)' or at 'C:\dev\MyProject\build\Debug\libprotobuf.pdb'; linking object as if no debug info
6>libprotobufd.lib(map.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libprotobufd.lib(map.obj) : warning LNK4099: PDB 'libprotobuf.pdb' was not found with 'libprotobufd.lib(map.obj)' or at 'C:\dev\MyProject\build\Debug\libprotobuf.pdb'; linking object as if no debug info
6>libprotobufd.lib(any.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libprotobufd.lib(any.obj) : warning LNK4099: PDB 'libprotobuf.pdb' was not found with 'libprotobufd.lib(any.obj)' or at 'C:\dev\MyProject\build\Debug\libprotobuf.pdb'; linking object as if no debug info
6>libcpmtd.lib(locale0.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libcpmtd.lib(locale0.obj) : error LNK2005: "void __cdecl std::_Facet_Register(class std::_Facet_base *)" (?_Facet_Register@std@@YAXPEAV_Facet_base@1@@Z) already defined in msvcprtd.lib(locale0_implib.obj)
6>libcpmtd.lib(locale0.obj) : error LNK2005: "private: static class std::locale::_Locimp * __cdecl std::locale::_Getgloballocale(void)" (?_Getgloballocale@locale@std@@CAPEAV_Locimp@12@XZ) already defined in msvcprtd.lib(MSVCP140D.dll)
6>libcpmtd.lib(locale0.obj) : error LNK2005: "private: static class std::locale::_Locimp * __cdecl std::locale::_Init(bool)" (?_Init@locale@std@@CAPEAV_Locimp@12@_N@Z) already defined in msvcprtd.lib(MSVCP140D.dll)
6>libcpmtd.lib(locale0.obj) : error LNK2005: "public: static void __cdecl std::_Locinfo::_Locinfo_ctor(class std::_Locinfo *,char const *)" (?_Locinfo_ctor@_Locinfo@std@@SAXPEAV12@PEBD@Z) already defined in msvcprtd.lib(MSVCP140D.dll)
6>libcpmtd.lib(locale0.obj) : error LNK2005: "public: static void __cdecl std::_Locinfo::_Locinfo_dtor(class std::_Locinfo *)" (?_Locinfo_dtor@_Locinfo@std@@SAXPEAV12@@Z) already defined in msvcprtd.lib(MSVCP140D.dll)
6>libcpmtd.lib(locale.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libcpmtd.lib(iosptrs.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libcpmtd.lib(wlocale.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libcpmtd.lib(xlocale.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libcpmtd.lib(xstol.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libcpmtd.lib(xstoul.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libcpmtd.lib(xstoll.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libcpmtd.lib(xstoull.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libcpmtd.lib(xlock.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libcpmtd.lib(xlock.obj) : error LNK2005: "public: __cdecl std::_Lockit::_Lockit(int)" (??0_Lockit@std@@QEAA@H@Z) already defined in msvcprtd.lib(MSVCP140D.dll)
6>libcpmtd.lib(xlock.obj) : error LNK2005: "public: __cdecl std::_Lockit::~_Lockit(void)" (??1_Lockit@std@@QEAA@XZ) already defined in msvcprtd.lib(MSVCP140D.dll)
6>libcpmtd.lib(xstrcoll.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libcpmtd.lib(xdateord.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libcpmtd.lib(xwcscoll.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libcpmtd.lib(xwcsxfrm.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libcpmtd.lib(xstrxfrm.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libcpmtd.lib(xmtx.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libcpmtd.lib(StlCompareStringA.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libcpmtd.lib(winapinls.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libcpmtd.lib(StlCompareStringW.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libcpmtd.lib(StlLCMapStringW.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libcpmtd.lib(StlLCMapStringA.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>libcpmtd.lib(winapisupp.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj
6>MSVCRTD.lib(initializers.obj) : warning LNK4098: defaultlib 'libcmtd.lib' conflicts with use of other libs; use /NODEFAULTLIB:library
6>C:\dev\MyProject\build\Debug\Service.exe : fatal error LNK1169: one or more multiply defined symbols found
6>Done building project "Service.vcxproj" -- FAILED.

我首先找到了 this SO question,但我看不到如何做用 cmake 回答的问题..

好吧,一如既往,我需要问一个问题,让我在几个小时后才能自己找到解决方案...

答案是. Just needed to add -Dprotobuf_MSVC_STATIC_RUNTIME=OFF in the cmake command to build the solution (this step):

cmake -G "Visual Studio 16 2019" -Dprotobuf_MSVC_STATIC_RUNTIME=OFF -DCMAKE_INSTALL_PREFIX=../../../install ../..