@import url(http://m.shnenglu.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
下載地址:https://github.com/google/protobuf
1.編譯
通過cmake生成sln文件來編譯
用cmd命令 cd 到3.0.0-beta-4\cmake
1
2
3
|
mkdir build & cd build
mkdir solutionx86 & cd solutionx86
cmake -G "Visual Studio 14 2015" -DCMAKE_INSTALL_PREFIX=../../../../install ../.. -Dprotobuf_BUILD_TESTS=OFF -Dprotobuf_BUILD_SHARED_LIBS=ON -DCMAKE_GENERATOR_TOOLSET=v140_xp
|
如果要編譯64位的話
1
|
cmake -G "Visual Studio 14 2015 Win64" -DCMAKE_INSTALL_PREFIX=../../../../install ../.. -Dprotobuf_BUILD_TESTS=OFF -Dprotobuf_BUILD_SHARED_LIBS=ON -DCMAKE_GENERATOR_TOOLSET=v140_xp
|
注意中間不能有回車之類的,該命令行生成的是dll版本的(靜態庫的話要注意lib的運行庫),其它版本請查考cmake目錄下面的README.md
2.hello world
將examples目錄下滿的addressbook.proto,拷貝到生成的protoc.exe目錄下面,cd到該目錄
protoc --cpp_out=./ addressbook.proto
這樣就會生成addressbook.pb.cc , addressbook.pb.h 兩個文件,包含到你的項目中
addressbook.pb.cc,需要取消預編譯頭或者手動加入#include "stdafx.h"
然后包含頭文件和lib文件
直接用examples目錄下面的 add_person.cc里面的代碼測試即可
注意:使用dll的時候需要定義宏PROTOBUF_USE_DLLS,小心不要將這個PROTOBUF_USE_DLLS定義在stdafx.h里面,然后你又取消了預編譯頭!
|
@import url(http://m.shnenglu.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
需要工具:
1. visual studio 2015
2. cmake
3.Git[可選]
安裝完畢,繼續
從官網下載protobuf :https://github.com/google/protobuf/releases/tag/v3.0.0
![]()
window編譯protobuf有兩種方式:一個使用vs自帶的msvc(環境測試vs開發者命令行工具),一種是用Cygwin 或者 MinGW。
此處使用msvc 進行編譯。
使用vs開發者命令行工具,先測試cmake是否可用。
![]()
打開上圖第一項,測試:
![]()
ok,接下來,解壓protobuf壓縮包,在和protobuf同級目錄下新建一個install文件夾,用作編譯完成后方include ,lib等文件。
如:
進入protobuf目錄,創建build目錄
- C:\Path\to\protobuf\cmake>mkdir build & cd build
- C:\Path\to\protobuf\cmake\build>
以下注意^和命令之間的空格
創建release版本的編譯目錄:
- C:\Path\to\protobuf\cmake\build>mkdir release & cd release
- C:\Path\to\protobuf\cmake\build\release>cmake -G "NMake Makefiles" ^
- -DCMAKE_BUILD_TYPE=Release ^
- -DCMAKE_INSTALL_PREFIX=../../../../install ^
- ../..
創建debug版本的編譯目錄:
- C:\Path\to\protobuf\cmake\build>mkdir debug & cd debug
- C:\Path\to\protobuf\cmake\build\debug>cmake -G "NMake Makefiles" ^
- -DCMAKE_BUILD_TYPE=Debug ^
- -DCMAKE_INSTALL_PREFIX=../../../../install ^
- ../..
創建生成visual stuido 工程的文件夾:
這一步需要注意的是,
- "Visual Studio 14 2015 Win64"
- 是因為安裝了visual studio 2015而決定的,這是所謂的generator,不同編譯器是不同的,具體類型可見:<span style="white-space:pre"> http:
- C:\Path\to\protobuf\cmake\build>mkdir solution & cd solution
- C:\Path\to\protobuf\cmake\build\solution>cmake -G "Visual Studio 14 2015 Win64" ^
- -DCMAKE_INSTALL_PREFIX=../../../../install ^
- ../..
注意:這里用了Win64 那么生成的就是64位庫,想要32位庫就去掉Win64
以上3種不是必須都做。
通過以上3個步驟,可以見到在不同目錄都生成了相應的makefile文件,接下來是執行nmake進行編譯:
- To compile protobuf:
-
- C:\Path\to\protobuf\cmake\build\release>nmake
-
- or
-
- C:\Path\to\protobuf\cmake\build\debug>nmake
以下安裝頭文件、庫文件等安裝到之前制定的文件(install):
- To install protobuf to the specified *install* folder:
-
- C:\Path\to\protobuf\cmake\build\release>nmake install
-
- or
-
- C:\Path\to\protobuf\cmake\build\debug>nmake install
到此,release 和 debug版本都編譯成功,vs可以使用了。
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
vs平臺開發用sln生成庫要注意兩點:
第一:
solution目錄下有生成sln文件,可以用vs打開生成庫,但要注意位數,比如如果
vs + qt (32位庫)
那么,protobuf也應該改成win32平臺,講所有項目都改成win32平臺的,
![]()
![]()


不然會出現: fatal error LNK1112: module machine type 'X86' conflicts with target machine type 'x64'
另外,32位編譯器可以編譯32和64位程序,這里選用win32
第二:
編譯的庫要和你使用的版本一致,如release debug,在下圖的位置確定;
![]()

MD代表動態鏈接,MT代表靜態鏈接,后面沒有有d代表是release模式-》![]()

release模式則用前兩種。
同時,如果你在項目用到其他庫,比如vs + qt (32位動態鏈接庫),那么protobuf也應該用動態鏈接方式生成lib,不然提示:
值“MT_StaticRelease”不匹配值“MD_DynamicRelease”
等這類錯誤。
動態鏈接方式:MD
靜態鏈接方式:MT
總之,qt 庫鏈接方式要和protobuf的一致,如果還用到其他的,全部都要一致。
cmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../../../../install ../.. -Dprotobuf_BUILD_TESTS=OFF
cmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=../../../../installDebug ../.. -Dprotobuf_BUILD_TESTS=OFF
cmake -G "Visual Studio 14 2015" -DCMAKE_INSTALL_PREFIX=../../../../install ../.. -Dprotobuf_BUILD_TESTS=OFF -Dprotobuf_BUILD_SHARED_LIBS=ON -DCMAKE_GENERATOR_TOOLSET=v140_xp
cmake -G "Visual Studio 14 2015 Win64" -DCMAKE_INSTALL_PREFIX=../../../../install ../..
cmake -G "Visual Studio 14 2015" -DCMAKE_INSTALL_PREFIX=../../../../install ../.. -Dprotobuf_BUILD_TESTS=OFF
posted on 2017-08-02 23:17
聶文龍 閱讀(1725)
評論(0) 編輯 收藏 引用