[英]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:我遇到了以下缺陷:
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.