github 源码https://github.com/grpc/grpc

参考资料:

  • 源码解析:https://cloud.tencent.com/developer/article/1414800
  • grpc介绍:
    https://medium.com/@andrewvetovitz/grpc-c-introduction-45a66ca9461f
  • Getting Started with gRPC and Bazel
    https://medium.com/@pencilflip/getting-started-with-grpc-and-bazel-24725fd9e5c2

grpc的编译

1.选择一个版本

git checkout v1.28.x

2.初始化

git submodule init  # 初始化本地.gitmodules文件
git submodule update  # 同步远端submodule源码

3.安装cmake

$ export MY_INSTALL_DIR=$HOME/local
$ mkdir -p $MY_INSTALL_DIR
$ export PATH="$PATH:$MY_INSTALL_DIR/bin"
$ wget -q -O cmake-linux.sh https://github.com/Kitware/CMake/releases/download/v3.17.0/cmake-3.17.0-Linux-x86_64.sh
$ sh cmake-linux.sh -- --skip-license --prefix=$MY_INSTALL_DIR
$ rm cmake-linux.sh

4.安装依赖

$ sudo apt install -y build-essential autoconf libtool pkg-config

5.编译和安装。参考https://github.com/grpc/grpc/blob/master/BUILDING.md

$ mkdir -p cmake/build
$ pushd cmake/build
$ cmake ../..
$ make
$ make install
$ popd

编译和运行example

$ cd examples/cpp/helloworld
$ mkdir -p cmake/build
$ pushd cmake/build
$ cmake ../..
$ make -j

1.Run the server:

$ ./greeter_server

2.From a different terminal, run the client and see the client output:

$ ./greeter_client
Greeter received: Hello world

如何debug

1.如果想debug,则需要修改CMakeLists.txt。在if前面设置GRPC_AS_SUBMODULE为true。就会使用编译的源码debug。然后在编译的时候增加参数–config Debug

set(GRPC_AS_SUBMODULE true)

if(GRPC_AS_SUBMODULE)

2.使用vs code 进行debug。有些人喜欢gdb进行debug。但是vs code的图形化debug对新手来说比较直观。修改玩上面的参数后,即可debug。具体可查看官方教程:https://code.visualstudio.com/Docs/editor/debugging 。
vs code 使用cmake插件编译(ctrl + shift + p 选择build)的时候会自己加上debug参数。我这里控制台打印的命令如下:

[proc] Executing command: /home/carl/local/bin/cmake --build /home/carl/workspace/grpc/examples/cpp/helloworld/build --config Debug --target all -- -j 6

使用vscode 的cmake插件进行debug的命令:ctrl + shift + p 选择set debug.选择greeter_server.然后在控制台运行greeter_client就可以开始debug了。

注意,vs code进行debug时,打开的目录(即根目录)为helloworld目录。而不是grpc的根目录。

3.如果想看到debug日志的输出。可以如下命令

$ export GRPC_VERBOSITY=DEBUG
$ export GRPC_TRACE=api ## 跟踪api的调用

更多参数:https://github.com/grpc/grpc/blob/master/doc/environment_variables.md

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