使用vcpkg编译gRPC C ++示例 [英] Compile gRPC C++ Examples with vcpkg

查看:358
本文介绍了使用vcpkg编译gRPC C ++示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用vcpkg管理器安装的grpc来构建和运行grpc示例.

我通过克隆并找到grpc来安装vcpkg管理器,如下所示:

  sudo apt-get install -y解压缩build-essentialgit clone https://github.com/microsoft/vcpkgcd vcpkg/./bootstrap-vcpkg.sh -disableMetrics./vcpkg搜索grpc./vcpkg安装grpc导出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正在计算安装计划...已安装以下软件包:protobuf [核心]:x64-linux软件包protobuf:x64-linux已经安装总耗时:205.3 us软件包protobuf:x64-linux提供了CMake目标:find_package(需要protobuf配置)target_link_libraries(main PRIVATE protobuf :: libprotoc protobuf :: libprotobuf protobuf :: libprotobuf-lite 

要仔细检查,我使用 ./vcpkg list 来显示我已安装的所有软件包.这是它的样子.注意:protobuf和grpc

  adrian @ ubuntu:〜/vcpkg $ ./vcpkg列表abseil:x64-linux 2020-03-03#8一个旨在增强功能的开源集合...c-ares:x64-linux 2019-5-2-1一个用于异步DNS请求的C库grpc:x64-linux 1.31.1 RPC库和框架openssl-unix:x64-linux 1.1.1h OpenSSL是一个开源项目,可提供...openssl:x64-linux 1.1.1g#1 OpenSSL是一个开源项目,可提供...protobuf:x64-linux 3.13.0#2协议缓冲区-Google的数据交换格式re2:x64-linux 2020-10-01 RE2是一种快速,安全,线程友好的替代方案...upb:x64-linux 2020-08-19μpb(通常写为'upb')是一个小型protobuf ...zlib:x64-linux 1.2.11#9压缩库 

我们还可以在以下突出显示的/vcpkg/installed/x64-linux/libs 目录中看到grpc和protobuf的二进制文件:

好极了!因此,现在我想尝试使用vcpkg管理器插件编译grpc的hello world示例.在grpc的官方网站上,它们提供了构建和编译的快速说明.(默认情况下,它们是在本地安装grpc的,但它是一团糟的卸载,这就是为什么我使用vcpkg的原因).因此,让我们尝试使用这些

更新1:

我进入示例hellow world文件夹,并按照建议运行以下内容:

  mkdir build-dircd生成目录cmake ..制作 

当我运行 cmake .. 时,出现以下错误:

  grpc/examples/cpp/helloworld/build $ cmake ../usr/share/cmake-3.16/Modules/CMakeDetermineSystem.cmake:99(消息)的CMake错误:找不到工具链文件:/usr/vcpkg/scripts/buildsystems/vcpkg.cmake呼叫堆栈(最近的呼叫优先):CMakeLists.txt:24(项目)CMake错误:CMake无法找到与"Unix Makefiles"相对应的构建程序.未设置CMAKE_MAKE_PROGRAM.您可能需要选择其他构建工具.CMake错误:EnableLanguage之后未设置CMAKE_C_COMPILERCMake错误:EnableLanguage之后未设置CMAKE_CXX_COMPILER-配置不完整,发生错误! 

