在Windows上使用gRPC C++

26
我已经努力寻找在Windows上安装和使用gRPC的方法三天了,但一直没有成功。 我正在使用Visual Studio 2015和Win7 64位操作系统。为了保险起见,我将逐步描述我的操作步骤。这可能并不是必要的,但我是一个C++和VS的初学者,所以我不确定我的操作是否正确。(按照指南http://www.infopulse.com/blog/grpc-framework-by-google-tutorial/进行)。
从 git 获取 gRPC,初始化子模块
为 protobuf 获取 gmock 和 gtest(指南中没有提到,但否则无法正常工作)
在 protobuf 上运行 cmake 在 Visual Studio 中以 Release 模式构建 protobuf.sln(对于解决方案的每个部分,在 Property Manager 中设置 C/C++ > Code Generation > Runtime Library > /MDd)
将 Release/ 文件夹复制到 protobuf/cmake/ 中(而不是按照说明中的 Debug/,这会给我 libprotobufd.lib 而不是所需的 libprotobuf.lib)
同样以 Release 模式构建 grpc/vsprojects/grpc_protoc_plugins.sln,并再次为解决方案的每个部分设置 Property Manager > C/C++ > Code Generation > Runtime Library > /MDd
将 protobuf/cmake/Release 中的 protoc.exe 复制到先前创建的 grpc/vsprojects/Release 中
在 Visual Studio 中以 Debug 模式构建 grpc.sln(仅构建 grpc++ 部分,因为我在某个地方读到过,并且再次为解决方案的每个部分设置 Property Manager > C/C++ > Code Generation > Runtime Library > /MDd)

目前情况良好。

  • 从示例文件夹中的proto生成c文件。我得到了helloworld.grpc.pb.cchelloworld.grpc.pb.hhelloworld.pb.cchelloworld.pb.h,然后将它们全部移动到grpc/examples/cpp/helloworld:

    protoc --grpc_out=./hello_proto --plugin=protoc-gen-grpc=grpc_cpp_plugin.exe ../../examples/protos/helloworld.proto --proto_path=../../examples/protos

    protoc --cpp_out=./hello_proto ../../examples/protos/helloworld.proto --proto_path=../../examples/protos

  • 我在VS中保持grpc.sln打开状态,并选择'添加' > '新建项目'

  • 对于新项目,'添加' > '现有项',并添加grpc/examples/cpp中的greeter_client.cc
  • 将依赖项添加为:https://github.com/grpc/grpc/issues/4707,包含在C/C++ > 附加包含目录中
当我尝试构建我的项目时,会报告找不到gflags、gtest和libprotobuf的错误。如果我找到它们并将它们移动到一个包含的文件夹中,那么我会得到以下错误:
    1>------ Build started: Project: greeter_client, Configuration: Debug Win32 ------
1>  greeter_client.cc
1>libprotobuf.lib(generated_message_util.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in greeter_client.obj
1>libprotobuf.lib(generated_message_util.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MT_StaticRelease' doesn't match value 'MDd_DynamicDebug' in greeter_client.obj
1>libprotobuf.lib(common.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in greeter_client.obj
1>libprotobuf.lib(common.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MT_StaticRelease' doesn't match value 'MDd_DynamicDebug' in greeter_client.obj
1>libprotobuf.lib(once.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in greeter_client.obj
1>libprotobuf.lib(once.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MT_StaticRelease' doesn't match value 'MDd_DynamicDebug' in greeter_client.obj
1>libprotobuf.lib(status.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in greeter_client.obj
1>libprotobuf.lib(status.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MT_StaticRelease' doesn't match value 'MDd_DynamicDebug' in greeter_client.obj
1>libprotobuf.lib(int128.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in greeter_client.obj
1>libprotobuf.lib(int128.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MT_StaticRelease' doesn't match value 'MDd_DynamicDebug' in greeter_client.obj
1>libprotobuf.lib(atomicops_internals_x86_msvc.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in greeter_client.obj
1>libprotobuf.lib(atomicops_internals_x86_msvc.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MT_StaticRelease' doesn't match value 'MDd_DynamicDebug' in greeter_client.obj
1>grpc++.lib(client_context.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in greeter_client.obj
1>grpc++.lib(insecure_credentials.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in greeter_client.obj
1>grpc++.lib(create_channel.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in greeter_client.obj
1>grpc++.lib(credentials.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in greeter_client.obj
1>grpc++.lib(create_channel_internal.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in greeter_client.obj
1>grpc++.lib(channel_arguments.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in greeter_client.obj
1>grpc++.lib(core_codegen.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in greeter_client.obj
1>grpc++.lib(codegen_init.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in greeter_client.obj
1>grpc++.lib(status.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in greeter_client.obj
1>msvcprtd.lib(MSVCP140D.dll) : error LNK2005: "public: bool __thiscall std::ios_base::good(void)const " (?good@ios_base@std@@QBE_NXZ) already defined in grpc++.lib(channel_arguments.obj)
1>msvcprtd.lib(MSVCP140D.dll) : error LNK2005: "public: int __thiscall std::ios_base::flags(void)const " (?flags@ios_base@std@@QBEHXZ) already defined in grpc++.lib(channel_arguments.obj)
1>msvcprtd.lib(MSVCP140D.dll) : error LNK2005: "public: __int64 __thiscall std::ios_base::width(void)const " (?width@ios_base@std@@QBE_JXZ) already defined in grpc++.lib(channel_arguments.obj)
1>msvcprtd.lib(MSVCP140D.dll) : error LNK2005: "public: __int64 __thiscall std::ios_base::width(__int64)" (?width@ios_base@std@@QAE_J_J@Z) already defined in grpc++.lib(channel_arguments.obj)
1>msvcprtd.lib(MSVCP140D.dll) : error LNK2005: "public: int __thiscall std::basic_streambuf<char,struct std::char_traits<char> >::sputc(char)" (?sputc@?$basic_streambuf@DU?$char_traits@D@std@@@std@@QAEHD@Z) already defined in grpc++.lib(channel_arguments.obj)
1>msvcprtd.lib(MSVCP140D.dll) : error LNK2005: "public: __int64 __thiscall std::basic_streambuf<char,struct std::char_traits<char> >::sputn(char const *,__int64)" (?sputn@?$basic_streambuf@DU?$char_traits@D@std@@@std@@QAE_JPBD_J@Z) already defined in grpc++.lib(channel_arguments.obj)
1>msvcprtd.lib(MSVCP140D.dll) : error LNK2005: "public: void __thiscall std::basic_ios<char,struct std::char_traits<char> >::setstate(int,bool)" (?setstate@?$basic_ios@DU?$char_traits@D@std@@@std@@QAEXH_N@Z) already defined in grpc++.lib(channel_arguments.obj)
1>msvcprtd.lib(MSVCP140D.dll) : error LNK2005: "public: class std::basic_ostream<char,struct std::char_traits<char> > * __thiscall std::basic_ios<char,struct std::char_traits<char> >::tie(void)const " (?tie@?$basic_ios@DU?$char_traits@D@std@@@std@@QBEPAV?$basic_ostream@DU?$char_traits@D@std@@@2@XZ) already defined in grpc++.lib(channel_arguments.obj)
1>msvcprtd.lib(MSVCP140D.dll) : error LNK2005: "public: class std::basic_streambuf<char,struct std::char_traits<char> > * __thiscall std::basic_ios<char,struct std::char_traits<char> >::rdbuf(void)const " (?rdbuf@?$basic_ios@DU?$char_traits@D@std@@@std@@QBEPAV?$basic_streambuf@DU?$char_traits@D@std@@@2@XZ) already defined in grpc++.lib(channel_arguments.obj)
1>msvcprtd.lib(MSVCP140D.dll) : error LNK2005: "public: char __thiscall std::basic_ios<char,struct std::char_traits<char> >::fill(void)const " (?fill@?$basic_ios@DU?$char_traits@D@std@@@std@@QBEDXZ) already defined in grpc++.lib(channel_arguments.obj)
1>msvcprtd.lib(MSVCP140D.dll) : error LNK2005: "public: char __thiscall std::basic_ios<char,struct std::char_traits<char> >::widen(char)const " (?widen@?$basic_ios@DU?$char_traits@D@std@@@std@@QBEDD@Z) already defined in grpc++.lib(channel_arguments.obj)
1>msvcprtd.lib(MSVCP140D.dll) : error LNK2005: "public: void __thiscall std::basic_ostream<char,struct std::char_traits<char> >::_Osfx(void)" (?_Osfx@?$basic_ostream@DU?$char_traits@D@std@@@std@@QAEXXZ) already defined in libprotobuf.lib(status.obj)
1>msvcprtd.lib(MSVCP140D.dll) : error LNK2005: "public: class std::basic_ostream<char,struct std::char_traits<char> > & __thiscall std::basic_ostream<char,struct std::char_traits<char> >::flush(void)" (?flush@?$basic_ostream@DU?$char_traits@D@std@@@std@@QAEAAV12@XZ) already defined in libprotobuf.lib(status.obj)
1>libcpmt.lib(ios.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in greeter_client.obj
1>libcpmt.lib(ios.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MT_StaticRelease' doesn't match value 'MDd_DynamicDebug' in greeter_client.obj
1>libcpmt.lib(ios.obj) : error LNK2005: "public: static void __cdecl std::ios_base::_Addstd(class std::ios_base *)" (?_Addstd@ios_base@std@@SAXPAV12@@Z) already defined in msvcprtd.lib(MSVCP140D.dll)
1>libcpmt.lib(ios.obj) : error LNK2005: "private: static void __cdecl std::ios_base::_Ios_base_dtor(class std::ios_base *)" (?_Ios_base_dtor@ios_base@std@@CAXPAV12@@Z) already defined in msvcprtd.lib(MSVCP140D.dll)
1>libcpmt.lib(locale0.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in greeter_client.obj
1>libcpmt.lib(locale0.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MT_StaticRelease' doesn't match value 'MDd_DynamicDebug' in greeter_client.obj
1>libcpmt.lib(locale0.obj) : error LNK2005: "void __cdecl std::_Facet_Register(class std::_Facet_base *)" (?_Facet_Register@std@@YAXPAV_Facet_base@1@@Z) already defined in msvcprtd.lib(locale0_implib.obj)
1>libcpmt.lib(locale0.obj) : error LNK2005: "private: static class std::locale::_Locimp * __cdecl std::locale::_Getgloballocale(void)" (?_Getgloballocale@locale@std@@CAPAV_Locimp@12@XZ) already defined in msvcprtd.lib(MSVCP140D.dll)
1>libcpmt.lib(locale0.obj) : error LNK2005: "private: static class std::locale::_Locimp * __cdecl std::locale::_Init(bool)" (?_Init@locale@std@@CAPAV_Locimp@12@_N@Z) already defined in msvcprtd.lib(MSVCP140D.dll)
1>libcpmt.lib(locale0.obj) : error LNK2005: "public: static void __cdecl std::_Locinfo::_Locinfo_ctor(class std::_Locinfo *,char const *)" (?_Locinfo_ctor@_Locinfo@std@@SAXPAV12@PBD@Z) already defined in msvcprtd.lib(MSVCP140D.dll)
1>libcpmt.lib(locale0.obj) : error LNK2005: "public: static void __cdecl std::_Locinfo::_Locinfo_dtor(class std::_Locinfo *)" (?_Locinfo_dtor@_Locinfo@std@@SAXPAV12@@Z) already defined in msvcprtd.lib(MSVCP140D.dll)
1>libcpmt.lib(iosptrs.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in greeter_client.obj
1>libcpmt.lib(iosptrs.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MT_StaticRelease' doesn't match value 'MDd_DynamicDebug' in greeter_client.obj
1>libcpmt.lib(locale.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in greeter_client.obj
1>libcpmt.lib(locale.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MT_StaticRelease' doesn't match value 'MDd_DynamicDebug' in greeter_client.obj
1>libcpmt.lib(xlock.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in greeter_client.obj
1>libcpmt.lib(xlock.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MT_StaticRelease' doesn't match value 'MDd_DynamicDebug' in greeter_client.obj
1>libcpmt.lib(xlock.obj) : error LNK2005: "public: __thiscall std::_Lockit::_Lockit(int)" (??0_Lockit@std@@QAE@H@Z) already defined in msvcprtd.lib(MSVCP140D.dll)
1>libcpmt.lib(xlock.obj) : error LNK2005: "public: __thiscall std::_Lockit::~_Lockit(void)" (??1_Lockit@std@@QAE@XZ) already defined in msvcprtd.lib(MSVCP140D.dll)
1>libcpmt.lib(xthrow.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in greeter_client.obj
1>libcpmt.lib(xthrow.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MT_StaticRelease' doesn't match value 'MDd_DynamicDebug' in greeter_client.obj
1>libcpmt.lib(xthrow.obj) : error LNK2005: "void __cdecl std::_Xbad_alloc(void)" (?_Xbad_alloc@std@@YAXXZ) already defined in msvcprtd.lib(MSVCP140D.dll)
1>libcpmt.lib(xthrow.obj) : error LNK2005: "void __cdecl std::_Xlength_error(char const *)" (?_Xlength_error@std@@YAXPBD@Z) already defined in msvcprtd.lib(MSVCP140D.dll)
1>libcpmt.lib(xthrow.obj) : error LNK2005: "void __cdecl std::_Xout_of_range(char const *)" (?_Xout_of_range@std@@YAXPBD@Z) already defined in msvcprtd.lib(MSVCP140D.dll)
1>libcpmt.lib(wlocale.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in greeter_client.obj
1>libcpmt.lib(wlocale.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MT_StaticRelease' doesn't match value 'MDd_DynamicDebug' in greeter_client.obj
1>libcpmt.lib(xlocale.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in greeter_client.obj
1>libcpmt.lib(xlocale.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MT_StaticRelease' doesn't match value 'MDd_DynamicDebug' in greeter_client.obj
1>libcpmt.lib(xdateord.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in greeter_client.obj
1>libcpmt.lib(xdateord.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MT_StaticRelease' doesn't match value 'MDd_DynamicDebug' in greeter_client.obj
1>libcpmt.lib(xwctomb.obj) : error LNK2005: __Getcvt already defined in msvcprtd.lib(MSVCP140D.dll)
1>libcpmt.lib(winapisupp.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in greeter_client.obj
1>libcpmt.lib(winapisupp.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MT_StaticRelease' doesn't match value 'MDd_DynamicDebug' in greeter_client.obj
1>libcpmt.lib(StlCompareStringA.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in greeter_client.obj
1>libcpmt.lib(StlCompareStringA.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MT_StaticRelease' doesn't match value 'MDd_DynamicDebug' in greeter_client.obj
1>libcpmt.lib(winapinls.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in greeter_client.obj
1>libcpmt.lib(winapinls.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MT_StaticRelease' doesn't match value 'MDd_DynamicDebug' in greeter_client.obj
1>libcpmt.lib(StlCompareStringW.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in greeter_client.obj
1>libcpmt.lib(StlCompareStringW.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MT_StaticRelease' doesn't match value 'MDd_DynamicDebug' in greeter_client.obj
1>libcpmt.lib(StlLCMapStringW.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in greeter_client.obj
1>libcpmt.lib(StlLCMapStringW.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MT_StaticRelease' doesn't match value 'MDd_DynamicDebug' in greeter_client.obj
1>libcpmt.lib(StlLCMapStringA.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in greeter_client.obj
1>libcpmt.lib(StlLCMapStringA.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MT_StaticRelease' doesn't match value 'MDd_DynamicDebug' in greeter_client.obj
1>LINK : warning LNK4098: defaultlib 'LIBCMT' conflicts with use of other libs; use /NODEFAULTLIB:library
1>LINK : warning LNK4098: defaultlib 'LIBCMTD' conflicts with use of other libs; use /NODEFAULTLIB:library
1>libeay32.lib(c_zlib.obj) : warning LNK4217: locally defined symbol _deflate imported in function _BIO_f_zlib
1>libeay32.lib(c_zlib.obj) : warning LNK4217: locally defined symbol _deflateEnd imported in function _BIO_f_zlib
1>libeay32.lib(c_zlib.obj) : warning LNK4217: locally defined symbol _inflate imported in function _BIO_f_zlib
1>libeay32.lib(c_zlib.obj) : warning LNK4217: locally defined symbol _inflateEnd imported in function _BIO_f_zlib
1>libeay32.lib(c_zlib.obj) : warning LNK4217: locally defined symbol _deflateInit_ imported in function _BIO_f_zlib
1>libeay32.lib(c_zlib.obj) : warning LNK4217: locally defined symbol _inflateInit_ imported in function _BIO_f_zlib
1>libeay32.lib(c_zlib.obj) : warning LNK4217: locally defined symbol _zError imported in function _zlib_zfree
1>greeter_client.obj : error LNK2019: unresolved external symbol "public: __thiscall helloworld::HelloRequest::HelloRequest(void)" (??0HelloRequest@helloworld@@QAE@XZ) referenced in function "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall GreeterClient::SayHello(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?SayHello@GreeterClient@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@ABV23@@Z)
1>greeter_client.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall helloworld::HelloRequest::~HelloRequest(void)" (??1HelloRequest@helloworld@@UAE@XZ) referenced in function "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall GreeterClient::SayHello(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?SayHello@GreeterClient@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@ABV23@@Z)
1>greeter_client.obj : error LNK2019: unresolved external symbol "public: __thiscall helloworld::HelloReply::HelloReply(void)" (??0HelloReply@helloworld@@QAE@XZ) referenced in function "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall GreeterClient::SayHello(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?SayHello@GreeterClient@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@ABV23@@Z)
1>greeter_client.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall helloworld::HelloReply::~HelloReply(void)" (??1HelloReply@helloworld@@UAE@XZ) referenced in function "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall GreeterClient::SayHello(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?SayHello@GreeterClient@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@ABV23@@Z)
1>greeter_client.obj : error LNK2019: unresolved external symbol "public: virtual class grpc::Status __thiscall helloworld::Greeter::Stub::SayHello(class grpc::ClientContext *,class helloworld::HelloRequest const &,class helloworld::HelloReply *)" (?SayHello@Stub@Greeter@helloworld@@UAE?AVStatus@grpc@@PAVClientContext@5@ABVHelloRequest@3@PAVHelloReply@3@@Z) referenced in function "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall GreeterClient::SayHello(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?SayHello@GreeterClient@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@ABV23@@Z)
1>greeter_client.obj : error LNK2019: unresolved external symbol "public: static class std::unique_ptr<class helloworld::Greeter::Stub,struct std::default_delete<class helloworld::Greeter::Stub> > __cdecl helloworld::Greeter::NewStub(class std::shared_ptr<class grpc::ChannelInterface> const &,class grpc::StubOptions const &)" (?NewStub@Greeter@helloworld@@SA?AV?$unique_ptr@VStub@Greeter@helloworld@@U?$default_delete@VStub@Greeter@helloworld@@@std@@@std@@ABV?$shared_ptr@VChannelInterface@grpc@@@4@ABVStubOptions@grpc@@@Z) referenced in function "public: __thiscall GreeterClient::GreeterClient(class std::shared_ptr<class grpc::Channel>)" (??0GreeterClient@@QAE@V?$shared_ptr@VChannel@grpc@@@std@@@Z)
1>grpc++.lib(create_channel_internal.obj) : error LNK2019: unresolved external symbol "public: virtual __thiscall grpc::Channel::~Channel(void)" (??1Channel@grpc@@UAE@XZ) referenced in function "public: virtual void * __thiscall grpc::Channel::`scalar deleting destructor'(unsigned int)" (??_GChannel@grpc@@UAEPAXI@Z)
1>grpc++.lib(create_channel_internal.obj) : error LNK2019: unresolved external symbol "private: __thiscall grpc::Channel::Channel(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,struct grpc_channel *)" (??0Channel@grpc@@AAE@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PAUgrpc_channel@@@Z) referenced in function "class std::shared_ptr<class grpc::Channel> __cdecl grpc::CreateChannelInternal(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,struct grpc_channel *)" (?CreateChannelInternal@grpc@@YA?AV?$shared_ptr@VChannel@grpc@@@std@@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@3@PAUgrpc_channel@@@Z)
1>C:\Tool\C++gRPC\grpc\vsprojects\\Debug\gflagsd.lib : warning LNK4272: library machine type 'x64' conflicts with target machine type 'X86'
1>C:\Tool\C++gRPC\grpc\vsprojects\Debug\greeter_client.exe : fatal error LNK1120: 8 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

如果有人能提供解决方案,我将不胜感激。

3
错误 LNK2038:检测到“_ITERATOR_DEBUG_LEVEL”不匹配:值“0”与值“2”不匹配。我相信这意味着您正在调试应用程序中使用发布库。 - drescherjm
2
链接:警告LNK4098:默认库'LIBCMT'与其他库的使用冲突;请使用/NODEFAULTLIB:library。 这也标识了发布CRT和调试CRT不匹配。确保每个库/ dll都使用相同的配置构建。 - drescherjm
2
在VS中以Debug模式构建grpc/vsprojects/grpc.sln。由于您之前是在Release模式下执行了上述步骤,因此无法在Debug模式下执行此步骤。 - drescherjm
1
错误已经消失了,尽管出现了新的问题 - /ZI 和 /GL 不兼容。这可能是可以解决的错误。与 Debug 和 Release 混合不同,它不起作用(可能会编译,但由于不兼容的 CRT,在运行时很可能会导致崩溃)。 - drescherjm
1
这可能不是必要的,但我是一个C语言的初学者。两件事情:1.一般来说,更多的信息是好的,所以要表扬。我们想看看你采取了哪些步骤 :) 2.C语言不是C++,确保术语命名正确! - Qix - MONICA WAS MISTREATED
显示剩余2条评论
6个回答

25

在自己苦苦摸索一段时间之后,我发现 vcpkg 在 Windows 下构建 gRPC C++ 非常出色。请注意,要求使用 Window 7 或更高版本以及 VS2015 Update 3 或更高版本。此外,您可以通过使用三元组将其配置为所需的方式,例如 .\vcpkg.exe install grpc --triplet x86-windows-static。


23

在经历了多天的各种错误后,我找到了这里 https://github.com/plasticbox/grpc-windows 一个已经准备好安装的版本,它似乎能够满足我的需求。我在这里发布以便有任何类似情况的人可以使用。

更新:2020年6月

根据GitHub上项目页面的说明,该项目不再维护,因此示例可能不再有效:

2019年5月注意:项目不再处于积极开发中,我强烈推荐使用vcpkg


1
在与GRPC/Protobuf挣扎了一天之后,我终于找到了这篇文章,它拯救了我的理智。太棒了! - Optional Option
我不需要苦苦挣扎,因为我在这篇文章中找到了这些好用的脚本(感谢作者),并使用它们在 Windows 10 64位系统上构建了 protobuf3 和 grpc。谢谢! - Dess
我做了几天相同的事情,然后我看到了这篇文章...在Github上给他一个星星! - Stefano Piovesan
2
注意,提到的脚本构建的是相当旧的 gRPC 的“v1.3.x”版本,而 gRPC 团队已经改变了构建方式,所以请小心! - Thomas
1
如果您想要构建一个新版本的grpc或使用更新版本的Visual Studio构建,请查看上述github项目的一些分支。 - jonathanjg
1
@jonathanjg,如果看到大多数的这些fork都是因为用户不小心点击了fork按钮,而本来是想查看现有的fork,那我也不会太惊讶 ;) - Andreas

10
为了使用vcpkg安装gRPC,在Windows中启动控制台并执行以下命令:
  1. vcpkg install grpc:x64-windows
  2. vcpkg install protobuf protobuf:x64-windows
  3. vcpkg install protobuf[zlib] protobuf[zlib]:x64-windows
  4. vcpkg integrate install
现在我们拥有了所需的所有内容,让我们尝试使用protoc编译器从proto文件生成消息和服务接口。
如果您的控制台中没有可用的protoc,则可能需要将其添加到系统环境变量PATH中。它将存在于<vcpkg_install_path>\packages\protobuf_x64-windows\tools\protobuf文件夹中。
现在将文件复制到我们的project/proto文件夹中,并从那里运行以下命令。
  1. protoc -I=. --cpp_out=. <yourprotofile>.proto

  2. protoc -I=. --grpc_out=. --plugin=protoc-gen-grpc="<vcpkg_install_path>\packages\grpc_x64-windows\tools\grpc\grpc_cpp_plugin.exe" <your_protofile>.proto

现在让我们向我们的CMakeLists.txt文件添加内容。取以下代码并将其复制到我们的CMakeLists.txt文件中。
//  start  //

cmake_minimum_required( VERSION 3.1 )

project( grpc_example )

find_package(gRPC CONFIG REQUIRED)

find_package( Protobuf REQUIRED )


include_directories(<vcpkg_install_path>/buildtrees/protobuf/src/23fa7edd52-3ba2225d30.clean/src)

add_executable( server src/server.cpp proto/helloworld.grpc.pb.cc proto/helloworld.pb.cc)

add_executable( client src/client.cpp proto/helloworld.grpc.pb.cc proto/helloworld.pb.cc)

target_link_libraries( server PRIVATE gRPC::grpc++ gRPC::grpc++_reflection gRPC::gpr gRPC::grpc gRPC::grpc++ gRPC::grpc_cronet protobuf::libprotoc protobuf::libprotobuf protobuf::libprotobuf-lite )

target_link_libraries( client PRIVATE gRPC::grpc++ gRPC::grpc++_reflection gRPC::gpr gRPC::grpc gRPC::grpc++ gRPC::grpc_cronet protobuf::libprotoc protobuf::libprotobuf protobuf::libprotobuf-lite )

// end //

现在,请进入我们的项目/构建文件夹,并发出以下命令,使用CMake生成一个项目。我将在此处生成Visual Studio 2015解决方案,但选择权在于您。

  1. cmake -G "Visual Studio 14 2015 Win64" ../ -DCMAKE_TOOLCHAIN_FILE=<vcpkg_install_path>/scripts/buildsystems/vcpkg.cmake
  2. cmake build .
  3. cmake --build . --config Release

现在转到Release文件夹,并运行server.exe和client.exe。


2
你好,cmake部分是必需的吗?我以为vcpkg已经提供了一切可以在Visual Studio中使用的东西?我试图跳过cmake部分,但现在我卡在链接器错误上了:https://stackoverflow.com/questions/68768805/lnk2001-and-lnk2019-for-grpc-installed-via-vcpkg?noredirect=1&lq=1 - OneWorld
2
回答之前的评论:不,这并非必要。可以省略您回答中的cmake部分,在VisualStudio中使用基于MSBuild的项目(即常规控制台cpp项目)。但是,基于MSBuild的项目存在链接器错误,需要通过以下方式进行修复:https://stackoverflow.com/questions/68768805/still-linker-errors-in-visualstudio-for-grpc-despite-autolink-vcpkg-enabled/68772745#68772745 - OneWorld

3

以下是我在Windows上安装和使用grpc的解决方案...
GRPC C++... 安装... Microsoft Visual Studio Community 2017 版本15.6.4 Git CMake Chocolatey 以管理员身份打开git Active State Perl choco install activeperl Go choco install golang yasm choco install yasm 将C:\ProgramData\chocolatey\lib\yasm\tools添加到PATH Ninja choco install ninja 克隆包括子模块在内的grpc源码... git clone --recursive -b v1.10.x https://github.com/grpc/grpc cd grpc cmake:使用Ninja(支持boringssl的汇编优化)... 关闭git 打开管理员:命令提示符(shift+RC cmd并以管理员身份运行) cd C:\Users\aaron\repo\grpc md .build && cd .build call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat" x64 cmake .. -GNinja -DCMAKE_BUILD_TYPE=Release cmake --build . ninja install
生成代码... 打开Visual Studio并创建新项目 关闭预编译头文件 RC项目 -> 属性 -> C/C++ -> 预编译头文件 设置预编译头文件="不使用预编译头文件" 删除stdafx.h/.cpp 添加.proto文件 RC资源文件 -> 添加新文件 -> 实用程序 -> Text.txt 使用.proto扩展名命名文件 编写.proto文件内容 将.proto文件复制到"C:\Program Files (x86)\grpc\bin" 导航到"C:\Users\aaron\repo\grpc\.build" 将grpc_cpp_plugin.exe复制到"C:\Program Files (x86)\grpc\bin" 打开管理员:命令提示符 cd C:\Program Files (x86)\grpc\bin protoc -I . --grpc_out=. --plugin=protoc-gen-grpc=grpc_cpp_plugin.exe cid_service.proto 生成cid_service.grpc.pb.cc/.h protoc -I . --cpp_out=. cid_service.proto 生成cid_service.pb.cc/.h 将文件复制到VS项目并将它们添加到Header Files/Source Files。

1
这对我很有帮助,谢谢你制作这个。我通过解压缩zip文件,在文件夹中使用cd命令来使用它。
git clone -b v1.2.0 https://github.com/grpc/grpc

git submodule update --init

将pthreads安装到Visual Studios VC\include(.h)、lib(.lib)和bin(.dll)中

运行nuget restore(对于每个.sln文件获取依赖项)

nuget restore grpc\vsprojects\grpc.sln

nuget restore grpc\vsprojects\grpc_protoc_plugins.sln

nuget restore grpc\third_party\protobuf\cmake\build\solution\protobuf.sln

修复 grpc\src\cpp\server\ server_builder.cc。
int num_frequently_polled_cqs = sync_server_cqs->size();

将第273行的int改为size_t,num...(如上所述)
运行grpc_build.bat,然后进入grpc_helloworld文件夹并运行test_protoc.bat,打开grpc_helloworld.sln,运行构建应该会自动执行nuget。

0

我按照 BUILDING.md 中的说明进行操作。您也可以在此处查看: https://github.com/grpc/grpc/blob/master/BUILDING.md

  • 我使用 git clone -b v1.55.0 https://github.com/grpc/grpc 克隆了 grpc 存储库
  • 命令 git submodule update --init 失败了,我不得不手动删除 /third_party/opencensus-proto 目录
  • 我需要运行 git rm --cached third_party/opencensus-proto 这一步骤可能在运行 git submodule 之前删除目录时不需要,然后我就能获取缺失的子模块了
  • 我使用 cmake .. -G "Visual Studio 17 2022" 在 Visual Studio 中进行操作

然后我就能按照 Windows 的进一步说明进行操作了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接