I use gRPC with ros melodic in ubuntu 18.04. Since protobuf 3.0.0 was install if I got ros-melodic-desktop-full installed, I installed grpc in a custom path.
My CMakeLists.txt check if gRPC founded, if not it will install it in project scope using FetchContent.
When it get gRPC throught FetchContent, every thing goes fine. But when I export these two veriables
export gRPC_DIR=~/.grpc/
export Protobuf_DIR=~/.grpc/
in my .bashrc to let CMake find my gRPC and Protobuf installed locally. It will run into error when CMake execute the custom command compile the proto files.
Error Log:
grpc::grpc_cpp_plugin: program not found or is not executable
Please specify a program using absolute path or make sure the program is available in your PATH system variable
--grpc_out: protoc-gen-grpc: Plugin failed with status code 1.
And if I replace grpc::grpc_cpp_plugin with the absolute path of grpc_cpp_plugin, which is $HOME/.grpc/bin/grpc_cpp_plugin, it works fine.
Here is my install script:
#!/bin/bash
git clone --recurse-submodules -b v1.28.0 --depth 1 https://github.com/grpc/grpc
cd grpc
mkdir -p cmake/build
cd cmake/build
mkdir ~/.grpc
cmake -DCMAKE_BUILD_TYPE=Release \
-DgRPC_INSTALL=ON \
-DgRPC_PROTOBUF_PACKAGE_TYPE=CONFIG \
-DgRPC_BUILD_TESTS=OFF \
-DCMAKE_CXX_FLAGS=-latomic \
-DCMAKE_INSTALL_PREFIX=~/.grpc ../..
make -j$(nproc) && make install && make clean
Here is part of my CMakeLists.txt:
...
find_package(Protobuf CONFIG)
find_package(gRPC CONFIG)
if(NOT ${gRPC_FOUND})
include(cmake_module/FetchGRPC.cmake)
else()
set(_PROTOBUF_PROTOC protobuf::protoc)
set(_GRPC_CPP_PLUGIN_EXECUTABLE grpc::grpc_cpp_plugin)
endif()
add_custom_command(
OUTPUT "${ProtoSrc}" "${GrpcSrc}" "${ProtoHds}" "${GrpcHds}"
COMMAND ${_PROTOBUF_PROTOC}
ARGS --grpc_out "${ProtoPath}"
--cpp_out "${ProtoPath}"
-I "${ProtoPath}"
--plugin=protoc-gen-grpc="${_GRPC_CPP_PLUGIN_EXECUTABLE}"
"${ProtoFile}"
DEPENDS "${ProtoFile}"
)
...
the FetchGRPC.cmake mention above:
include(FetchContent)
FetchContent_Declare(
gRPC
GIT_REPOSITORY https://github.com/grpc/grpc
GIT_TAG v1.28.0
GIT_PROGRESS TRUE
USES_TERMINAL_DOWNLOAD TRUE
)
set(FETCHCONTENT_QUIET OFF)
FetchContent_MakeAvailable(gRPC)
if (CMAKE_CROSSCOMPILING)
find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin)
else ()
set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:grpc_cpp_plugin>)
endif ()
set(_REFLECTION grpc++_reflection)
set(_GRPC_GRPCPP_UNSECURE grpc++_unsecure)
set(_PROTOBUF_LIBPROTOBUF libprotobuf)
set(_PROTOBUF_PROTOC $<TARGET_FILE:protoc>)
set(_GRPC_GRPCPP grpc++)