这表明我没有使用正确的cmake(vcpkg使用的cmake.所以我做了一些挖掘,并根据此 <代码>〜/vcpkg $ ./vcpkg集成安装为此vcpkg根应用了用户范围的集成.CMake项目应使用:-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"/usr/share/cmake-3.16/Modules/CMakeDetermineSystem.cmake:99(消息)的CMake错误:找不到工具链文件:/usr/vcpkg/scripts/buildsystems/vcpkg.cmake呼叫堆栈(最近的呼叫优先):CMakeLists.txt:24(项目)CMake错误:CMake无法找到与"Unix Makefiles"相对应的构建程序.未设置CMAKE_MAKE_PROGRAM.您可能需要选择其他构建工具.CMake错误:EnableLanguage之后未设置CMAKE_C_COMPILERCMake错误:EnableLanguage之后未设置CMAKE_CXX_COMPILER-配置不完整,发生错误! 

似乎我导出了vcpkg目录中cmake的 错误版本 .

我导出了3.14.0,这是我的ubuntu附带的默认cmake版本,而不是vcpkg附带的cmake版本3.17.2附带的cmake.下面解决了我的问题,因此我以后可以编译示例.

 <代码> 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的示例hello world文件夹并进行编译:

 <代码> a @ ubuntu:〜/grpc/examples/cpp/helloworld $ mkdir builda @ ubuntu:〜/grpc/examples/cpp/helloworld $ mkdir builda @ 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"-C编译器标识为GNU 9.3.0-CXX编译器标识为GNU 9.3.0-检查工作的C编译器:/usr/bin/cc-检查工作的C编译器:/usr/bin/cc-工作-检测C编译器ABI信息-检测C编译器ABI信息-完成-检测C编译功能-检测C编译功能-完成-检查可运行的CXX编译器:/usr/bin/c ++-检查工作中的CXX编译器:/usr/bin/c ++-工作-检测CXX编译器ABI信息-检测CXX编译器ABI信息-完成-检测CXX编译功能-检测CXX编译功能-完成-寻找pthread.h-寻找pthread.h-找到-执行测试CMAKE_HAVE_LIBC_PTHREAD-执行测试CMAKE_HAVE_LIBC_PTHREAD-失败-在pthreads中寻找pthread_create-在pthreads中寻找pthread_create-未找到-在pthread中寻找pthread_create-在pthread中寻找pthread_create-找到-找到的线程:TRUE在/home/adrian/vcpkg/installed/x64-linux/share/protobuf/protobuf-options.cmake:6(option)处的CMake警告(dev):未设置策略CMP0077:option()支持普通变量.运行"cmake"--help-policy CMP0077"有关政策的详细信息.使用cmake_policy命令来设置策略并禁止显示此警告.为了与CMake的较早版本兼容,请清除该选项.正常变量"protobuf_MODULE_COMPATIBLE".呼叫堆栈(最近的呼叫优先):/home/adrian/vcpkg/installed/x64-linux/share/protobuf/protobuf-config.cmake:2(包括)/home/adrian/vcpkg/installed/x64-linux/share/protobuf/vcpkg-cmake-wrapper.cmake:13(_find_package)/home/adrian/vcpkg/scripts/buildsystems/vcpkg.cmake:481(包括)CMakeLists.txt:103(find_package)此警告是针对项目开发人员的.使用-Wno-dev禁止它.-使用protobuf-找到ZLIB:/home/adrian/vcpkg/installed/x64-linux/debug/lib/libz.a(找到的版本为"1.2.11")-找到OpenSSL:/home/adrian/vcpkg/installed/x64-linux/debug/lib/libcrypto.a(找到的版本为1.1.1h)-找到的c-ares:/home/adrian/vcpkg/installed/x64-linux/share/c-ares/c-ares-config.cmake(找到的版本为"1.15.0")-使用gRPC 1.31.1-配置完成-完成生成-构建文件已写入:/home/adrian/grpc/examples/cpp/helloworld/build 

I am trying to build and run the grpc examples with grpc that was installed with vcpkg manager.

I installed vcpkg manager by cloning and finding grpc like so:

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 

Great! So I think I have grpc. I also needed protobuf but it came installed with vcpkg's grpc. If I try to install protobuf I get the following output:

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

To double check I use ./vcpkg list to show all the packages I have installed. Heres what it looks like. Note: protobuf and 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:

Ok great! So now I want to try and compile grpc's hello world examples with the vcpkg manager plugins. In the official grpc website they have a quick instructions for building then compiling. (by default they install grpc natively but its a mess uninstall which is why I went with vcpkg instead).So lets try to compile with these instructions from grpc's quick start guide:

mkdir -p cmake/build
pushd cmake/build
cmake -DCMAKE_PREFIX_PATH=$MY_INSTALL_DIR ../..
make -j

Its important to note that at the beginning they installed grpc natively (which is messy to remove unliked vcpkg). Their instructions indicate to set MY_INSTALL_DIR like so:

export MY_INSTALL_DIR=$HOME/.local
export PATH="$PATH:$MY_INSTALL_DIR/bin"

But this is for installing and compiling grpc's binaries natively. Unfortunately I dont know where the binaries are for vckpkg manager. I see that cmake installs and has a binary located at: vcpkg/downloads/tools but I do not see grpc or protobuf folder for their respecive binaries (see image below). Where would the binaries be located for vcpkg? Per grpc's official instructions I am supposed to use cmake and define these below.

find_package(gRPC CONFIG REQUIRED)
find_package(Protobuf CONFIG REQUIRED)

Unfortunately I am relatively new to vcpkg and dependencies with a little experience using Makefiles (which i believe are much different than cmake/cmakelists. I never made cmake files from scratch as I primarily use Makefiles). Could someone please point me in the right direction how to compile and run these examples with vcpkg (or suggest a better approach?) My goal is to learn how to use vcpkg and integrate it into an existing project like gRPC's example files.

Update 1:

I cd to the example hellow world folder and ran the following as suggested:

mkdir build-dir
cd build-dir
cmake ..
make

When I ran cmake .. I got the following error:

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!

This was an indicator that I am not using the correct cmake (the one used by vcpkg. So i did a little digging and according to this vcpkg documentation I need to integrate vcpkg packages with ./vcpkg integrate install which exposes all my packages on my system as long as I point it to the cmake within. It then says I should seed a path:

~/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"

So I try to build with cmake .. but instead I provide the DCMAKE_TOOL_CHAIN argument like so but I am back to the original problem as before.

/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!

解决方案

It appears that I exported the wrong version of cmake that was in my vcpkg directory.

I exported 3.14.0 which was the default cmake version that came with my ubuntu instead of the cmake that came installed with vcpkg which is cmake version 3.17.2. Below resolved my issue so I was able to compile the examples afterwards.

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

Check to make sure it was added to paths:

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 to grpc's examples hello world folder and compile:

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

这篇关于使用vcpkg编译gRPC C ++示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