編譯環境: win10 x64 編譯器 :mingw32 cmake 使用場景:Qt4.8.7
下載 protobuf 最新的代碼:https://github.com/google/protobuf
點擊 configure
使用默認的 mingw32 即可,前提是把 mingw32/bin 加到環境變量Path里面。
然后,發現出錯了 gmock 沒有 ,可以直接勾選取消 gmock 也可以GitHub 自己下載 mock 補充到源碼 (我這直接不生成測試了)
然后在自己指定的文件夾中生成了如下文件
開始編譯 (mingw32-make )是 mingw32/bin 下的exe (此目錄已在環境變量Path中了)等待完成即可。
成果就是它了
用 .\protoc.exe --proto_path=SRC --cpp_out=DST SRC/test.proto 生成文件(自己建立 src 和dst 文件夾 test.proto 放到src文件下)

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;
配置一下 頭文件和庫文件
最后就是結果了,收工。