简体   繁体   English

使用MSVC 2015编译GRPC 0.15与Windows上的Qt 5.6一起使用

[英]Compiling GRPC 0.15 with MSVC 2015 for usage with Qt 5.6 on Windows

The process is extremely non obvious and error prone so I think a place where a proper procedure is described is required. 该过程非常不明显且容易出错,因此我认为需要一个描述适当过程的地方。

I will answer my own question below, but it is more or less a hack and slash solution as Visual Studio is mostly Terra Incognita for me and I just stumbled on a combination that works after a lot of trial and error, sometimes not even understanding what I am doing. 我将在下面回答我自己的问题,但它或多或少是一个破解和斜线解决方案,因为Visual Studio对我而言主要是Terra Incognita,而我只是偶然发现了一种经过反复试验和验证的组合,有时甚至无法理解我在做。 If it looks horrible - sorry about that. 如果看起来很恐怖-对此感到抱歉。 There was simply no proper guide for me to use. 根本没有适当的指导供我使用。

Steps: 脚步:

Download GRPC and Protobuf3 from their respective github repositories. 从各自的github存储库下载GRPC和Protobuf3。 It is better to use GRPC from repository than from zip package because this way you will be able to populate "third_party" folder via git. 最好从存储库中使用GRPC而不是从zip包中使用GRPC,因为这样您就可以通过git填充“ third_party”文件夹。

Download CMake/CMake-gui and feed protobuf\\cmake folder to it. 下载CMake / CMake-gui并向其提供protobuf \\ cmake文件夹。 This will generate your visual stuido projects 这将生成您的视觉stuido项目

Compile All_Build.vcxproj as is in release mode. 在发布模式下按原样编译All_Build.vcxproj。 This will generate protoc.exe file which you must store somewhere. 这将生成protoc.exe文件,您必须将其存储在某处。

Here comes the trickery: 骗术来了:

By default, Release protobuf/grpc projects are compiled as /MT. 默认情况下,Release protobuf / grpc项目被编译为/ MT。 Chances are - qtcreator is compiling your projects as /MD. 可能-qtcreator将您的项目编译为/ MD。 If you try to use the library generated by MSVC as is - you will get a library format conflict. 如果您尝试按原样使用MSVC生成的库,则会遇到库格式冲突。

So first step is to edit libprotobuf subproject: 因此,第一步是编辑libprotobuf子项目:

C/C++->Code Generation->Runtime Library to Multi-threaded DLL (/MD) C / C ++->代码生成->运行时库到多线程DLL(/ MD)

Next problem: default project will generate a .lib file, that even with /MD I have been unable to succesfully attach. 下一个问题:默认项目将生成一个.lib文件,即使使用/ MD,我也无法成功附加该文件。

If anyone knows how to avoid a lot of "undefined reference" errors while trying to use it, feel free to comment below. 如果有人知道在尝试使用它时如何避免很多“未定义的引用”错误,请在下面随意评论。

// these are only necessary if you can't link statically as I do
Configuration Properties->General->Target extension : .dll
Configuration Properties->General->Configuration Type : Dynamic Library
We also have to add a bunch of defines:
Configuration Properties->C/C++->Preprocessor : add PROTOBUF_USE_DLLS;LIBPROTOBUF_EXPORTS

Now you can compile protobuf. 现在您可以编译protobuf了。 Do this and store libprotobuf.lib and libprotobuf.dll somewhere 这样做并将libprotobuf.lib和libprotobuf.dll存储在某个地方

Onwards to grpc. 继续到grpc。

Open grpc.sln 打开grpc.sln

As I am not using MSVC for my work routinely these later steps were non-obvious. 由于我通常不会在工作中使用MSVC,因此这些后续步骤并不明显。

You will need Nuget installed in Visual Studio before anything else. 您需要先在Visual Studio中安装Nuget,然后再进行其他操作。 Nuget will, on compilation, pull a bunch of dependencies into your grpc\\vsprojects\\packages folder.You will need .dll's from there later if you plan to deploy your app. Nuget会在编译时将一堆依赖项放入grpc \\ vsprojects \\ packages文件夹中。如果以后打算部署应用程序,则从那里需要.dll。

Once it's installed go and switch /MT /MTd to /MD /MDd depending on the mode you plan to use, the same as for protobuf. 安装完成后,根据计划使用的模式,将/ MT / MTd切换到/ MD / MDd,与protobuf相同。

/MD corresponds to Release build (for me) while /MDd is for Debug / MD对应于Release build(对我而言),而/ MDd对应于Debug。

Also, you need to remove "z" and "borinssl" subprojects as they are broken on windows and won't let you compile properly. 另外,您还需要删除“ z”和“ borinssl”子项目,因为它们在Windows上已损坏并且无法正确编译。

Compile the project. 编译项目。 It will produce grpc.lib grpc_unsecure.lib grpc++.lib grpc++_unsecure.lib which you will need to store somewhere accessible to qtcreator while it builds your project (same with libprotobuf.lib and libprotobuf.dll) 它将生成grpc.lib grpc_unsecure.lib grpc ++。lib grpc ++ _ unsecure.lib,您将在构建项目时将其存储在qtcreator可以访问的位置(与libprotobuf.lib和libprotobuf.dll相同)

Open grpc_protoc_plugins.sln 打开grpc_protoc_plugins.sln

Add a folder that has libprotobuf.lib to Library Directories in project configuration. 在项目配置中,将具有libprotobuf.lib的文件夹添加到库目录中。

Compile grpc_cpp_plugin. 编译grpc_cpp_plugin。 It will produce grpc_cpp_plugin.exe which you need to store with protoc.exe 它将生成grpc_cpp_plugin.exe,您需要将其存储在protoc.exe中

If you plan to deploy your app you will need to copy a bunch of dependencies nuget collected for you from packages folders: libeay32.dll ssleay32.dll zlib.dll 如果您打算部署应用程序,则需要复制从程序包文件夹中为您收集的一堆依赖项nuget:libeay32.dll ssleay32.dll zlib.dll

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM