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::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > 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::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > 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 }