本文章参考了多位大佬与gRPC官方的文章,在此拜谢,以下仅为本人自己的编译过程与使用的分享,如有错误,还望指正。
gRPC在ubuntu系统下的安装和使用https://blog.csdn.net/w13l14/article/details/117984322gRPC官方https://grpc.io/docs/languages/cpp/quickstart/ubuntu20.04 编译安装 grpc-c++https://blog.csdn.net/Bart__/article/details/106992395#:~:text=%E7%BC%96%E8%AF%91%E5%AE%89%E8%A3%85grpc%20%23%E5%AE%89%E8%A3%85%E4%BB%A5%E4%B8%8B%E7%BB%84%E4%BB%B6%20%24%20sudo%20apt-get%20install%20build-essential%20autoconf,%23%E7%BC%96%E8%AF%91%E5%AE%89%E8%A3%85grpc%E5%BA%93%20%24%20mkdir%20-p%20cmake%2Fbuild%20%24%20pushd%20cmake%2Fbuild
1、环境/工具
(1)Ubuntu 20.04(arm)
(2)Qt 5.12.8
(3)CMake 3.28.3
(4)Git
(5)GCC/G++ 9.4.0
注意:gRPC对CMake与GCC/G++版本有所要求,最好能够升级到较高的版本,具体的升级方法请查阅网上资料,本人在这里只升级了CMake,故在下方只列出CMake的升级文章
ubuntu18.04的cmake升级https://blog.csdn.net/Boys_Wu/article/details/104940575
2、安装编译工具
在ubuntu中打开终端,输入以下代码,安装gRPC的编译工具
$ sudo apt install -y build-essential autoconf libtool pkg-config
另外,本人额外安装了libssl-dev工具,不知道有啥用,先记录进来
$ sudo apt-get install libssl-dev
3、下载gRPC(1.48版本)
具体参照我之前的文章,中第4章(1)中的内容。windows+vs2017 C++编译gRPChttps://blog.csdn.net/weixin_44328139/article/details/136504504?spm=1001.2014.3001.5502注意:仅下载gRPC代码,必须下载完全,(可以重复执行 git submodule update --init --recursive 命令,直到没有错误提示)。
之后将源码目录下third_party\zlib\CMakeList.txt的第一行依赖的CMake版本修改为2.8.12;
4、使用CMake编译gRPC
(1)创建编译文件夹build
进入刚刚git clone 下来的grpc目录,使用以下代码,创建文件编译文件夹
mkdir arm64_build
(2)编译gRPC
1.进入创建的文件夹
cd arm64_build
2.使用以下代码进行编译设置
cmake -DgRPC_INSTALL=ON -DCMAKE_BUILD_TYPE=Release -DgRPC_BUILD_TESTS=OFF -DCMAKE_INSTALL_PREFIX=/home/nvidia/workspace/grpc/GRPCPACKAGE -DgRPC_SSL_PROVIDER=package ..
注意:最后有 “..”
3.执行以下代码编译,等待编译完成
make -j 8
注意:编译过程中是否存在红色报错,waring信息忽略,若存在报错,可能是git clone代码下载不完全,若确认代码下载完全,则可网上查找相关错误问题。
(3)安装gRPC
make install
通过此条代码进行安装gRPC,安装的路径就是2.编译设置中-DCMAKE_INSTALL_PREFIX所对应的路径。
至此,gRPC安装完成。
5、Qt使用gRPC
(1)Qt添加gRPC库文件(注意引用顺序,不能更改)
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -lgrpc
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -lgpr
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_hashtablez_sampler
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_hash
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_random_distributions
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_random_seed_sequences
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_random_internal_pool_urbg
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_random_internal_randen
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_random_internal_randen_hwaes
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_random_internal_randen_hwaes_impl
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_random_internal_randen_slow
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_random_internal_platform
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_random_internal_seed_material
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_random_seed_gen_exception
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_synchronization
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_cord
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_cordz_info
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_cord_internal
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_cordz_functions
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_statusor
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_status
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_base
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_stacktrace
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_symbolize
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_debugging_internal
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_kernel_timeout_internal
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_time
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_int128
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_strings
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_strings_internal
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_malloc_internal
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_log_flags
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_log_globals
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_raw_logging_internal
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_bad_any_cast_impl
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_bad_optional_access
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_bad_variant_access
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_city
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_civil_time
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_cordz_handle
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_cordz_sample_token
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_demangle_internal
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_examine_stack
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_exponential_biased
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_failure_signal_handler
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_flags_commandlineflag_internal
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_flags_commandlineflag
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_flags_config
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_flags_internal
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_flags_marshalling
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_flags_parse
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_flags_private_handle_accessor
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_flags_program_name
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_flags_reflection
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_flags_usage_internal
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_flags_usage
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_flags
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_graphcycles_internal
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_leak_check
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_log_entry
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_log_flags
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_log_globals
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_log_initialize
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_log_internal_check_op
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_log_internal_conditions
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_log_internal_format
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_log_internal_globals
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_log_internal_log_sink_set
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_log_internal_message
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_log_internal_nullguard
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_log_internal_proto
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_log_severity
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_log_sink
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_low_level_hash
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_malloc_internal
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_periodic_sampler
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_raw_hash_set
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_scoped_set_env
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_spinlock_wait
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_strerror
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_str_format_internal
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_throw_delegate
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -labsl_time_zone
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -laddress_sorting
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -lcares
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -lssl
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -lcrypto
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -lgrpc++_unsecure
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -lgrpc_plugin_support
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -lgrpc_unsecure
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -lgrpc++_alts
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -lgrpc++_error_details
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -lgrpc++_reflection
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -lgrpc++
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -lgrpcpp_channelz
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -lprotobuf-lite
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -lprotobuf
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -lprotoc
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -lre2
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -lupb
LIBS += -L$$PWD/../../grpc/GRPCPACKAGE/lib/ -lz
注意:这些导入的类需要在其他类之前导入,另外此处头文件与库文件路径本人采用了相对路径,可自行改为绝对路径
(2)Qt添加gRPC头文件
INCLUDEPATH += $$PWD/../../grpc/GRPCPACKAGE/include
DEPENDPATH += $$PWD/../../grpc/GRPCPACKAGE/include
(3)Qt添加proto相关.pb.h等文件
具体proto编译相关方法,请参照我的上篇文章第5章的(1)编译proto文件的操作
windows+vs2017 C++编译gRPChttps://blog.csdn.net/weixin_44328139/article/details/136504504?spm=1001.2014.3001.5502添加.pb.h等相关文件到项目目录中
(4)编写类方法使用proto
略
6、踩过的坑
在Ubuntu Qt 中使用grpc 的使用类(即对proto中通讯进行了简单封装以供使用的类)时,仅仅包含了头文件,编译没有错误,新建类并进行实例化,即如下:
GrpcClient demoClient(grpc::CreateChannel(target_str, grpc::InsecureChannelCredentials()));
出现奇怪错误——显示各种缺少包含的库,当增加了类中首个通讯协议的发送函数的使用(即使此行代码不会被使用到,如下),也能编译通过,目前没有找到原因。
GrpcClient demoClient(grpc::CreateChannel(target_str, grpc::InsecureChannelCredentials()));
fun(){
demoClient.sendDemo(1);
}