'Unable to install and use gRPC C/C++ on windows 10 with VS2017

I know the question I am asking here seems a bit silly but it is still an open ended question and will definitely help a lot of folks.

Problem :- What could be the way to install and use gRPC C/C++ in Visual Studio 2017 and windows 10 64 bit.

The documentation says to use CMAKE but providing very less information on how to actually build gRPC.

If anyone could please guide me step by step how to install gRPC would be very helpful to me and others who are new to gRPC and trying to build it.

Thanks a lot in advance!



Solution 1:[1]

I recently had issues with this myself and would like to present my solution, although the question is a little old.

There is a working vcpkg package available, but its version is outdated. Therefore I build and installed gRPC on Windows from source using Visual Studio 2017. The official documentation gives some hints, but is pretty vague in the details. I will list all steps below, some are copied from said documentation:

Prerequisites

  • Visual Studio (2015 or above should do)
  • Git
  • CMake (make sure to add CMake to the system PATH during installation)

Get gRPC

  1. Clone gRPC repository:

    git clone -b RELEASE_TAG_HERE https://github.com/grpc/grpc
    

    Replace RELEASE_TAG_HERE with the release tag of your choice, e.g. v1.42.0 is the latest right now.

  2. Load its dependencies:

    cd grpc
    git submodule update --init
    

Build gRPC from source

  1. Create a build directory and go to it:

    mkdir .build
    cd .build
    
  2. Generate Visual Studio project structure with CMake:

    for Visual Studio 2017:

    cmake .. -G "Visual Studio 15 2017"
    

    or for Visual Studio 2019:

    cmake .. -G "Visual Studio 16 2019"
    

    or for Visual Studio 2022:

    cmake .. -G "Visual Studio 17 2022"
    

    Specify a specific target platform with the -A option, e.g. Win32, x64,ARM or ARM64.

  3. Open the newly created solution grpc.sln (can be found in the .build directory) in Visual Studio.

  4. Select the desired "Solution Configuration" (Debug, Release, ...) in Visual Studio and build the solution.

Install gRPC

This was the hard part to figure out, although it is fairly easy.

  1. Start Visual Studio with administrator privileges and load grpc.sln.

  2. In the "Solution Explorer", search for the project INSTALL.

  3. Build the INSTALL project.

  4. gRPC is now installed to:

    C:\Program Files (x86)\grpc
    
  5. Add C:\Program Files (x86)\grpc\bin to your systems PATH.

Use gRPC in your project

  1. Add gRPC header files:

    Right click on project -> Properties -> Configuration Properties -> C/C++ -> General -> Additional Include Directories

    Edit this property and add the gRPC include directory C:\Program Files (x86)\grpc\include.

  2. Add gRPC library directory: Right click on project -> Properties -> Configuration Properties -> Linker -> General -> Additional Library Directories

    Edit this property and add the gRPC library directory C:\Program Files (x86)\grpc\lib\.

  3. Add gRPC libraries:

    Right click on project -> Properties -> Configuration Properties -> Linker -> Input -> Additional Dependencies

    Edit this property and add all libraries from the gRPC library directory (C:\Program Files (x86)\grpc\lib\).

    There will be errors when building the project! Apparently, some libraries are present in different versions. You have to choose one of the versions for each library. As of gRPC v1.42.0 (Release Build), these libraries are:

    • grpc.lib vs. grpc_unsecure.lib
    • grpc++.lib vs. grpc++_unsecure.lib
    • libprotobuf.lib vs. libprotobuf-lite.lib
    • libprotoc.lib seems to be included in libprotobuf.lib
    • zlib.lib vs. zlibstatic.lib

    Another option is link the libraries with preprocessor directives #pragma comment(lib, LIBRARY_HERE) into your project. Thus, the unused version can be commented out.

  4. Now, your project should build without errors. Make sure you use the same "Solution Configuration" (Debug, Release) as you used for the gRPC build.

I hope this rather detailed guide helps you guys!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1