上一节: windows下vs2017编译protobuf 3.6.1: https://blog.csdn.net/shan165310175/article/details/86624119
1. 新建立console空工程。(这里使用debug编译方式示例)
2. 拷贝protoc.exe到工程目录
3.工程目录新建立helloworld.proto文件,内容:
syntax="proto3";
package hello;
enum Type{
UNKNOWN=0;
BOOK1=1;
BOOK2=2;
}
message User{
int32 id=1;
string name=2;
}
message Book{
int32 id=1;
string name=2;
repeated int32 list=3;
Type type=4;
User user1=5;
repeated User userList=6;
}
4.使用命令生成文件,并将文件加入工程:protoc.exe helloworld.proto --cpp_out=./
5. 配置工程中的头文件目录与lib依赖:
libprotobufd.lib
libprotobuf-lited.lib
libprotocd.lib
6. 增加main.cpp:
#include "helloworld.pb.h"
using namespace std;
int main(int argc, char **argv) {
hello::Book book;
book.set_id(100);
book.set_name("jack");
book.add_list(11);
book.add_list(21);
book.set_type(hello::Type::BOOK1);
hello::User *user=book.add_userlist();
user->set_id(1);
user->set_name("Mr X");
user = book.add_userlist();
user->set_id(2);
user->set_name("Mr Y");
book.PrintDebugString();
return 0;
}
7. 编译执行:
附加: 这里有个protobuf编译错误:
https://blog.csdn.net/shan165310175/article/details/86626013