下载地址:
https://github.com/protocolbuffers/protobuf/releases
目前最新版是Protocol Buffers v3.12.0-rc2
经测试在windows上,用Qt Creator5.14.1自带的Mingw32 7.5版本编译会有错误:
mingw32-make[2]: *** [CMakeFiles\libprotobuf-lite.dir\build.make:63: CMakeFiles/libprotobuf-lite.dir/E_/protobuf-cpp-3.12.0-rc-2/protobuf-3.12.0-rc-2/src/google/protobuf/any_lite.cc.obj] Error 1
mingw32-make[1]: *** [CMakeFiles\Makefile2:78: CMakeFiles/libprotobuf-lite.dir/all] Error 2
mingw32-make: *** [Makefile:129: all] Error 2
于是用它的上一个版本:Protocol Buffers v3.11.4
经测试可以编译过了,于是记录利用Qt中自带的mingw编译Protocol buffer的经历:
第一步:https://github.com/protocolbuffers/protobuf/releases
下载

下载的是cpp版本,all版本应该也可以。
第二步:解压

新建一个build文件夹,这样cmake生成的一些东西就会到这个build文件夹中;新建一个install目录,最后编译生成的头文件和库文件会自动安装到这个目录。
第三步:添加(检查环境变量
)
这一步非常关键

将Qt安装目录中的D:\Qt\Qt5.14.1\Tools\mingw730_64\bin
添加到环境变量中,尽量让它上移动(避免后面的CMake找到其他的编译器)
第四步:cmake编译出工程
选择我们刚才解压的cmake目录,和build生成文件存放目录,看下图:

点击Configure:

如果你没有看到MinGW,就去检查环境变量是否配置了,像我这样(D:\Qt\Qt5.14.1\Tools\mingw730_64\bin)这个目录根据个人的Qt安装路径而定。
选择MingGW Makefiles,点击Finish
编译动态库版本

如上图所示勾选的就是生成动态库版本。
说明:protobuf_WITH_ZLIB默认是勾选的,但是如果我们的电脑没有安装zlib库,只会有提示,不影响最终的库生成。
会报一个这样的错误提示,但是没关系。
Could NOT find ZLIB (missing: ZLIB_LIBRARY ZLIB_INCLUDE_DIR)
把protobuf_BUILD_TESTS勾选去掉,否则非常久,而且还会报下面的错误:
[ 92%] Linking CXX executable tests.exe
CMakeFiles\tests.dir/objects.a(map_test.cc.obj):map_test.cc:(.text+0xc993): undefined reference to `google::protobuf::Reflection::GetMessageA(google::protobuf::Message const&, google::protobuf::FieldDescriptor const*, google::protobuf::MessageFactory*) const'
CMakeFiles\tests.dir/objects.a(map_test.cc.obj):map_test.cc:(.text+0xcf68): undefined reference to `google::protobuf::Reflection::GetMessageA(google::protobuf::Message const&, google::protobuf::FieldDescriptor const*, google::protobuf::MessageFactory*) const'
CMakeFiles\tests.dir/objects.a(map_test.cc.obj):map_test.cc:(.text+0xfc99): undefined reference to `google::protobuf::Reflection::GetMessageA(google::protobuf::Message const&, google::protobuf::FieldDescriptor const*, google::protobuf::MessageFactory*) const'
CMakeFiles\tests.dir/objects.a(map_test.cc.obj):map_test.cc:(.text+0x102e6): undefined reference to `google::protobuf::Reflection::GetMessageA(google::protobuf::Message const&, google::protobuf::FieldDescriptor const*, google::protobuf::MessageFactory*) const'
CMakeFiles\tests.dir/objects.a(map_test.cc.obj):map_test.cc:(.text+0x11986): undefined reference to `google::protobuf::Reflection::GetMessageA(google::protobuf::Message const&, google::protobuf::FieldDescriptor const*, google::protobuf::MessageFactory*) const'
CMakeFiles\tests.dir/objects.a(map_test.cc.obj):map_test.cc:(.text+0x1355d): more undefined references to `google::protobuf::Reflection::GetMessageA(google::protobuf::Message const&, google::protobuf::FieldDescriptor const*, google::protobuf::MessageFactory*) const' follow
collect2.exe: error: ld returned 1 exit status
mingw32-make[2]: *** [CMakeFiles\tests.dir\build.make:2308: tests.exe] Error 1
mingw32-make[1]: *** [CMakeFiles\Makefile2:282: CMakeFiles/tests.dir/all] Error 2
mingw32-make: *** [Makefile:129: all] Error 2
点击Generate。
如果有Could NOT find ZLIB (missing: ZLIB_LIBRARY ZLIB_INCLUDE_DIR) 的提示,忽略即可,继续下一步。

