win7 vs2015編譯protobuf-3.1.0
1、安裝vs2015
2、 安裝cmake https://cmake.org/download/
3、下載protobuff 3.1.0 https://github.com/google/protobuf/releases/
解壓protobuf壓縮包,在和protobuf同級目錄下新建一個install資料夾,用作編譯完成後方include ,lib等檔案。
如:
- C:\Path\to\install
從VS開發人員命令列工具進入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 ../..
-
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 Studio 14 2015 Win64"
-
是因為安裝了visual studio 2015而決定的,這是所謂的generator,不同編譯器是不同的,具體型別可見:<span style="white-space:pre"
- 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 ../..
以上3種不是必須都做。
通過以上3個步驟,可以見到在不同目錄都生成了相應的makefile檔案,接下來是執行nmake進行編譯:
- To compile protobuf:
- C:\Path\to\protobuf\cmake\build\release>nmake
- or
- C:\Path\to\protobuf\cmake\build\debug>nmake
- 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,不然提示:
等這類錯誤。
動態連結方式:MD
靜態連結方式:MT
總之,qt 庫連結方式要和protobuf的一致,如果還用到其他的,全部都要一致。