GitHub:下载地址:https://github.com/google/protobuf
protobuf 编译
https://blog.csdn.net/u012020854/article/details/104514865
如何构造一个简单的.proto
其中 repeated 类似于list ,但是它没有提供删除函数。。。
如何把.proto 生成.h/.cc文件
如果你看完上面的内容就开始敲命令那么你将会很懊恼:
1.新建.txt文件,复制一下数据,改文件名为 protoc.bat
2.把该文件与 protoc.exe放在同一目录下
3.把写好的 .proto文件拷贝到 protoc.exe目录下
4.双击protoc.bat运行(如果没有生成.h,.cc 那么就是你的.proto写错了,请敲上面介绍的的命令行方法,它会提示错误)
@echo off
for /f "delims=" %%a in ('dir /b/a-d/oN *.proto') do (
start protoc.exe --cpp_out=./ %%a
)
msvc 编译器使用时,需要导出宏、
@echo off
for /f "delims=" %%a in ('dir /b/a-d/oN *.proto') do (
start protoc.exe --cpp_out=dllexport_decl=YHMSVCDLL_EXPORT:./ %%a
)
如何在.pro文件中添加 protobuf 库
注:如果在.pri,pro等文件中看到【$$PWD】表示.pri/.pro文件所在的路径
$$PWD/third_party_lib/lib/mingw_x86 =>.pro同路径下third_party_lib文件夹
CONFIG(debug,debug|release){#debug
macx:{
# macOS only
LIBS += -L"$$PWD/third_party_lib/lib/macOS" \
-lprotobufd
}
linux-g++:{
# linux only
LIBS += -L"$$PWD/third_party_lib/lib/linux_x64" \
-lprotobufd
}
win32:{
# windows only
LIBS += -L"$$PWD/third_party_lib/lib/mingw_x86" \
-lprotobufd
}
}
CONFIG(release,debug|release){#release
macx:{
# macOS only
LIBS += -L"$$PWD/third_party_lib/lib/macOS" \
-lprotobuf
}
linux-g++:{
# linux only
LIBS += -L"$$PWD/third_party_lib/lib/linux_x64" \
-lprotobuf
}
win32:{
# windows only
LIBS += -L"$$PWD/third_party_lib/lib/mingw_x86" \
-lprotobuf
}
}
如何在代码中使用
//.h
#include "Messages/message_map.pb.h"
Message_MapLog _map_log_;
//.cpp
#include "google/protobuf/util/json_util.h"
#include "google/protobuf/util/type_resolver.h"
#include "google/protobuf/util/type_resolver_util.h"
//注册MapData信号槽可用
qRegisterMetaType<Message_MapLogData*>("Message_MapLogData*");
//读取离线文件数据
bool GThreadDrawMap::readOffLineMap(const QString&filename)
{
_map_log_.Clear();
QFile file(filename);
bool is_ok = file.open(QIODevice::ReadOnly);
if (is_ok) {
QByteArray b = file.readAll();
//--------------------------------
google::protobuf::util::TypeResolver* type_resolver =
google::protobuf::util::
NewTypeResolverForDescriptorPool("",
google::protobuf::DescriptorPool::generated_pool());
std::string str;
std::string str_url = "/";
str_url.append("rbk.protocol.Message_MapLog");
google::protobuf::util::JsonToBinaryString(type_resolver, str_url,b.toStdString(), &str);
_map_log_.ParseFromString(str);
//-----------------------------------
if (""!=str)
{
is_ok = _map_log_.ParseFromString(str);
if(is_ok){
}else{
qDebug()<<"map_data_.ParseFromString: not ok";
}
}else{
qDebug()<<"google::protobuf::util::JsonToBinaryString is empty";
PublicClass::init()->showMessage(tr("这是一个空的地图文件"));
}
file.close();
}else{
PublicClass::init()->showMessage(tr("打开地图文件失败(可能是文件被占用)."));
}
return is_ok;//Message_MapLog*;
}
//保存
ofstream ofs(path.toStdString(),ios::out|ios::binary);
if (ofs) {
google::protobuf::util::TypeResolver* type_resolver = google::protobuf::util::NewTypeResolverForDescriptorPool("",google::protobuf::DescriptorPool::generated_pool());
google::protobuf::util::JsonOptions json_options;
std::string json_buffer;
std::string str_url = "/";
str_url.append("rbk.protocol.Message_Map");
google::protobuf::util::BinaryToJsonString(type_resolver,str_url,current_map_data_.SerializeAsString(),&json_buffer,json_options);
ofs<<json_buffer;
ofs.flush();
ofs.close();
}