[Installation] grpc source code compilation and installation (Linux)

1. Compilation environment description

1. grpc version: V1.58.0

2. gcc version: gcc7 or above (currently using gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04))

3. cmake version: 3.8 or above (currently using cmake version 3.19.6)

4. Operating system version: Ubuntu16.04

5. apt source: Alibaba Cloud

2. Compilation environment preparation

Note: Since Ubuntu 16.04 comes with its own compilation environment, gcc is 4.8 and cmake is 3.5, which does not meet the minimum requirements for compiling grpc-v1.58.0, so the gcc and cmake environments need to be upgraded.

1. Update gcc

$ apt-get install build-essential autoconf libtool pkg-config

2. Update cmake

# 1. Download the automatic installation script
  $ wget -q -O cmake-linux.sh https://github.com/Kitware/CMake/releases/download/v3.19.6/cmake-3.19.6-Linux-x86_64.sh
# 2. Execute the automatic installation script (--prefix specifies the cmake installation directory)
  $ sh cmake-linux.sh -- --skip-license --prefix=/usr/local/cmake
# 3. Delete automatic installation script
  $ rm cmake-linux.sh
# 4. Permanently set the cmake installation directory to the PATH environment variable
  $ vim /etc/profile
   (Add the last line: export PATH=/usr/local/cmake/bin:$PATH)
  $ source /etc/profile

3. Source code download

# Note: When downloading sub-modules, because the corresponding source code of github is not in the domestic server, the download will be very slow. It is best to use a ladder.
git clone --recurse-submodules -b v1.58.0 --depth 1 --shallow-submodules https://github.com/grpc/grpc

4. Source code compilation and installation of gRPC and Protocol Buffers (command line)

# 1. Enter the grpc source code root path
$ cd grpc
# 2. Create a compilation directory
$ mkdir -p cmake/build
# 3. Enter the compilation directory
$ pushd cmake/build
# 4. Execute cmake to set cmake parameters, and specify the installation directory as /usr/local/grpc (this command will be converted into a makefile for compilation)
$ cmake -DgRPC_INSTALL=ON -DgRPC_BUILD_TESTS=OFF -DCMAKE_INSTALL_PREFIX=/usr/local/grpc ../..
# 5. Compile
$ make -j 4
# 6. Installation (install the compiled target files to /usr/local/grpc)
$ make install
# 7. Exit the compilation directory
$popd
################################################ #################
# At this point, the grpc source code library has been compiled, and the generated related header files and library files are in the /usr/local/grpc directory.
# 8. View the compiled target file
$ ls /usr/local/grpc
bin include lib share

4. Source code compilation and installation of gRPC and Protocol Buffers (Qt Creator)

1. Set the Qt Creator CMake environment to 3.19.0 and set it as the default package

Set CMmake

  • Modify the CMake Tool of the build kit to the new CMake
    Modify build package

2. Open the grpc project

    1. Select CMakeLists.txt in the grpc source code directory (grpc/CMakeLists.txt)
    1. When initially opened, a window will pop up to set the build directory for cmake compilation; (the build files generated by the build will be generated in this directory)
    1. After initially opening and setting the build directory, a window will pop up to set CMake parameters;
    1. Click CMake to execute CMake; (the MakeFile file will eventually be generated)
    • Steps 3) and 4) are equivalent to the command cmake -DgRPC_INSTALL=ON -DgRPC_BUILD_TESTS=OFF -DCMAKE_INSTALL_PREFIX=/usr/local/grpc…/…; this command sets parameters and executes cmake.
      Execute cmake
    1. Then the project is opened and completed. After opening, the project structure is as follows:
      grpc cmake project

3. Modify build settings and add build steps

    1. In the default build settings, the build step only has the make command. If you want to execute make install, you can modify the build settings and add the make install build step.
      Add build step

4. Build (compile project)

Build

5. Sample program compilation (use existing CMakeLists.txt, compile all)

$ cd examples/cpp/helloworld

$ mkdir -p cmake/build

$ pushd cmake/build

# Execute cmake to generate a makefile (CMAKE_PREFIX_PATH specifies the search path of CMake, which is provided for functions such as find_package(), find_program(), find_library(), find_file(), and find_path().
# Another little knowledge point here is that CMAKE_INSTALL_PREFIX (the path to the root directory installed when executing the make install command) will be added to CMAKE_SYSTEM_PREFIX_PATH, so find_package(), find_program(), find_library(), find_path(), find_ile() You can also use the directory as prefix to search for other commands. )
$ cmake -DCMAKE_PREFIX_PATH=/usr/local/grpc ../..

