protobuf是谷歌開發並開源的二進制序列化工具,比常用的xml,json體積更小,解析速度更快。
目前,手頭上的項目有涉及到這個工具,打算仔細研究一番,順帶記錄遇到的問題,解決方法,時間足夠的話,深入探究下源碼。
今天主要記錄下怎么下載源碼,怎么成功編譯。本人的平台是windows,編譯器用的是vs2010。
源碼下載:https://github.com/google/protobuf.git
本人下載到本地目錄為:E:\code\protobufstudy\github

下載完成后的目錄結構如上。
怎么編譯的話里面有份文檔解釋的非常清楚

考慮到有些人不喜歡看英文,我再把我的編譯過程展示下。
一:前期准備:CMake工具,可以去官網下載二進制版本的。https://cmake.org/download/

安裝到本地后,如 C:\Program Files\CMake\bin ,然后打開命令行設置下環境變量E:\code\protobufstudy\github>set PATH=%PATH%;C:\Program Files (x86)\CMake\bin
(ps:命令行最好切換到下載的源碼目錄下,這樣與本人講述的一致)
這時就能使用cmake命令了,否則會說cmake不是內部命令什么的。
然后進入源碼目錄,E:\code\protobufstudy\github>cd trunk
E:\code\protobufstudy\github\trunk>
進入源碼下面的cmake目錄,E:\code\protobufstudy\github\trunk>cd cmake
E:\code\protobufstudy\github\trunk\cmake>
創建build目錄,E:\code\protobufstudy\github\trunk\cmake>mkdir build & cd build
E:\code\protobufstudy\github\trunk\cmake\build>
創建solution目錄, E:\code\protobufstudy\github\trunk\cmake\build>mkdir solution & cd solution
E:\code\protobufstudy\github\trunk\cmake\build\solution>
執行cmake指令,cmake -G "Visual Studio 10 2010 Win64" ^
-DCMAKE_INSTALL_PREFIX=../../../../install ^
../.. (暫且叫cmake指令1)
如果會報錯誤,找不到cl.exe,請設置下cl.exe的環境變量
E:\code\protobufstudy\github\trunk\cmake\build\solution>set PATH=%PATH%;C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\bin (這個是你本地的cl.exe所在目錄)
再次執行cmake指令1,如果還報錯誤,找不到mspdb100.dll,請設置下mspdb100.dll的環境變量
E:\code\protobufstudy\github\trunk\cmake\build\solution>set PATH=%PATH%;D:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE (這個是你本地的mspdb100.dll所在目錄)
再次執行cmake指令1,如果還報錯誤,沒有找到gmock目錄,要么下載下gmock,https://github.com/google/googlemock.git 目錄為E:\code\protobufstudy\github\trunk\gmock,
本人懶,沒有下載,那么久更改下cmake執行。
執行cmake指令,cmake -G "Visual Studio 10 2010 Win64" ^
-Dprotobuf_BUILD_TESTS=OFF ^
-DCMAKE_INSTALL_PREFIX=../../../../install ^
../.. (暫且叫cmake指令2)
這時在solution目錄下就會有相應的.sln了。

打開編譯即可,本人編譯的release,編譯的成果

