Zeks
Zeks

Reputation: 2375

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. If it looks horrible - sorry about that. There was simply no proper guide for me to use.

Upvotes: 1

Views: 1300

Answers (1)

Zeks
Zeks

Reputation: 2375

Steps:

Download GRPC and Protobuf3 from their respective github repositories. 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.

Download CMake/CMake-gui and feed protobuf\cmake folder to it. This will generate your visual stuido projects

Compile All_Build.vcxproj as is in release mode. This will generate protoc.exe file which you must store somewhere.

Here comes the trickery:

By default, Release protobuf/grpc projects are compiled as /MT. Chances are - qtcreator is compiling your projects as /MD. If you try to use the library generated by MSVC as is - you will get a library format conflict.

So first step is to edit libprotobuf subproject:

C/C++->Code Generation->Runtime Library to Multi-threaded DLL (/MD)

Next problem: default project will generate a .lib file, that even with /MD I have been unable to succesfully attach.

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. Do this and store libprotobuf.lib and libprotobuf.dll somewhere

Onwards to grpc.

Open grpc.sln

As I am not using MSVC for my work routinely these later steps were non-obvious.

You will need Nuget installed in Visual Studio before anything else. 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.

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.

/MD corresponds to Release build (for me) while /MDd is for Debug

Also, you need to remove "z" and "borinssl" subprojects as they are broken on windows and won't let you compile properly.

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)

Open grpc_protoc_plugins.sln

Add a folder that has libprotobuf.lib to Library Directories in project configuration.

Compile grpc_cpp_plugin. It will produce grpc_cpp_plugin.exe which you need to store with 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

Upvotes: 2

Related Questions