简体   繁体   English

为 cpp 项目设置 protobuf 和 grpc 的正确方法是什么?

[英]What is a correct way to setup protobuf and grpc for cpp project?

I have a cpp project, and I want to build everything from source, to get latest things likned in. So under my project root I've created a 3rd_party folder and assemble following script:我有一个 cpp 项目,我想从源代码构建所有内容,以获取最新的东西。所以在我的项目根目录下,我创建了一个3rd_party文件夹并组装以下脚本:

sudo apt-get install autoconf automake libtool curl make g++ unzip -y
git clone https://github.com/google/protobuf.git
cd protobuf
git submodule update --init --recursive
./autogen.sh
./configure
make
make check
sudo make install
sudo ldconfig
cd ..

echo installing grpc
git clone --recurse-submodules -b v1.43.0 https://github.com/grpc/grpc
export MY_INSTALL_DIR=$HOME/.local
be sure that its exists
cd grpc
mkdir -p cmake/build
pushd cmake/build
cmake -DgRPC_INSTALL=ON       -DgRPC_BUILD_TESTS=OFF       -DCMAKE_INSTALL_PREFIX=$MY_INSTALL_DIR       ../..
make -j
make install
popd

i've encountered following flaws:我遇到了以下缺陷:

  • grpc v1.43.0 clones 3rd party submodule protoc v3.18, which after build has problems - protoc binary says "cpp plugin not found", when trying to generate grpc v1.43.0 克隆了 3rd 方子模块 protoc v3.18,在构建后出现问题 - protoc 二进制文件在尝试生成时显示“找不到 cpp 插件”

To overcome that I've copied sources obtained in the first part of script, to 3rd party subfolder of second part, to gurantee it to be 3.19 - then after compilation protoc working great, plugins are in place, and grpc is linked against latest version.为了克服这个问题,我将脚本第一部分中获得的源代码复制到第二部分的第 3 方子文件夹中,以确保它是 3.19 - 然后在编译协议工作正常之后,插件就位,并且 grpc 与最新版本相关联.

Very weird issue, needs understanding why it clones outdated version, and why 3.18 has no plugins under same build parameters as 3.19非常奇怪的问题,需要了解为什么它会克隆过时的版本,以及为什么 3.18 在与 3.19 相同的构建参数下没有插件

  • in cmakelists find_package(Protobuf REQUIRED) fails at all在 cmakelists find_package(Protobuf REQUIRED)中完全失败

  • find_package( gRPC REQUIRED ) is not operating properly: find_package( gRPC REQUIRED ) 运行不正常:

Found package configuration file:
[cmake] 
[cmake]     /home/a/.local/lib/cmake/grpc/gRPCConfig.cmake
[cmake] 
[cmake]   but it set gRPC_FOUND to FALSE so package "gRPC" is considered to be NOT
[cmake]   FOUND.  Reason given by package:
[cmake] 
[cmake]   The following imported targets are referenced, but are missing:
[cmake]   protobuf::libprotobuf protobuf::libprotoc

Question问题

How can I write script, that in an empty folder will get me everything, related to grpc 3rd party libs and tools, that I need for project?我怎样才能编写脚本,在一个空文件夹中将得到我项目所需的与 grpc 3rd 方库和工具相关的所有内容?

Here are the steps I took:以下是我采取的步骤:

$ mkdir .local
$ export INSTALL_PREFIX="$PWD/.local"
$ export CMAKE_GENERATOR=Ninja  # optional, but recommended

$ git clone --recurse-submodules -b v3.19.3 --depth 1 https://github.com/google/protobuf.git
$ pushd protobuf
$ ./autogen.sh
$ ./configure --prefix=$INSTALL_PREFIX
$ make -j$(nproc)
$ make install
$ popd

$ git clone --recurse-submodules -b v1.43.0 --depth 1 https://github.com/grpc/grpc.git
$ cmake -S grpc -B .build/grpc \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=$INSTALL_PREFIX \
    -DCMAKE_PREFIX_PATH=$INSTALL_PREFIX \
    -DgRPC_INSTALL=ON \
    -DgRPC_BUILD_TESTS=OFF \
    -DgRPC_PROTOBUF_PROVIDER=package \
    -DABSL_PROPAGATE_CXX_STD=ON
$ cmake --build .build/grpc --target install
...

$ mkdir example
$ echo "int main () { return 0; }" > example/main.cpp
$ vim example/CMakeLists.txt
$ cat example/CMakeLists.txt
cmake_minimum_required(VERSION 3.22)
project(example)

find_package(gRPC REQUIRED)

add_executable(main main.cpp)
target_link_libraries(main PRIVATE gRPC::grpc++)

$ cmake -S example -B .build/example \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_PREFIX_PATH=$INSTALL_PREFIX
$ cmake --build .build/example
...

I build protobuf separately because gRPC doesn't set up its dependencies in CMake correctly when using its internal protobuf build (I tried both ways).我单独构建 protobuf 是因为 gRPC 在使用其内部 protobuf 构建时没有正确设置其在 CMake 中的依赖关系(我尝试了两种方式)。 This involves passing --prefix to protobuf's ./configure script and passing -DgRPC_PROTOBUF_PROVIDER=package to gRPC's CMake build.这包括将--prefix传递给 protobuf 的./configure脚本并将-DgRPC_PROTOBUF_PROVIDER=package传递给 gRPC 的 CMake 构建。 The latter is a gRPC-specific variable that tells it not to build protobuf, but to search for it instead.后者是一个特定于 gRPC 的变量,告诉它不要构建 protobuf,而是搜索它。 I tell gRPC where to find protobuf by setting CMAKE_PREFIX_PATH , which is a standard variable.我通过设置CMAKE_PREFIX_PATH告诉 gRPC 在哪里可以找到 protobuf,这是一个标准变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MSYS2 MinGW 包中缺少 Protobuf grpc_cpp_plugin - Protobuf grpc_cpp_plugin missing in MSYS2 MinGW packages 在 yaml-cpp 中流式传输我自己的类型的正确方法是什么? - What is the correct way to stream my own types in yaml-cpp? 使用 Visual Studio 2017 为 Windows 设置 GRPC CPP - GRPC CPP Setup for Windows using Visual Studio 2017 这是在 CPP 中初始化向量的正确方法吗? - Is this the correct way to initialize a vector in CPP? 在Qt项目中包括zLib的正确方法是什么? - What is the correct way to include zLib in a Qt project? 从 GRPC C++ 服务器实现返回“未找到”响应的正确方法是什么? - What's the correct way to return a 'not found' response from a GRPC c++ server implementation? 在处理单独的.cpp 和.h 文件中的类时,实现 inheritance 的正确方法是什么? - What is the correct way to implement inheritance when dealing with classes in separate .cpp and .h files? 在 .cpp 文件中定义积分常数的正确方法 - Correct way to define integral constant in .cpp file 有没有办法在同一项目的另一个.cpp中使用在.cpp中声明的静态void - Is there a way to use a static void declared in .cpp in another .cpp in the same project 在protobuf中处理可选消息的正确方法是什么? - What is the proper way to handle optional messages in protobuf?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM