文件版本说明
版本 | 颁布日期 | 修订章节 | 作者 |
---|---|---|---|
0.1 | 2017.09.23 | 撰写草稿 | 钟鑫 |
0.2 | 2017.09.24 | 添加Protobuf编译与使用 | 钟鑫 |
0.3 | 2017.12.06 | 添加windows下编译Protobuf | 钟鑫 |
0.4 | 2017.12.13 | 整理编译静态库动态库 | 钟鑫 |
0.5 | 2017.12.16 | 添加protobuf语法 | 钟鑫 |
0.6 | 2017.12.17 | 添加protobuf发送和接收类型 | 钟鑫 |
0.8 | 2017.12.21 | 整理protobuf基类 | 钟鑫 |
0.9 | 2017.12.23 | 整理测试源码 | 钟鑫 |
1.0 | 2017.12.25 | 整理protobuf基类测试源码 | 钟鑫 |
下载代码
protobuf为开源工具,在GitHub上可以下载源码:
https://github.com/google/protobuf.git
由于protobuf支持多种编程有语言的安装,因此在本次使用的是用C++语言编译安装protobuf工具
protobuf源码下的REANDME.md下的安装提示:
Protobuf supports several different programming languages. For each programming
language, you can find instructions in the corresponding source directory about
how to install protobuf runtime for that specific language:
| Language | Source |
|--------------------------------------|-------------------------------------------------------------|
| C++ (include C++ runtime and protoc) | [src](src) |
| Java | [java](java) |
| Python | [python](python) |
| Objective-C | [objectivec](objectivec) |
| C# | [csharp](csharp) |
| JavaNano | [javanano](javanano) |
| JavaScript | [js](js) |
| Ruby | [ruby](ruby) |
| Go | [golang/protobuf](https://github.com/golang/protobuf) |
| PHP | [php](php) |
| Dart | [dart-lang/protobuf](https://github.com/dart-lang/protobuf) |
linux下的编译安装
根据顶层代码README.md的提示,进入C++语言编译文件夹下src,阅读src下的README.md
1、安装必要工具
On Ubuntu, you can install them with:
$ sudo apt-get install autoconf automake libtool curl make g++ unzip
2、产生配置脚本
If you get the source from github, you need to generate the configure script
first:
$ ./autogen.sh
3、安装protobuf
To build and install the C++ Protocol Buffer runtime and the Protocol
Buffer compiler (protoc) execute the following:
$ ./configure
$ make
$ make check
$ sudo make install
$ sudo ldconfig # refresh shared library cache.
4、查看版本号
查看版本号时会出现如下出现问题:
~/Tools/protobuf$ protoc --version
protoc: error while loading shared libraries: libprotoc.so.14: cannot open shared object file: No such file or directory
那是因为没有给protobuf工具配置环境,配置库文件的环境
export LIBRARY_PATH=/usr/local/lib:$LIBRARY_PATH
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
再次查看版本号
~/Tools/protobuf$ protoc --version
libprotoc 3.4.0
至此linux版本下的protobuf工具安装成功
windows下的编译安装
在windows下编译protobuf源码需要安装Visual Studio和Cmake编译工具。
cmake下载地址:https://cmake.org/download/
下载安装包即可,如下图所示
安装好cmake后添加cmake环境变量, 如下所示。
set PATH=%PATH%;C:\Program Files (x86)\CMake\bin
在cmd命令行查看cmake版本,如下所示。
本次依旧使用的是用C++语言编译安装protobuf工具。
打开Visual studio 2017编译工具命令行选项,如下所示。
进入protobuf/cmake/文件夹下,阅读README.md文件。
Cmake编译
README.md中有一句说明:
If the *gmock* directory does not exist, and you do not want to build protobuf unit tests,
you need to add *cmake* command argument `-Dprotobuf_BUILD_TESTS=OFF` to disable testing.
没有用到gmock测试就在编译语句中加入-Dprotobuf_BUILD_TESTS=OFF
通过cmake -h查看当前cmake支持构建的编译工具
G:\share\protobuf\cmake\build\solution>cmake -h
......................................
Generators
The following generators are available on this platform:
Visual Studio 15 2017 [arch] = Generates Visual Studio 2017 project files.
Optional [arch] can be "Win64" or "ARM".
Visual Studio 14 2015 [arch] = Generates Visual Studio 2015 project files.
Optional [arch] can be "Win64" or "ARM".
Visual Studio 12 2013 [arch] = Generates Visual Studio 2013 project files.
Optional [arch] can be "Win64" or "ARM".
Visual Studio 11 2012 [arch] = Generates Visual Studio 2012 project files.
Optional [arch] can be "Win64" or "ARM".
Visual Studio 10<