CMake install grpc to generate gRPCTargets.cmake file

Posted by Vertical3 on Wed, 04 Dec 2019 09:41:09 +0100

The following are the installation statements:

cd grpc_folder
git submodule update --init
cmake ..
make -j 4
sudo make install

However, when writing a program that depends on gRPC, it is found that the CMake file of gRPC cannot be called. The error prompt is as follows:

  include could not find load file:

    /usr/local/lib/cmake/grpc/gRPCTargets.cmake

In the issue list of gRPC, a solution is found (when CMake is installed, if the third party relies on it, it will be installed through the source code by default. If it cannot be installed through the source code, the gRPCTargets.cmake file cannot be generated):
Therefore, you need to specify whether third-party dependencies are installed through source code or package manager
Because zlib, protobuf and cares have been installed, modify cmake.. to:

cmake  -DgRPC_INSTALL=ON -DgRPC_ZLIB_PROVIDER=package -DgRPC_CARES_PROVIDER=package -DgRPC_PROTOBUF_PROVIDER=package -DgRPC_SSL_PROVIDER=package ..

Projects that rely on gRPC can use the following statements to add gRPC dependencies:

if (WITH_GRPC)
    find_package(gRPC CONFIG)
    # First attempt to set up gRPC via cmake; but if cmake config files aren't
    # available, fallback to pkg-config.
    if (gRPC_FOUND)
        set(GRPC_CPP_PLUGIN $<TARGET_FILE:gRPC::grpc_cpp_plugin>)
        list(APPEND LIGHTSTEP_LINK_LIBRARIES gRPC::grpc++)
        include_directories(SYSTEM
                $<TARGET_PROPERTY:gRPC::grpc++,INTERFACE_INCLUDE_DIRECTORIES>)
    else()
        message("Falling back to finding gRPC with pkg-config")
        find_program(GRPC_CPP_PLUGIN grpc_cpp_plugin)
        if (NOT GRPC_CPP_PLUGIN)
            message(FATAL_ERROR "grpc_cpp_plugin not found!")
        endif()
        find_package(PkgConfig REQUIRED)
        pkg_search_module(GRPC REQUIRED grpc)
        pkg_search_module(GRPCPP REQUIRED grpc++)
        list(APPEND LIGHTSTEP_LINK_LIBRARIES ${GRPCPP_LDFLAGS} ${GRPC_LDFLAGS})
        include_directories(SYSTEM ${GRPC_INCLUDE_DIRS} ${GRPCPP_INCLUDE_DIRS})
    endif()
endif()

PS:
If you think my article is helpful to you, you can scan the code to get the red bag, thank you!

Topics: C++ cmake git sudo zlib