看到这里的两个done提示,就是生成好后面给mingw用的工程。
第五步:命令行中编译和安装protobuf

说明:如果以普通身份运行,后面可以正常编译,但是比如对需要管理员权限的目录,不能自动安装protobuf,需要手动拷贝;非管理员权限目录则不需要,直接双击打开就行。
切换到build目录,输入mingw32-make

等待…

看到100%,就是编译成功了(如果报错了,就回头检查CMake中设置)

编译完成后输入:mingw32-make install
如果报没有权限,就第五步右键管理员打开,重新来输入这个命令,就有权限了。

安装到这个目录E:\protobuf-cpp-3.11.4\install,这个是个普通目录,不需要管理员权限
如果刚才在cmake中没有选目录,就是默认安装到C:\Program Files (x86)\protobuf,这个目录是需要管理员权限的
第六步:测试
编译出来的库还有经过最后一步检查,能否跑起来。
新建一个Qt工程,选择MinGW 54-bi编译器


新建的工程为protobufDemo
在其工程目录中新建一个文件夹SharedProtobuf
将上一步中生成的bin,include和lib拷贝进来(这些文件夹中的cmake可以删掉,我这里就不演示了)

新建一个SharedProtobuf.pri,其内容为:
INCLUDEPATH += $$PWD/include
LIBS += -L$$PWD/bin
LIBS += -L$$PWD/lib -lprotobuf
说明:LIBS += -L$$PWD/bin
这个是为了在Qt Creator中运行程序时,不需要拷贝libprotobuf.dll到编译目录中而添加的,
打包的时候,把libprotobuf.dll拷贝到和exe同层级目录中。
在bin目录中添加person.proto
syntax = "proto2";
package tutorial;
message Person {
required int32 id = 1;
required string name = 2;
optional string email = 3;
}
在bin目录中,运行批处理命令:
protoc.exe --cpp_out=./ person.proto
这里我们为了更好的管理生成文件,特意写了个简单的批处理命令:
create.bat
set DST_DIR=gen
if not exist %DST_DIR% (
mkdir %DST_DIR%
cd %DST_DIR%
if not exist cpp (
mkdir cpp
)
)
protoc.exe -I=./ --cpp_out=%DST_DIR%/cpp/ ./*.proto
pause

如果文件夹gen不存在则创建,然后再gen下创建一个cpp文件夹,里面就是点击该批处理命令后生成的

别忘记将这两个文件添加到我们的Qt工程中。
在protobufDemo.pro文件中添加:
include(./SharedProtobuf/SharedProtobuf.pri)
这样就将我们前面编译的protobuf作为模块添加到Qt工程中了。
在widget.cpp的构造函数中测试:
#include "widget.h"
#include "ui_widget.h"
#include "pb/person.pb.h"
#include <QDebug>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
tutorial::Person person;
//序列化
person.set_id(123456);
person.set_name("Mark");
person.set_email("mark@example.com");
std::string stdContent;
person.SerializeToString(&stdContent);
// QString content = QString::fromStdString(stdContent); //这样std::string就可以转成QString了
//反序列化
tutorial::Person otherPerson;
otherPerson.ParseFromString(stdContent);
qDebug() << "otherPerson id:" << otherPerson.id();
qDebug() << "otherPerson name:" << otherPerson.name().c_str();
qDebug() << "otherPerson email:" << otherPerson.email().c_str();
}
Widget::~Widget()
{
delete ui;
}
我将测试的工程和代码放到了这里,大家可以下载下来看:
链接:https://pan.baidu.com/s/1U4Pdqmh20WPoqbzOMs_guw
提取码:jbdm
959



被折叠的 条评论
为什么被折叠?



