我正在尝试使用与 vcpkg 管理器一起安装的 grpc 来构建和运行 grpc 示例。
我通过克隆并查找 grpc 安装了 vcpkg 管理器,如下所示:
sudo apt-get install -y unzip build-essential
git clone https://github.com/microsoft/vcpkg
cd vcpkg/
./bootstrap-vcpkg.sh -disableMetrics
./vcpkg search grpc
./vcpkg install grpc
export PATH=$PATH:$HOME/vcpkg/downloads/tools/cmake-3.14.0-linux/cmake-3.14.0-Linux-x86_64/bin
伟大的!所以我想我有grpc。我还需要 protobuf,但它是随 vcpkg 的 grpc 一起安装的。如果我尝试安装 protobuf 我会得到以下输出:
an@ubuntu:~/vcpkg$ ./vcpkg install protobuf
Computing installation plan...
The following packages are already installed:
protobuf[core]:x64-linux
Package protobuf:x64-linux is already installed
Total elapsed time: 205.3 us
The package protobuf:x64-linux provides CMake targets:
find_package(protobuf CONFIG REQUIRED)
target_link_libraries(main PRIVATE protobuf::libprotoc protobuf::libprotobuf protobuf::libprotobuf-lite
为了仔细检查我使用./vcpkg list
显示我已安装的所有软件包。这就是它的样子。注:protobuf 和 grpc
adrian@ubuntu:~/vcpkg$ ./vcpkg list
abseil:x64-linux 2020-03-03#8 an open-source collection designed to augment th...
c-ares:x64-linux 2019-5-2-1 A C library for asynchronous DNS requests
grpc:x64-linux 1.31.1 An RPC library and framework
openssl-unix:x64-linux 1.1.1h OpenSSL is an open source project that provides ...
openssl:x64-linux 1.1.1g#1 OpenSSL is an open source project that provides ...
protobuf:x64-linux 3.13.0#2 Protocol Buffers - Google's data interchange format
re2:x64-linux 2020-10-01 RE2 is a fast, safe, thread-friendly alternative...
upb:x64-linux 2020-08-19 μpb (often written 'upb') is a small protobuf i...
zlib:x64-linux 1.2.11#9 A compression library
We can also see grpc and protobuf's binarys in the /vcpkg/installed/x64-linux/libs
directory highlighted below:
太好了!所以现在我想尝试使用 vcpkg 管理器插件编译 grpc 的 hello world 示例。在 grpc 官方网站上,他们提供了构建和编译的快速说明。 (默认情况下,它们会本地安装 grpc,但卸载过程很混乱,这就是我使用 vcpkg 的原因)。所以让我们尝试使用这些进行编译grpc 快速入门指南中的说明 https://grpc.io/docs/languages/cpp/quickstart/#build-the-example:
mkdir -p cmake/build
pushd cmake/build
cmake -DCMAKE_PREFIX_PATH=$MY_INSTALL_DIR ../..
make -j
值得注意的是,一开始他们原生安装了 grpc(删除不喜欢的 vcpkg 很混乱)。他们的指示表明要设置MY_INSTALL_DIR
像这样:
export MY_INSTALL_DIR=$HOME/.local
export PATH="$PATH:$MY_INSTALL_DIR/bin"
但这是为了本地安装和编译 grpc 的二进制文件。不幸的是我不知道 vckpkg 管理器的二进制文件在哪里。我看到 cmake 安装并有一个二进制文件位于:vcpkg/downloads/tools
但我没有看到 grpc 或 protobuf 文件夹中各自的二进制文件(见下图)。 vcpkg 的二进制文件位于哪里?根据 grpc 的官方说明,我应该使用 cmake 并在下面定义它们。
find_package(gRPC CONFIG REQUIRED)
find_package(Protobuf CONFIG REQUIRED)
不幸的是,我对 vcpkg 和依赖项比较陌生,有一点使用 Makefile 的经验(我相信这与 cmake/cmakelists 有很大不同。我从未从头开始制作 cmake 文件,因为我主要使用 Makefile)。有人可以为我指明如何使用 vcpkg 编译和运行这些示例的正确方向(或建议更好的方法吗?)我的目标是学习如何使用 vcpkg 并将其集成到现有项目(如 gRPC 的示例文件)中。
更新1:
我 cd 到示例 hello world 文件夹并按照建议运行以下命令:
mkdir build-dir
cd build-dir
cmake ..
make
当我跑的时候cmake ..
我收到以下错误:
grpc/examples/cpp/helloworld/build$ cmake ..
CMake Error at /usr/share/cmake-3.16/Modules/CMakeDetermineSystem.cmake:99 (message):
Could not find toolchain file: /usr/vcpkg/scripts/buildsystems/vcpkg.cmake
Call Stack (most recent call first):
CMakeLists.txt:24 (project)
CMake Error: CMake was unable to find a build program corresponding to "Unix Makefiles". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool.
CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
-- Configuring incomplete, errors occurred!
这表明我没有使用正确的 cmake(vcpkg 使用的 cmake。所以我做了一些挖掘,并根据此vcpkg 文档 https://vcpkg.readthedocs.io/en/latest/examples/installing-and-using-packages/我需要将 vcpkg 软件包与./vcpkg integrate install
只要我将其指向其中的 cmake,它就会公开系统上的所有包。然后它说我应该播种一条路径:
~/vcpkg$ ./vcpkg integrate install
Applied user-wide integration for this vcpkg root.
CMake projects should use: "-DCMAKE_TOOLCHAIN_FILE=/home/adrian/vcpkg/scripts/buildsystems/vcpkg.cmake"
所以我尝试构建cmake ..
但我提供了DCMAKE_TOOL_CHAIN
像这样争论,但我又回到了原来的问题。
/grpc/examples/cpp/helloworld/build$ cmake .. "-DCMAKE_TOOLCHAIN_FILE=/home/adrian/vcpkg/scripts/buildsystems/vcpkg.cmake"
CMake Error at /usr/share/cmake-3.16/Modules/CMakeDetermineSystem.cmake:99 (message):
Could not find toolchain file: /usr/vcpkg/scripts/buildsystems/vcpkg.cmake
Call Stack (most recent call first):
CMakeLists.txt:24 (project)
CMake Error: CMake was unable to find a build program corresponding to "Unix Makefiles". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool.
CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
-- Configuring incomplete, errors occurred!