DachuanZhao
DachuanZhao

Reputation: 1339

How to generate c++ grpc file after installing it by vcpkg and cmake?

I install grpc by vcpkg install grpc and add it to cmake file . Now I want to generate grpc c++ file , I run :

protoc --cpp_out=grpc_file/ --grpc_out=grpc_file/  --proto_path=grpc_proto/  ./grpc_proto/side_information.proto

It returns error :

protoc-gen-grpc: program not found or is not executable
--grpc_out: protoc-gen-grpc: Plugin failed with status code 1.

What should I do to use the grpc_cpp_plugin installed by vcpkg ? My workspace is :

.
├── CMakeLists.txt
├── Readme.md
├── build
├── build_envs.sh
├── grpc_file
├── grpc_proto
├── main.cpp
├── twitter.json
└── vcpkg

Upvotes: 2

Views: 2533

Answers (2)

Nexus Software Systems
Nexus Software Systems

Reputation: 353

Is your CMakeLists.txt like below?

cmake_minimum_required(VERSION 3.1)

if(DEFINED ENV{VCPKG_ROOT})
    set(CMAKE_TOOLCHAIN_FILE $ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake)
else()
    set(CMAKE_TOOLCHAIN_FILE "/path_to_vcpkg/scripts/buildsystems/vcpkg.cmake")
endif()

project(Foo)

find_package(gRPC CONFIG REQUIRED)

...

add_executable(${PROJECT_NAME} ${Bar})

target_link_libraries(${PROJECT_NAME} PRIVATE gRPC::gpr gRPC::grpc gRPC::grpc++ gRPC::grpc_cronet)

In the Folder path_to_vcpkg/packages/grpc_x64-PLATFORM/tools/grpc you will find all the precompiled grpc-plugins for your platform (also grpc_cpp_plugin).

Upvotes: 1

Rane
Rane

Reputation: 371

The protoc is trying to look for the protoc-gen-grpc plugin from your PATH, but it is unable to find one, thus the error. The fix is to simply specify the path to the said plugin. Given that you are using vcpkg, that path is probably something along these lines:

protoc --cpp_out=grpc_file/ --grpc_out=grpc_file/ --proto_path=grpc_proto/ \
--plugin=protoc-gen-grpc="<vcpkg_install_path>\packages\grpc_x64-windows\tools\grpc\grpc_cpp_plugin.exe" \ 
./grpc_proto/side_information.proto 

If you are looking to be using Visual Studio for your grpc project, I recommend checking out this guide for generating a VS solution among other things.

Upvotes: 4

Related Questions