gRPC C++ on Windows

After struggling with various errors for many days, I found ready-to-install version here https://github.com/plasticbox/grpc-windows which seems to work satisfactory for my purpose. I post it here in case anyone is in similar situation.

Update: June 2020

According to the project page on GitHub the project is no longer maintained and therefore the example might not work any longer:

May 2019 NOTE: Project is no longer under active development, I definitely recommend using vcpkg


In order to install gRPC using vcpkg, launch a console in Windows and execute the following command,

  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

Now we have everything we need, so let's try to generate the message and service interfaces from a proto file using the protoc compiler

If protoc is not available from your console, you might have to add it to your system env PATH. And it will be present in your <vcpkg_install_path>\packages\protobuf_x64-windows\tools\protobuf folder

Now copy the file to our project/proto folder and run the following commands from there

  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

Now let's add content to our CMakeLists.txt file. Take the following code and copy it to our CMakeLists.txt file.

//  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 //

Now go to our project/build folder and issue the following command to generate a project using CMake. I'll be generating a Visual Studio 2015 solution here, but the choice is up to you.

  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

Now go to the Release folder, and run the server.exe and client.exe


Here is the solution I used to install and use grpc on windows...

GRPC C++...
install...
Microsoft Visual Studio Community 2017 Version 15.6.4
Git
CMake
Chocolatey
Open git as admin
Active State Perl
choco install activeperl
Go
choco install golang
yasm
choco install yasm
add C:\ProgramData\chocolatey\lib\yasm\tools to PATH
Ninja
choco install ninja
clone grpc sources including submodules...
git clone --recursive -b v1.10.x https://github.com/grpc/grpc
cd grpc
cmake: Using Ninja (supports boringssl's assembly optimizations)...
close git
open Administrator: Command Prompt (shift+RC cmd and run as admin)
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

generating code...
open visual studio and create new project
turn off precompiled headers
RC project -> Properties -> C/C++ -> Precompiled Headers
Set Precompiled Header = "Not Using Precompiled Headers"
Delete stdafx.h/.cpp
add .proto file
RC Resource Files -> Add new -> Utility -> Text.txt
name file with .proto extension
write .proto file contents
copy .proto to "C:\Program Files (x86)\grpc\bin"
navigate to "C:\Users\aaron\repo\grpc\.build"
copy grpc_cpp_plugin.exe to "C:\Program Files (x86)\grpc\bin"
open Administrator: Command Prompt
cd C:\Program Files (x86)\grpc\bin
protoc -I . --grpc_out=. --plugin=protoc-gen-grpc=grpc_cpp_plugin.exe cid_service.proto
Generates cid_service.grpc.pb.cc/.h
protoc -I . --cpp_out=. cid_service.proto
Generates cid_service.pb.cc/.h
copy files to VS project and add them to Header Files/Source Files


After struggling with this for some time myself, I found that vcpkg does a very good job building gRPC C++ for Windows. Note the requirements are Window 7 or later and VS2015 Update 3 or later. Note that you can configure it the way you want it by using a triplet, e.g. .\vcpkg.exe install grpc --triplet x86-windows-static