[ protobuf c++ ] protobuf + vs2013編譯安裝

1:下載:

protobuf 2.5.0

壓縮文件中包含 protobuf-2.5.0.tar.gz 和 protoc-2.5.0-win32.zip,protobuf-2.5.0.tar.gz是protobuf的源文件,protoc-2.5.0-win32.zip是一個protoc.exe應用文件,用於把傳輸類編譯成.h和.cc文件。把 protoc-2.5.0-win32.zip 解壓後的目錄添加到系統的環境變量中。打開cmd,輸入protoc -h 有相應的提示,表示成功。

 

2:編譯

(1):打開protobuf-2.5.0的解壓文件的vsprojects中工程文件protobuf.sln,編譯:libprotobuf 有錯誤:

1>..\src\google\protobuf\io\zero_copy_stream_impl_lite.cc(121): error C3861: “min”:  找不到標識符
1>..\src\google\protobuf\io\zero_copy_stream_impl_lite.cc(168): error C3861: “max”:  找不到標識符
1>..\src\google\protobuf\io\zero_copy_stream_impl_lite.cc(195): error C3861: “min”:  找不到標識符

則在文件中添加:#include <algorithm>頭文件

(2):修改vs2013的編譯強制copy檢查,若不修改,編譯對象文件的時候,會報_Copy_Imp的錯誤,修改libprotobuf工程下的repeated_field.h文件,修改如下:

namespace internal {
template <typename Element, bool HasTrivialCopy>
void ElementCopier<Element, HasTrivialCopy>::operator()(
    Element to[], const Element from[], int array_size) {
std::copy(from, from + array_size, stdext::checked_array_iterator<Element*>(to, array_size));
  //std::copy(from, from + array_size, to);
}

(3):最後一個個編譯工程,生成libprotobuf.lib,libprotobuf-lite.lib,libprotoc.lib,lite-test.exe,protoc.exe,test_plugin.exe,tests.exe

 

3:添加測試工程文件:

新建控制檯文件testprotobuf.sln工程

新建protobuf_lib文件夾,把上面編譯的lib文件copy到這個文件下。

新建protobuf_src文件夾,把protobuf-2.5.0的源文件下的src文件夾下的所有文件copy到此文件夾下。

新建Person.proto文件,內容如下:

  1. package Test;  
  2. message Person   
  3. {  
  4.    required string name = 1;  
  5.    required int32 id = 2;  
  6.    optional string email = 3;  
  7. }  

打開cmd,輸入命令:

protoc -I=E:\work\testProtobuf --cpp_out=E:\work\testProtobuf E:\work\testProtobuf\Person.proto

把相應的目錄換成.proto文件所在的目錄即可,完成後會生成兩個文件:Person.pb.h,Person.pb.cc,然後添加到工程中。

 

4:添加源文件的頭文件。

添加lib,【屬性】-【鏈接器】-【輸入】-【附加依賴項】把上面的三個lib添加進來

在包含lib所在的庫文件:【屬性】-【鏈接器】-【常規】-【附加庫本目錄】:$(SolutionDir)\protobuf_lib

添加測試源碼:

  1. #include "stdafx.h"  
  2. #include "..\Person.pb.h"  
  3. #include <iostream>  
  4. #include <fstream>  
  5.   
  6.   
  7. int _tmain(int argc, _TCHAR* argv[])  
  8. {  
  9.     GOOGLE_PROTOBUF_VERIFY_VERSION;  
  10.   
  11.     Test::Person person;  
  12.     person.set_id(123);  
  13.     person.set_name("abc");  
  14.     person.set_email("[email protected]");  
  15.   
  16.     std::cout << "Before:" << std::endl;  
  17.     std::cout << "ID:" << person.id() << std::endl;  
  18.     std::cout << "name:" << person.name() << std::endl;  
  19.     std::cout << "email:" << person.email() << std::endl;  
  20.   
  21.     std::string str;  
  22.     person.SerializeToString(&str);  
  23.   
  24.     Test::Person person2;  
  25.     person2.ParseFromString(str);  
  26.   
  27.     std::cout << "After:" << std::endl;  
  28.     std::cout << "ID:" << person2.id() << std::endl;  
  29.     std::cout << "Name:" << person2.name() << std::endl;  
  30.     std::cout << "Email:" << person2.email() << std::endl;  
  31.   
  32.   
  33.     system("pause");  
  34.   
  35.     return 0;  
  36. }  

結果如下:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章