编译环境: win10 x64 编译器 :mingw32 cmake 使用场景:Qt4.8.7ios
下载 protobuf 最新的代码:https://github.com/google/protobuf git
点击 configure github
使用默认的 mingw32 便可,前提是把 mingw32/bin 加到环境变量Path里面。ide
而后,发现出错了 gmock 没有 ,能够直接勾选取消 gmock 也能够GitHub 本身下载 mock 补充到源码 (我这直接不生成测试了)测试
而后在本身指定的文件夹中生成了以下文件google
开始编译 (mingw32-make )是 mingw32/bin 下的exe (此目录已在环境变量Path中了)等待完成便可。spa
成果就是它了code
用 .\protoc.exe --proto_path=SRC --cpp_out=DST SRC/test.proto 生成文件(本身创建 src 和dst 文件夹 test.proto 放到src文件下)blog
1 // 指定语法规则 2 syntax = "proto3"; 3 4 message Book 5 { 6 string name = 1; 7 int32 pages = 2; 8 float price = 3; 9 } 10 11 message Student 12 { 13 int32 age = 1; 14 string name = 2; 15 float score = 3; 16 repeated Book arrBook = 4; 17 }
生成 test.pb.h 和 test.pb.cc 创建一个Qt项目测试一下吧编译器
1 void MainWindow::on_pushButton_clicked() 2 { 3 //GOOGLE_PROTOBUF_VERIFY_VERSION; 4 Book book; 5 Book book2; 6 book.set_name("哈哈shishi"); 7 book.set_pages(32); 8 book.set_price(2.63); 9 //qDebug()<< book.IsInitialized(); 10 string str ; 11 book.SerializePartialToString(&str); 12 book2.ParseFromString(str); 13 qDebug()<<QString::fromUtf8(book2.name().c_str())<< " "<<book2.price()<<" "<<book2.pages(); 14 }
包含生成的头文件
1 #include "test.pb.h" 2 #include <QDebug> 3 #include <iostream> 4 using namespace std;
配置一下 头文件和库文件
最后就是结果了,收工。