windows下编译google-protocolbuf在Qt中使用

2021-04-13 17:27

阅读:562

标签:一个   fst   get   tput   资源下载   hub   read   根据   工程   

1.首先从Github-Protobuf下载代码,本文下载的版本号是3.1.0.

技术分享图片

2.仔细查看各个README,有相关的资源下载和编译说明.

3.在一个方便的地方创建一个Install类型的文件夹,放置Cmake生成的工程文件相关内容,使用CMake-gui配置,生成visual studio ide工程.

技术分享图片

CMAKE_CONFIGRATION_TYPES是工程配置类型,可以删除不感兴趣的配置.

CMAKE_INSTALL_PREFIX是导出visual studio ide项目文件的位置

根据自己的需求选择BUILD_SHARED_LIBS或者是MSVC_STATIC_RUNTIME(对应编译选项/Mtd和/Mt),二者选其一

技术分享图片

4.Open Project直接编译工程.


将生成的protobuf的库引用项目,报如下错误:

1 error LNK2001: 无法解析的外部符号 "class google::protobuf::internal::ExplicitlyConstructed,class std::allocator > > google::protobuf::internal::fixed_address_empty_string" (?fixed_address_empty_string@internal@protobuf@google@@3V?$ExplicitlyConstructed@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@123@A)
2 error LNK2001: 无法解析的外部符号 "class google::protobuf::internal::ExplicitlyConstructed,class std::allocator > > google::protobuf::internal::fixed_address_empty_string" (?fixed_address_empty_string@internal@protobuf@google@@3V?$ExplicitlyConstructed@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@123@A)
3 error LNK2001: 无法解析的外部符号 "int google::protobuf::internal::empty_string_once_init_" (?empty_string_once_init_@internal@protobuf@google@@3HA)

需要在工程中添加预处理PROTOBUF_USE_DLLS

技术分享图片


 

Protos:

技术分享图片技术分享图片
 1 syntax = "proto3";
 2 
 3 message CoinMsg
 4 {
 5 
 6 }
 7 
 8 syntax = "proto3";
 9 
10 message ExecMsg
11 {
12     string name = 1;    
13 }
14 
15 syntax = "proto3";
16 
17 message VerifyMsg
18 {
19     bool isOk = 1;
20     string error = 2;
21 }
22 
23 syntax = "proto3";
24 
25 import "ExecMsg.proto";
26 import "CoinMsg.proto";
27 import "VerifyMsg.proto";
28 
29 message MsgPolicy
30 {
31     enum Type
32     {
33         ExecMsg = 0;
34         CoinMsg = 1;
35         VerifyMsg = 2;
36     }
37     Type type = 1;
38     ExecMsg execMsg = 2;
39     CoinMsg coinMsg = 3;
40     VerifyMsg verifyMsg = 4;
41 }
View Code

  每个Message对应一个proto文件  

// 一次生成完cpp与csharp代码,注protobuf-version-3.1.0
protoc -I ./*.proto --cpp_out=../cpp --csharp_out=../csharp

  

使用示例:

 1 #include "ConfigHelper.h"
 2 #include  3 #include  4 #include  5 #include  6 #include  7 #include string>
 8 #include "MsgPolicy.pb.h"
 9 #include 10 #include 11 #include 12 
13 ConfigHelper * ConfigHelper::instance = new ConfigHelper();
14 
15 ConfigHelper::ConfigHelper()
16 {
17 #pragma region standard c++ io
18     {
19         // 序列化到文件
20         MsgPolicy msgPolicy;
21         ExecMsg *execMsg = new ExecMsg();
22         execMsg->set_name("exec message name.");
23         msgPolicy.set_allocated_execmsg(execMsg);
24         msgPolicy.set_type(MsgPolicy::ExecMsg);
25         //QFile file("msg.bin");
26         //file.open(QIODevice::WriteOnly);
27         std::fstream out("msg.bin", std::ios::out | std::ios::binary | std::ios::trunc);
28         msgPolicy.SerializeToOstream(&out);
29         out.close();
30 
31         // 从文件反序列化到对象
32         MsgPolicy dmsgPolicy;
33         std::fstream in("msg.bin", std::ios::in | std::ios::binary);
34         if (!dmsgPolicy.ParseFromIstream(&in))
35         {
36             qDebug() "deserialize data error.";
37             return;
38         }
39         if (dmsgPolicy.type() == MsgPolicy_Type::MsgPolicy_Type_CoinMsg);
40         else if (dmsgPolicy.type() == MsgPolicy_Type::MsgPolicy_Type_ExecMsg)
41         {
42             qDebug() "policy message type = " "MsgPolicy_Type::MsgPolicy_Type_ExecMsg";
43             qDebug() "execMsg name = "  QString::fromStdString(dmsgPolicy.execmsg().name());
44         }
45         else if (dmsgPolicy.type() == MsgPolicy_Type::MsgPolicy_Type_VerifyMsg);
46         in.close();
47     }
48 #pragma endregion standard c++ io
49 
50 #pragma region protobuf codedstream
51     {
52         // 序列化
53         MsgPolicy msgPolicy5;
54         VerifyMsg *verifyMsg = new VerifyMsg();
55         verifyMsg->set_isok(false);
56         verifyMsg->set_error("the password is invalid.");
57         msgPolicy5.set_allocated_verifymsg(verifyMsg);
58         msgPolicy5.set_type(MsgPolicy_Type::MsgPolicy_Type_VerifyMsg);
59         int len = msgPolicy5.ByteSize() + 4;
60         char *buffer = new char[len];
61         google::protobuf::io::ArrayOutputStream arrayOut(buffer, len);
62         google::protobuf::io::CodedOutputStream codedOut(&arrayOut);
63         codedOut.WriteVarint32(msgPolicy5.ByteSize());
64         if (!msgPolicy5.SerializeToCodedStream(&codedOut)) 
65         {
66             qDebug() "serialize error.";
67         }
68         delete buffer;
69 
70         // 序列化
71         len = msgPolicy5.ByteSize();
72         buffer = new char[len];
73         if (!msgPolicy5.SerializeToArray(buffer, len)) qDebug() "serialize error.";
74 
75         // 反序列化
76         MsgPolicy msgPolicy6;
77         msgPolicy6.ParseFromArray(buffer, len);
78         if (msgPolicy6.type() == MsgPolicy_Type::MsgPolicy_Type_CoinMsg);
79         else if (msgPolicy6.type() == MsgPolicy_Type::MsgPolicy_Type_ExecMsg);
80         else if (msgPolicy6.type() == MsgPolicy_Type::MsgPolicy_Type_VerifyMsg) 
81         {
82             qDebug() "policy message type = " "MsgPolicy_Type::MsgPolicy_Type_VerifyMsg";
83             qDebug() "isOk = " "error = "  QString::fromStdString(msgPolicy6.verifymsg().error());
84         }
85         delete buffer;
86     }
87 #pragma endregion protobuf codedstream
88     google::protobuf::ShutdownProtobufLibrary();
89 }
// 输出结果
policy message type =  MsgPolicy_Type::MsgPolicy_Type_ExecMsg
execMsg name =  "exec message name."
policy message type =  MsgPolicy_Type::MsgPolicy_Type_VerifyMsg
isOk =  false error =  "the password is invalid."

 

windows下编译google-protocolbuf在Qt中使用

标签:一个   fst   get   tput   资源下载   hub   read   根据   工程   

原文地址:https://www.cnblogs.com/linxmouse/p/8975475.html


评论


亲,登录后才可以留言!