Compile gRPC C++ Examples with vcpkg

4.8k views Asked by At

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: enter image description here

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.

vcpkg's installed package directory location

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

There are 1 answers

0
Potion On BEST ANSWER

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