今天倒腾windows下面grpc编译以及在qt下面如何简单实用
1.grpc的编译工具准备
根据官网说明,安装下面的相关工具。
Windows
To prepare for cmake + Microsoft Visual C++ compiler build
- Install Visual Studio 2015 or 2017 (Visual C++ compiler will be used).
- Install Git.
- Install CMake.
- Install Active State Perl (
choco install activeperl
) - required by boringssl- Install Go (
choco install golang
) - required by boringssl- Install yasm and add it to
PATH
(choco install yasm
) - required by boringssl- (Optional) Install Ninja (
choco install ninja
)
2.clone 代码库
Windows
@rem You can also do just "git clone --recursive -b THE_BRANCH_YOU_WANT https://github.com/grpc/grpc"
git clone --recursive -b https://github.com/grpc/grpc.git
cd grpc
@rem To update submodules at later time, run "git submodule update --init"
3.使用cmake编译
cmake: Windows, Using Visual Studio 2015 or 2017 (can only build with OPENSSL_NO_ASM).
When using the "Visual Studio" generator, cmake will generate a solution (grpc.sln) that contains a VS project for every target defined in CMakeLists.txt (+ few extra convenience projects added automatically by cmake). After opening the solution with Visual Studio you will be able to browse and build the code.
@rem Run from grpc directory after cloning the repo with --recursive or updating submodules.
md .build
cd .build
cmake .. -G "Visual Studio 14 2015" -DCMAKE_BUILD_TYPE=Release
cmake --build .
由于需要debug版本,只需要将cmake的倒数第二步修改成即可。
cmake .. -G "Visual Studio 14 2015" -DCMAKE_BUILD_TYPE=Debug
编译过程很顺利,没有什么错误提示。
4.在qt中使用生成的debug以及release库
下面是自己一步一步尝试出来的使用的pro文件,在当前目录下面建立了staticlib文件夹以及子文件夹debug与release,分别放置debug以及release库。
自己没有找到具体include 的lib指导,自己慢慢摸索出来的,有蛮多的坑的。一些简单的说明
INCLUDEPATH += ../include 是拷贝grpc/include下面的所有的头文件
其他的包含库,只需要按照下面,拷贝生成目录下面对应的debug以及release文件即可
DEFINES += _WIN32_WINNT=0x600 是grpc需要的define
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = greeter
TEMPLATE = app
INCLUDEPATH += ../include
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
CONFIG(debug, debug|release) {
LIBS += -L$$PWD/staticlib/debug/ -lcares
PRE_TARGETDEPS += $$PWD/staticlib/debug/cares.lib
LIBS += -L$$PWD/staticlib/debug/ -laddress_sorting
PRE_TARGETDEPS += $$PWD/staticlib/debug/address_sorting.lib
LIBS += -L$$PWD/staticlib/debug/ -lgpr
PRE_TARGETDEPS += $$PWD/staticlib/debug/gpr.lib
LIBS += -L$$PWD/staticlib/debug/ -lgrpc
PRE_TARGETDEPS += $$PWD/staticlib/debug/grpc.lib
LIBS += -L$$PWD/staticlib/debug/ -lgrpc++
PRE_TARGETDEPS += $$PWD/staticlib/debug/grpc++.lib
LIBS += -L$$PWD/staticlib/debug/ -llibprotobufd
PRE_TARGETDEPS += $$PWD/staticlib/debug/libprotobufd.lib
LIBS += -L$$PWD/staticlib/debug/ -lssl
PRE_TARGETDEPS += $$PWD/staticlib/debug/ssl.lib
LIBS += -L$$PWD/staticlib/debug/ -lcrypto
PRE_TARGETDEPS += $$PWD/staticlib/debug/crypto.lib
LIBS += -L$$PWD/staticlib/debug/ -ldecrepit
PRE_TARGETDEPS += $$PWD/staticlib/debug/decrepit.lib
LIBS += -L$$PWD/staticlib/debug/ -lzlibstaticd
PRE_TARGETDEPS += $$PWD/staticlib/debug/zlibstaticd.lib
} else {
LIBS += -L$$PWD/staticlib/release/ -lcares
PRE_TARGETDEPS += $$PWD/staticlib/release/cares.lib
LIBS += -L$$PWD/staticlib/release/ -laddress_sorting
PRE_TARGETDEPS += $$PWD/staticlib/release/address_sorting.lib
LIBS += -L$$PWD/staticlib/release/ -lgpr
PRE_TARGETDEPS += $$PWD/staticlib/release/gpr.lib
LIBS += -L$$PWD/staticlib/release/ -lgrpc
PRE_TARGETDEPS += $$PWD/staticlib/release/grpc.lib
LIBS += -L$$PWD/staticlib/release/ -lgrpc++
PRE_TARGETDEPS += $$PWD/staticlib/release/grpc++.lib
LIBS += -L$$PWD/staticlib/release/ -llibprotobuf
PRE_TARGETDEPS += $$PWD/staticlib/release/libprotobuf.lib
LIBS += -L$$PWD/staticlib/release/ -lssl
PRE_TARGETDEPS += $$PWD/staticlib/release/ssl.lib
LIBS += -L$$PWD/staticlib/release/ -lcrypto
PRE_TARGETDEPS += $$PWD/staticlib/release/crypto.lib
LIBS += -L$$PWD/staticlib/release/ -ldecrepit
PRE_TARGETDEPS += $$PWD/staticlib/release/decrepit.lib
LIBS += -L$$PWD/staticlib/release/ -lzlibstatic
PRE_TARGETDEPS += $$PWD/staticlib/release/zlibstatic.lib
}
LIBS += -lWs2_32 -ladvapi32
INCLUDEPATH += $$PWD/include
DEFINES += _WIN32_WINNT=0x600
SOURCES += \
main.cpp \
mainwindow.cpp \
greeter_server.cc \
helloworld.grpc.pb.cc \
helloworld.pb.cc
HEADERS += \
mainwindow.h \
helloworld.grpc.pb.h \
helloworld.pb.h \
greeter_client.h
FORMS += \
mainwindow.ui
ok
有疑问加站长微信联系(非本文作者)