使用 vcpkg 编译 gRPC C++ 示例

2024-03-07

我正在尝试使用与 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: enter image description here

太好了!所以现在我想尝试使用 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!

看来我导出了版本错误我的 vcpkg 目录中的 cmake 。

我导出了 3.14.0,这是我的 ubuntu 附带的默认 cmake 版本,而不是随 vcpkg 安装的 cmake,即 cmake 版本 3.17.2。下面解决了我的问题,以便我能够随后编译示例。

export PATH=$PATH:$HOME/vcpkg/downloads/tools/cmake-3.17.2-linux/cmake-3.17.2-Linux-x86_64/bin

检查以确保它已添加到路径中:

a@ubuntu:~/grpc/examples/cpp/helloworld/build$ echo $PATH | grep cmake
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/adrian/vcpkg/downloads/tools/cmake-3.17.2-linux/cmake-3.17.2-Linux-x86_64/bin

CD到grpc的examples hello world文件夹并编译:

a@ubuntu:~/grpc/examples/cpp/helloworld$ mkdir build
a@ubuntu:~/grpc/examples/cpp/helloworld$ mkdir build
a@ubuntu:~/grpc/examples/cpp/helloworld$ cd build/
a@ubuntu:~/grpc/examples/cpp/helloworld/build$ cmake .. "-DCMAKE_TOOLCHAIN_FILE=/home/adrian/vcpkg/scripts/buildsystems/vcpkg.cmake"
-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE  
CMake Warning (dev) at /home/adrian/vcpkg/installed/x64-linux/share/protobuf/protobuf-options.cmake:6 (option):
  Policy CMP0077 is not set: option() honors normal variables.  Run "cmake
  --help-policy CMP0077" for policy details.  Use the cmake_policy command to
  set the policy and suppress this warning.

  For compatibility with older versions of CMake, option is clearing the
  normal variable 'protobuf_MODULE_COMPATIBLE'.
Call Stack (most recent call first):
  /home/adrian/vcpkg/installed/x64-linux/share/protobuf/protobuf-config.cmake:2 (include)
  /home/adrian/vcpkg/installed/x64-linux/share/protobuf/vcpkg-cmake-wrapper.cmake:13 (_find_package)
  /home/adrian/vcpkg/scripts/buildsystems/vcpkg.cmake:481 (include)
  CMakeLists.txt:103 (find_package)
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Using protobuf 
-- Found ZLIB: /home/adrian/vcpkg/installed/x64-linux/debug/lib/libz.a (found version "1.2.11") 
-- Found OpenSSL: /home/adrian/vcpkg/installed/x64-linux/debug/lib/libcrypto.a (found version "1.1.1h")  
-- Found c-ares: /home/adrian/vcpkg/installed/x64-linux/share/c-ares/c-ares-config.cmake (found version "1.15.0") 
-- Using gRPC 1.31.1
-- Configuring done
-- Generating done
-- Build files have been written to: /home/adrian/grpc/examples/cpp/helloworld/build
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 vcpkg 编译 gRPC C++ 示例 的相关文章

