高手可參閱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" ./helloworld.proto
protoc -I ./ --cpp_out=./ ./helloworld.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币)
/* 完整demo,不要C币,歡迎指正 QQ 278397935 */
https://download.csdn.net/download/tadpole_java/85695361
=====================後記==================
限于本人水準,閱讀gRPC官網,一頭霧水。
參考csdn上各位大神傑作,跌跌撞撞,總算大功告成。
各位看官老爺們,請多多指教。
感謝csdn上的前輩們,謝謝!