#Compile (if multi-threaded compilation reports an error, just use make to compile directly)
$ make -j 4
############################

# After compilation, the following files will be generated:
root@node2:~/software/grpc/examples/cpp/helloworld/cmake/build# ls
CMakeCache.txt cmake_install.cmake greeter_async_client2 greeter_callback_client greeter_client helloworld.grpc.pb.cc helloworld.pb.cc libhw_grpc_proto.a
CMakeFiles greeter_async_client greeter_async_server greeter_callback_server greeter_server helloworld.grpc.pb.h helloworld.pb.h Makefile

6. Sample program compilation (separate compilation)

# 1. Enter the sample program directory
$ cd /root/software/grpc/examples/cpp/helloworld

# 2. Use protoc and gRPC C++ plug-ins to generate gRPC server and client interfaces from the helloworld.proto file. The generated code will be placed in the current directory (this command will generate helloworld.grpc.pb.h and helloworld. grpc.pb.cc two files)
$ protoc -I ../../protos --grpc_out=. --plugin=protoc-gen-grpc=`which grpc_cpp_plugin` ../../protos/helloworld.proto

# 3. Use protoc and C++ plug-ins to generate the corresponding data interfaces from the helloworld.proto file, that is, response and request message classes (this command will generate two files, helloworld.pb.h and helloworld.pb.cc)
$ protoc -I ../../protos --cpp_out=. ../../protos/helloworld.proto

# 4. Set the PKG_CONFIG_PATH variable (grpc related libraries are managed through pkg-config, modify this variable to specify pkg-config and search for pkgconfig in the grpc installation directory)
$ export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/grpc/lib/pkgconfig

##### The re2 library does not have a re2.pc file in the grpc directory, and the search path needs to be specified separately; otherwise, the following error will occur when using pkgconfig.
<<ErrorMsg
   Package re2 was not found in the pkg-config search path.
   Perhaps you should add the directory containing `re2.pc'
   to the PKG_CONFIG_PATH environment variable
   Package 're2', required by 'grpc', not found
   In file included from helloworld.pb.cc:4:0:
ErrorMsg
$ export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/root/software/grpc/third_party/re2/

# 5. Define variables to manage protobuf-related public dependency libraries (the purpose is to make the g++ compilation command shorter)
$ export PROTOBUF_ABSL_DEPS=absl_absl_check absl_absl_log absl_algorithm absl_base absl_bind_front absl_bits absl_btree absl_cleanup absl_cord absl_core_headers absl_debugging absl_die_if_null absl_dynamic_annotations absl_flags absl_flat_hash_map absl_flat_hash_set absl_function _ref absl_hash absl_layout absl_log_initialize absl_log_severity absl_memory absl_node_hash_map absl_node_hash_set absl_optional absl_span absl_status absl_statusor absl_strings absl_synchronization absl_time absl_type_traits absl_utility absl_variant

# 6. Use greeter_server.cc to compile the server program
$ g + + -o greeter_server helloworld.pb.cc helloworld.grpc.pb.cc greeter_server.cc -I/usr/local/grpc/include -L/usr/local/lib `pkg-config --libs --static protobuf grpc + + absl_flags absl_flags_parse $PROTOBUF_ABSL_DEPS` -lutf8_validity -pthread -Wl,--no-as-needed -lgrpc + + _reflection -Wl,--as-needed -ldl

# 7. Use greeter_client.cc to compile the client program
$ g + + -o greeter_client helloworld.pb.cc helloworld.grpc.pb.cc greeter_client.cc -I/usr/local/grpc/include -L/usr/local/lib `pkg-config --libs --static protobuf grpc + + absl_flags absl_flags_parse $PROTOBUF_ABSL_DEPS` -lutf8_validity -pthread -Wl,--no-as-needed -lgrpc + + _reflection -Wl,--as-needed -ldl

7. Commonly used parameters of the protoc command of the Protobuf compiler

  • -I …/…/protos: tells protoc where to find .proto files. In this case it will look in the …/…/protos directory
  • –grpc_out=.: Specify the output directory for generated gRPC service files. In this case, they will be generated in the current directory.
  • –plugin=protoc-gen-grpc=which grpc_cpp_plugin“: Tell protoc which plugin to use to generate gRPC code. In this case, it’s using grpc_cpp_plugin, which is the gRPC C++ plugin.
  • –cpp_out=.: Specify the output directory for generated C++ code files. In this case, they will be generated in the current directory.