随机推荐

  • 检索当前正在运行的应用程序的导航历史记录

    我想要一种方法来向用户显示他当前在应用程序中的位置 例如 如果我有activity A activity B activity C等等 如果他导航到activity E 例如 他应该能够看到他如何登陆当前屏幕的历史记录 从Home gt E
  • ASP.Net MVC 4 捆绑包

    我见过的很多代码都引用了这个 section Scripts Scripts Render bundles jqueryval 这很棒 而且它可以工作 如果包含 某些东西 我是否必须添加参考才能获取这些 使用 NuGet 复制 DLL 这是
  • C++ 全局初始化可以有多懒?

    我习惯于认为全局 静态类成员的所有初始化都发生在 main 的第一行之前 但我最近在某处读到该标准允许稍后进行初始化以 协助模块的动态加载 在动态链接时我可以看到这是真的 我不希望在 dlopen 库之前初始化库中初始化的全局变量 然而 在
  • 有没有办法在 return 语句之后做更多的工作?

    我有点好奇返回结果后是否可以在函数中做更多的工作 基本上 我正在使用金字塔框架 只是用 python 编码 创建一个网站 在处理输入后 我返回变量来渲染页面 但有时我想在渲染页面后做更多工作 例如 您访问我的网站并更新您的个人资料 您所关心
  • BigQuery 数据集通配符?

    我用过tableBigQuery 中的通配符功能 但是否可以使用通配符跨多个查询datasets在一个项目内 不 目前这是不可能的 技术原因之一是 如果表通配符跨越数据集 查找涉及的表会更加困难
  • html文本框形式不允许输入

    html 表单中是否有任何类不允许您在该文本框中输入或更改值 但你可以看到它的内容 例如下面的代码将允许你看到mysql数据库中记录的内容 但我想要的是它不被编辑 我将在下面的代码中添加什么 以便用户不会编辑其内容 tr td font s
  • 使用 ListAdapter 填充 ScrollView 布局内的 LinearLayout

    我面临一个非常常见的问题 我布置了一个活动 现在发现它应该显示其中的一些项目ScrollView 通常的方法是使用现有的ListAdapter 将其连接到ListView and BOOM我会有我的物品清单 BUT你不应该放置一个嵌套的Li
  • 如何在 Eclipse 中指定预处理器指令?

    Eclipse 中如何为不同的配置指定预处理器指令 例如 如果我有多个电源 应该以不同的配置运行并指定 ifdef Problem1 main func endif Problem1 请注意 这是托管 makefile 的情况 The 文档
  • 同位素 v2 网格 - 多个过滤器 - 隐藏空过滤器

    我当前的同位素网格有两个下拉过滤器 用于对网格项目进行排序 第一个过滤器是菜单类型 第二个过滤器是饮料类型 然而 每种菜单类型并不包含所有饮料类型 因此当选择某些过滤器配置时 不会显示任何结果 这是正确的 但我想通过当用户选择第一个过滤器时
  • jquery 不从 javascript 函数调用 Web 方法

    长期从事 WinForm 程序员 但对 Web 编程领域还是个新手 我有 Visual Studio 2010 并且创建了一个新的网站项目 我似乎无法让 ajax 调用我创建的 webmethod 当我单击页面上的按钮时 什么也没有发生 当
  • 使用 Anaconda 设置 Hydrogen 和 Atom 管理 python 安装

    我已将我的 python 3 可执行文件添加到系统路径 违背 Anaconda 的建议 以尝试让 Hydrogen 以及实际上任何 Atom 扩展 插件 在 Atom 中运行代码行或代码块 脚本 原子插件似乎可以工作 我选择一些代码并输入c
  • npm 错误!致命:无法找到“https”的远程帮助程序

    我在跑npm install在远程会话的 React Native 项目中 更具体地说 这npm install是我运行的构建脚本的一部分VSTS代理 https github com microsoft vsts agent 我遇到了这个
  • 在mockito中模拟通用scala方法

    我正在开发一个 Scala 项目 使用 Mockito 作为模拟框架 我想模拟以下通用 Scala 方法 def parseXml T ClassTag xmlUrl URL xsdUrl Option URL Option T 当嘲笑时
  • 用于将十六进制字符串转换为字节数组的预处理器宏

    我已将 AES 128 密钥定义为 IDE 中的构建符号 以便它像这样调用 GCC arm none eabi gcc D AES KEY 3B7116E69E222295163FF1CAA1681FAC 这相当于 define AES K
  • 如何在 html 内使用双百分号(%%variable%%)括起来的 PHP 变量

    我正在修改发送到电子邮件的 html 文件 我需要通过以下方式创建一些检查PHP到变量但是我不知道双倍百分比是多少 我尝试在网上搜索 但我看到的结果甚至与编程无关 我需要确定何时发送另一封邮件 因为我只能在 30 分钟后发送邮件时才能看到输
  • 如何快速更新锚点约束

    我想在 iOS 中创建一个像 android 一样的菜单 我在用着布局限制建立限制条件 当我尝试更新左侧时 我遇到了这个问题约束单击按钮时的图像 它应该动画到单击按钮的位置 谁能帮我 它应该支持横向和纵向 我不想使用第三方代码 也不想使用N
  • log4net 日志正在创建但仍为空

    我正在尝试使用log4net http logging apache org log4net 当我启动应用程序时 它会创建日志文件 但无论我调用多少次Log Info Application Started 它仍然是空的 我研究了谷歌返回的
  • AWS IOT 连接在 IPAD OS v12.1.1 上关闭

    当我们在 ios 版本 12 1 1 上使用 wss 协议连接到 AWS IoT 时 我们能够成功连接到 IoT 但立即我们可以看到 IoT 触发 onError 事件 然后连接关闭 它尝试再次重新连接 但没有任何运气 我们从 IoT 得到
  • Ruby 中的舍入不一致?

    Ruby 的舍入有错误吗 为什么它的行为是这样的 gt gt 1 14 1 15 1 16 map x 1f x gt 1 1 1 1 1 2 gt gt 1 4 1 5 1 6 map x 0f x gt 1 2 2 例如 为什么 1 1
  • 使用 vcpkg 编译 gRPC C++ 示例

    我正在尝试使用与 vcpkg 管理器一起安装的 grpc 来构建和运行 grpc 示例 我通过克隆并查找 grpc 安装了 vcpkg 管理器 如下所示 sudo apt get install y unzip build essentia