gRPC

Posted by Bryan on March 5, 2020

Install gPRC

Two methods are recommanded by google. The first one is to build projects from main directory and other module will be built and installed automatically (this one is called module mode) and the second is to build submodule (grpc dependent lib) respectively and build grpc then (this one is call package mode). Here, I recommand the latter. And installation turorial is mainly refered to stackoverflow and its github.

  1. Install dependent libs
    # Install c-ares
    cd third_party/cares/cares
    git fetch origin
    git checkout cares-1_13_0
    # I used the e982924 commit id version
    mkdir -p cmake/build
    cd cmake/build
    cmake -DCMAKE_BUILD_TYPE=Release ../..
    make -j4 install
    cd ../../../../..
    rm -rf third_party/cares/cares  # wipe out to prevent influencing the grpc build
    # Install zlib
    cd third_party/zlib
    mkdir -p cmake/build
    cd cmake/build
    cmake -DCMAKE_BUILD_TYPE=Release ../..
    make -j4 install
    cd ../../../..
    rm -rf third_party/zlib  # wipe out to prevent influencing the grpc build
    # Install protobuf
    cd third_party/protobuf
    mkdir -p cmake/build
    cd cmake/build
    cmake -Dprotobuf_BUILD_TESTS=OFF -DCMAKE_BUILD_TYPE=Release ..
    make -j4 install
    cd ../../../..
    rm -rf third_party/protobuf  # wipe out to prevent influencing the grpc build
    
  2. Install grpc lib
    # Install gRPC
    mkdir -p cmake/build
    cd cmake/build
    cmake -DgRPC_INSTALL=ON -DgRPC_BUILD_TESTS=OFF -DgRPC_PROTOBUF_PROVIDER=package -DgRPC_ZLIB_PROVIDER=package -DgRPC_CARES_PROVIDER=package -DgRPC_SSL_PROVIDER=package -DCMAKE_BUILD_TYPE=Release ../..
    make -j4 install
    

    But cmake command shows me a warning, like that(I met the warning about lib:ABSL):

    CMake Warning at CMakeLists.txt:143 (message):
    gRPC_INSTALL will be forced to FALSE because gRPC_ABSL_PROVIDER is
    "module"
    

    The problem can be solve via stackoverflow. In accordance to the words:
    ————————————————————————————————
    Because of some limitations of our current CMakeLists.txt, the install targets (see gRPC_INSTALL option) will only be generated if you are building using a pre-installed version of our dependencies (gRPC_CARES_PROVIDER in your case needs to be set to package). The warning you saw “gRPC_INSTALL will be forced to FALSE because gRPC_CARES_PROVIDER” is “module” basically tells you that even though gRPC_INSTALL was set to ON by you, we’re setting it back to OFF because your gRPC_CARES_PROVIDER is set to use c-ares from git submodule (which wouldn’t work well with the current CMakeLists.txt) - so you shouldn’t expect anything to be installed (not even grpc_cpp_plugin.
    ————————————————————————————————
    We should replace it with the following command (adding -DgRPC_ABSL_PROVIDER=package):

    cmake -DgRPC_ABSL_PROVIDER=package -DgRPC_INSTALL=ON -DgRPC_BUILD_TESTS=OFF -DgRPC_PROTOBUF_PROVIDER=package -DgRPC_ZLIB_PROVIDER=package -DgRPC_CARES_PROVIDER=package -DgRPC_SSL_PROVIDER=package -DCMAKE_BUILD_TYPE=Release ../..
    

    Maybe we don’t install ABSL lib on linux at the time, then go to grpc/third_party/abseil-cpp and build the lib:

    cd grpc/third_party/abseil-cpp
    mkdir build
    cd build
    # add definition, if not, static lib will be build
    # static libs cannot be linked to dynamic libs
    cmake -DBUILD_SHARED_LIBS=ON ..
    make install
    

    Here I just adopt ABSL as an example. You can find it in third_party dir if lacking some libs. Don’t forget to use git submodule update --init to download codes.

  3. Build helloworld example with CMakeLists.txt
    cd grpc/examples/cpp/helloworld
    mkdir build
    cd build
    cmake ..
    make -j4
    

Protocol Buffer

  1. usage (cmake version 3.18 in windows)
    The package protobuf:x64-windows provides CMake targets:
     find_package(protobuf CONFIG REQUIRED)
     target_link_libraries(main PRIVATE protobuf::libprotoc protobuf::libprotobuf protobuf::libprotobuf-lite)
    

    The package protobuf:x86-windows provides CMake targets:

     find_package(protobuf CONFIG REQUIRED)
     target_link_libraries(main PRIVATE protobuf::libprotoc protobuf::libprotobuf protobuf::libprotobuf-lite)
    
  2. visual studio uses packages install by vspkg
    PS D:\vcpkg> .\vcpkg.exe integrate install
    Applied user-wide integration for this vcpkg root.
    All MSBuild C++ projects can now #include any installed libraries.
    Linking will be handled automatically.
    Installing new libraries will make them instantly available.
    CMake projects should use: “-DCMAKE_TOOLCHAIN_FILE=D:/vcpkg/scripts/buildsystems/vcpkg.cmake”