使用 vcpkg 编译 gRPC C++ 示例
Compile gRPC C++ Examples with vcpkg
我正在尝试构建和运行 grpc 示例,其中 grpc 是通过 vcpkg 管理器安装的。
我通过克隆和查找 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
我们还可以在下面突出显示的 /vcpkg/installed/x64-linux/libs
目录中看到 grpc 和 protobuf 的二进制文件:
好的,太棒了!所以现在我想尝试使用 vcpkg 管理器插件编译 grpc 的 hello world 示例。在官方 grpc 网站上,他们有构建和编译的快速说明。 (默认情况下,他们本机安装 grpc,但卸载时一团糟,这就是我改用 vcpkg 的原因)。所以让我们尝试使用这些 instructions from grpc's quick start guide:
进行编译
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 和依赖关系比较陌生,对使用 Makefiles 有一点经验(我认为这与 cmake/cmakelists 有很大不同。我从来没有从头开始制作 cmake 文件,因为我主要使用 Makefiles)。有人能给我指出正确的方向如何使用 vcpkg 编译和 运行 这些示例(或建议更好的方法吗?)我的目标是学习如何使用 vcpkg 并将其集成到现有项目中,例如 gRPC 的示例文件.
更新一:
我 cd 到示例 hellow 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 使用的那个。所以我做了一些挖掘,根据这个 vcpkg documentation 我需要将 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 的示例 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
我正在尝试构建和运行 grpc 示例,其中 grpc 是通过 vcpkg 管理器安装的。
我通过克隆和查找 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
我们还可以在下面突出显示的 /vcpkg/installed/x64-linux/libs
目录中看到 grpc 和 protobuf 的二进制文件:
好的,太棒了!所以现在我想尝试使用 vcpkg 管理器插件编译 grpc 的 hello world 示例。在官方 grpc 网站上,他们有构建和编译的快速说明。 (默认情况下,他们本机安装 grpc,但卸载时一团糟,这就是我改用 vcpkg 的原因)。所以让我们尝试使用这些 instructions from grpc's quick start guide:
进行编译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 和依赖关系比较陌生,对使用 Makefiles 有一点经验(我认为这与 cmake/cmakelists 有很大不同。我从来没有从头开始制作 cmake 文件,因为我主要使用 Makefiles)。有人能给我指出正确的方向如何使用 vcpkg 编译和 运行 这些示例(或建议更好的方法吗?)我的目标是学习如何使用 vcpkg 并将其集成到现有项目中,例如 gRPC 的示例文件.
更新一:
我 cd 到示例 hellow 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 使用的那个。所以我做了一些挖掘,根据这个 vcpkg documentation 我需要将 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 的示例 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