高手可参阅gRPC官网,以免浪费时间。
=============================
Qt框架下有两种编译方式,MSVC和MinGW。
MSVC方式,git命令clone vcpkg资源,使用vcpkg来安装gRPC和protobuf,然后cmake编译。尝试过,CMake 构建阶段失败,原因可能是git clone 资源不完整。
MinGW方式,使用msys2获取gRPC Package。成功。
=========================================================================
Qt、QtCreator、Perl64 已安装
=============各位看官老爷们,现在开搞============
下载msys2,安装到C盘。
在msys2中执行以下命令:
pacman -Syu
pacman -S mingw-w64-x86_64-grpc
/* 之前我已经安装了Qt6.3.0,以下4行我没执行。且不建议使用 msys2来安装Qt,尝试过,会出现clone失败,找不到镜像文件 */
//pacman -S mingw-w64-x86_64-qt5
//pacman -S mingw-w64-x86_64-qt-creator
//pacman -S mingw-w64-x86_64-clang
//pacman -S mingw-w64-x86_64-qt5-static
待无误完成,查看msys2目录
此时电脑中就有了gRPC的静态库和*.h等文件。
把helloworld.proto文件放到C:\msys64\mingw64\bin目录。
syntax = "proto3";
//这行没有,编译报错 “helloworld 未定义”
package helloworld;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
cmd 命令行, 切到 C:\msys64\mingw64\bin 目录中,分别执行两行指令:
protoc -I ./ --grpc_out=./ --plugin=protoc-gen-grpc="C:\msys64\mingw64\bin\grpc_cpp_plugin.exe" ./protos/*.proto
protoc -I ./ --cpp_out=./ ./protos/*.proto
C:\msys64\mingw64\bin 目录下生成新的文件:
新建Qt项目。
参考壹号前辈博文(*.pro文件做了修改,否则编译失败),新建服务端和客户端project。
server端 client 端 *.pro
QT -= gui
CONFIG += c++17 console
CONFIG -= app_bundle
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
DISTFILES += \
helloworld.proto
HEADERS += \
helloworld.grpc.pb.h \
helloworld.pb.h
SOURCES += \
helloworld.grpc.pb.cc \
helloworld.pb.cc \
main.cpp
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
DEFINES += _WIN32_WINNT=0x600
LIBS += -LC:/msys64/mingw64/lib/ -llibabsl_bad_optional_access.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibabsl_bad_variant_access.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibabsl_base.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibabsl_city.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibabsl_civil_time.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibabsl_cord.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibabsl_debugging_internal.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibabsl_demangle_internal.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibabsl_exponential_biased.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibabsl_graphcycles_internal.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibabsl_hash.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibabsl_hashtablez_sampler.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibabsl_int128.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibabsl_log_severity.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibabsl_malloc_internal.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibabsl_raw_hash_set.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibabsl_raw_logging_internal.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibabsl_spinlock_wait.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibabsl_stacktrace.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibabsl_status.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibabsl_str_format_internal.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibabsl_strings.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibabsl_strings_internal.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibabsl_symbolize.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibabsl_synchronization.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibabsl_throw_delegate.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibabsl_time.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibabsl_time_zone.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibaddress_sorting.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibcares.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibcrypto.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibgpr.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibgrpc.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibgrpc_plugin_support.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibgrpc_unsecure.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibgrpc++.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibgrpc++_alts.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibgrpc++_error_details.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibgrpc++_reflection.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibgrpc++_unsecure.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibgrpcpp_channelz.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibprotobuf.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibre2.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibssl.dll
LIBS += -LC:/msys64/mingw64/lib/ -llibupb.dll
LIBS += -LC:/msys64/mingw64/bin/ -lzlib1
INCLUDEPATH += C:/msys64/mingw64/include
DEPENDPATH += C:/msys64/mingw64/include
请注意,我用的是绝对路径。最后一条LIBS 源于bin目录 。参考贰号前辈博文
LIBS += -L$$PWD/ -lXXX // 这里XXX是你的lib文件名字(注意,不要加lib的后缀)
把 helloworld.grpc.pb.cc helloworld.pb.cc helloworld.proto helloworld.grpc.pb.h helloworld.pb.h文件添加到server和client目录中。
/* 完整demo,不要C币,欢迎指正 QQ 278397935 */
https://download.youkuaiyun.com/download/tadpole_java/85695361
=====================后记==================
限于本人水平,阅读gRPC官网,一头雾水。
参考csdn上各位大神杰作,跌跌撞撞,总算大功告成。
各位看官老爷们,请多多指教。
感谢csdn上的前辈们,谢谢!