一键编译Protobuf

Protobuf是一种数据交换的格式,相比XML和JSON都会高效,安全。现在游戏开发也大都采用Protobuf。那么对其编译就必不可少的步骤。

所需要的工具:cmake和git

1.下载源码 download_protobuf.bat文件,运行

::参考文章 https://github.com/google/protobuf/blob/master/cmake/README.md
::设置所需要的Protobuf版本,可以在github上查到 https://github.com/google/protobuf
set PROTOBUF_VESION="2.7.0"
echo %PROTOBUF_VESION%
set PROTOBUF_PATH="protobuf_%PROTOBUF_VESION%"
echo %PROTOBUF_PATH%

::从githug上拉取protobuf源代码
git clone -b %PROTOBUF_VESION% https://github.com/google/protobuf.git %PROTOBUF_PATH%

::从github上拉取gmock
cd %PROTOBUF_PATH%
git clone -b release-1.7.0 https://github.com/google/googlemock.git gmock

::从github上拉取gtest
cd gmock
git clone -b release-1.7.0 https://github.com/google/googletest.git gtest
pause

2.编译
你可以利用cmake构建你所需要的版本,下面的的例子是构建并编译一个VS2015版本的protobuf

例:构建VS2015版本

复制以下代码,保存到 build_VS.bat 文件,放到 download_protobuf.bat 同级目录,然后执行

::参考文章 https://github.com/google/protobuf/blob/master/cmake/README.md
::默认当前操作系统已安装 git 和 cmake,并配置好了环境变量
echo off & color 0A

::设置所需要的Protobuf版本,最新版本可以在github上查到 https://github.com/google/protobuf
::必须与下载的版本一致
set PROTOBUF_VESION="2.7.0"
echo %PROTOBUF_VESION%
set PROTOBUF_PATH="protobuf_%PROTOBUF_VESION%"
echo %PROTOBUF_PATH%
cd %PROTOBUF_PATH%

::设置VS工具集,相当于指定VS版本,取决于VS的安装路径
set VS_DEV_CMD="E:\Soft\Microsoft Visual Studio 14.0\Common7\Tools\VsDevCmd.bat"
::设置工程文件夹名字,用来区分不同的VS版本
set BUILD_PATH="build_VS2015"
::设置编译版本 Debug Or Release
set MODE="Release"

cd cmake
if not exist %BUILD_PATH% md %BUILD_PATH%

cd %BUILD_PATH%
if not exist %MODE% md %MODE%
cd %MODE%

::开始构建和编译
call %VS_DEV_CMD%
cmake ../../ -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=%MODE%
call extract_includes.bat
nmake /f Makefile

echo %cd%
pause

此时,所有的东西都已经生成,包括头文件 和 lib文件
zzzz