文件版本说明
版本 | 颁布日期 | 修订章节 | 作者 |
---|---|---|---|
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 2010 [arch] = Generates Visual Studio 2010 project files.
Optional [arch] can be "Win64" or "IA64".
Visual Studio 9 2008 [arch] = Generates Visual Studio 2008 project files.
Optional [arch] can be "Win64" or "IA64".
Visual Studio 8 2005 [arch] = Deprecated. Generates Visual Studio 2005
project files. Optional [arch] can be
"Win64".
Borland Makefiles = Generates Borland makefiles.
NMake Makefiles = Generates NMake makefiles.
NMake Makefiles JOM = Generates JOM makefiles.
Green Hills MULTI = Generates Green Hills MULTI files
(experimental, work-in-progress).
MSYS Makefiles = Generates MSYS makefiles.
MinGW Makefiles = Generates a make file for use with
mingw32-make.
Unix Makefiles = Generates standard UNIX makefiles.
Ninja = Generates build.ninja files.
Watcom WMake = Generates Watcom WMake makefiles.
CodeBlocks - MinGW Makefiles = Generates CodeBlocks project files.
CodeBlocks - NMake Makefiles = Generates CodeBlocks project files.
CodeBlocks - NMake Makefiles JOM
= Generates CodeBlocks project files.
CodeBlocks - Ninja = Generates CodeBlocks project files.
CodeBlocks - Unix Makefiles = Generates CodeBlocks project files.
CodeLite - MinGW Makefiles = Generates CodeLite project files.
CodeLite - NMake Makefiles = Generates CodeLite project files.
CodeLite - Ninja = Generates CodeLite project files.
CodeLite - Unix Makefiles = Generates CodeLite project files.
Sublime Text 2 - MinGW Makefiles
= Generates Sublime Text 2 project files.
Sublime Text 2 - NMake Makefiles
= Generates Sublime Text 2 project files.
Sublime Text 2 - Ninja = Generates Sublime Text 2 project files.
Sublime Text 2 - Unix Makefiles
= Generates Sublime Text 2 project files.
Kate - MinGW Makefiles = Generates Kate project files.
Kate - NMake Makefiles = Generates Kate project files.
Kate - Ninja = Generates Kate project files.
Kate - Unix Makefiles = Generates Kate project files.
Eclipse CDT4 - NMake Makefiles
= Generates Eclipse CDT 4.0 project files.
Eclipse CDT4 - MinGW Makefiles
= Generates Eclipse CDT 4.0 project files.
Eclipse CDT4 - Ninja = Generates Eclipse CDT 4.0 project files.
Eclipse CDT4 - Unix Makefiles= Generates Eclipse CDT 4.0 project files.
G:\share\protobuf\cmake\build\solution>
编译Release版本
README.md说明:
To start using a *Release* configuration:
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 ^
../..
It will generate *nmake* *Makefile* in current directory.
在Cmake文件夹中建立文件build\release,进入该文件夹内。
输入编译命令:
G:\share\protobuf\cmake\build\release>cmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release -Dprotobuf_BUILD_TESTS=OFF -DCMAKE_INSTALL_PREFIX=../../../../install ../..
-- The C compiler identification is MSVC 19.11.25547.0
-- The CXX compiler identification is MSVC 19.11.25547.0
-- Check for working C compiler:
F:/Visual_studio_2017/VC/Tools/MSVC/14.11.25503/bin/HostX64/x64/cl.exe
-- Check for working C compiler:
F:/Visual_studio_2017/VC/Tools/MSVC/14.11.25503/bin/HostX64/x64/cl.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler:
F:/Visual_studio_2017/VC/Tools/MSVC/14.11.25503/bin/HostX64/x64/cl.exe
-- Check for working CXX compiler:
F:/Visual_studio_2017/VC/Tools/MSVC/14.11.25503/bin/HostX64/x64/cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - not found
-- Found Threads: TRUE
-- Configuring done
-- Generating done
-- Build files have been written to: G:/share/protobuf/cmake/build/release
nmake编译
G:\share\protobuf\cmake\build\release>nmake
............................
[ 99%] Linking CXX static library libprotoc.lib
[ 99%] Built target libprotoc
Scanning dependencies of target protoc
[ 99%] Building CXX object CMakeFiles/protoc.dir/G_/share/protobuf/src/google/protobuf/compiler/main.cc.obj
main.cc
[100%] Linking CXX executable protoc.exe
[100%] Built target protoc
G:\share\protobuf\cmake\build\release>ls
CMakeCache.txt cmake js_embed.exe libprotobuf.lib protobuf.pc
CMakeFiles cmake_install.cmake js_embed.exe.manifest libprotoc.lib protoc.exe
Makefile extract_includes.bat libprotobuf-lite.lib protobuf-lite.pc protoc.exe.manifest
G:\share\protobuf\cmake\build\release>
编译Debug版本
README.md说明:
To use *Debug* configuration:
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 ^
../..
It will generate *nmake* *Makefile* in current directory.
在Cmake文件夹中建立文件build\debug,进入该文件夹内。
输入编译命令:
G:\share\protobuf\cmake\build\debug>cmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Debug -Dprotobuf_BUILD_TESTS=OFF -DCMAKE_INSTALL_PREFIX=../../../../install ../..
-- The C compiler identification is MSVC 19.11.25547.0
-- The CXX compiler identification is MSVC 19.11.25547.0
-- Check for working C compiler:
F:/Visual_studio_2017/VC/Tools/MSVC/14.11.25503/bin/HostX64/x64/cl.exe
-- Check for working C compiler:
F:/Visual_studio_2017/VC/Tools/MSVC/14.11.25503/bin/HostX64/x64/cl.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler:
F:/Visual_studio_2017/VC/Tools/MSVC/14.11.25503/bin/HostX64/x64/cl.exe
-- Check for working CXX compiler:
F:/Visual_studio_2017/VC/Tools/MSVC/14.11.25503/bin/HostX64/x64/cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - not found
-- Found Threads: TRUE
-- Configuring done
-- Generating done
-- Build files have been written to: G:/share/protobuf/cmake/build/debug
nmake编译
G:\share\protobuf\cmake\build\debug>nmake
............................
[ 99%] Linking CXX static library libprotocd.lib
[ 99%] Built target libprotoc
Scanning dependencies of target protoc
[ 99%] Building CXX object CMakeFiles/protoc.dir/G_/share/protobuf/src/google/protobuf/compiler/main.cc.obj
main.cc
[100%] Linking CXX executable protoc.exe
[100%] Built target protoc
G:\share\protobuf\cmake\build\debug>ls
CMakeCache.txt cmake js_embed.exe libprotobuf-lited.lib protobuf-lite.pc protoc.ilk
CMakeFiles cmake_install.cmake js_embed.ilk libprotobufd.lib protobuf.pc protoc.pdb
Makefile extract_includes.bat js_embed.pdb libprotocd.lib protoc.exe
G:\share\protobuf\cmake\build\debug>
Visual Studio 2017编译
README.md说明:
To create *Visual Studio* solution file:
C:\Path\to\protobuf\cmake\build>mkdir solution & cd solution
C:\Path\to\protobuf\cmake\build\solution>cmake -G "Visual Studio 12 2013 Win64" ^
-DCMAKE_INSTALL_PREFIX=../../../../install ^
../..
It will generate *Visual Studio* solution file *protobuf.sln* in current directory.
本次安装是Visual Studio 2017,根据实际安装的编译器选择编译语句。
在Cmake文件夹中建立文件build\ solution,进入该文件夹内。
输入编译命令:
G:\share\protobuf\cmake\build\solution>cmake -G "Visual Studio 15 2017 Win64" -Dprotobuf_BUILD_TESTS=OFF -DCMAKE_INSTALL_PREFIX=../../../../install ../..
-- Selecting Windows SDK version 10.0.16299.0 to target Windows 10.0.14393.
-- The C compiler identification is MSVC 19.11.25547.0
-- The CXX compiler identification is MSVC 19.11.25547.0
-- Check for working C compiler:
F:/Visual_studio_2017/VC/Tools/MSVC/14.11.25503/bin/HostX86/x64/cl.exe
-- Check for working C compiler:
F:/Visual_studio_2017/VC/Tools/MSVC/14.11.25503/bin/HostX86/x64/cl.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler:
F:/Visual_studio_2017/VC/Tools/MSVC/14.11.25503/bin/HostX86/x64/cl.exe
-- Check for working CXX compiler:
F:/Visual_studio_2017/VC/Tools/MSVC/14.11.25503/bin/HostX86/x64/cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - not found
-- Found Threads: TRUE
-- Configuring done
-- Generating done
-- Build files have been written to: G:/share/protobuf/cmake/build/solution
G:\share\protobuf\cmake\build\solution>ls
ALL_BUILD.vcxproj ZERO_CHECK.vcxproj js_embed.vcxproj.filters libprotoc.vcxproj.filters
ALL_BUILD.vcxproj.filters ZERO_CHECK.vcxproj.filters libprotobuf-lite.vcxproj protobuf-lite.pc
CMakeCache.txt cmake libprotobuf-lite.vcxproj.filters protobuf.pc
CMakeFiles cmake_install.cmake libprotobuf.vcxproj protobuf.sln
INSTALL.vcxproj extract_includes.bat libprotobuf.vcxproj.filters protoc.vcxproj
INSTALL.vcxproj.filters js_embed.vcxproj libprotoc.vcxproj protoc.vcxproj.filters
G:\share\protobuf\cmake\build\solution>
打开protobuf.sln文件,进入Visual Studio 2017编译器。
编译Release版本
编译完成如下图所示
编译Debug版本
编译完成如下图所示
编译后的Protobuf工具
Linux和windows编译Protobuf最终需要的文件为
Linux :/usr/local/bin/protoc
Windows :protoc.exe
对比Linux与Windows编译出来的工具大小。
$ ll /usr/local/bin/protoc
-rwxr-xr-x 1 root root 278960 9月 23 10:35 /usr/local/bin/protoc*
$ ls -l
total 30088
-rwxr-xr-x 1 zx_work 197609 12236800 Dec 5 15:15 VS_debug_protoc.exe*
-rwxr-xr-x 1 zx_work 197609 3166720 Dec 5 15:20 VS_release_protoc.exe*
-rwxr-xr-x 1 zx_work 197609 12236800 Dec 5 14:50 nmake_debug_protoc.exe*
-rwxr-xr-x 1 zx_work 197609 3160064 Dec 5 15:01 nmake_release_protoc.exe*
头文件包含
linux
头文件在/usr/local/include/google
ghost@ghost-machine:~$ ls /usr/local/include/google/
protobuf
ghost@ghost-machine:~$
windows
在每个文件夹下,成功编译后会有一个批处理文件生成代码需要的头文件extract_includes.bat,运行即可在当前目录下生成protobuf的头文件include。
库文件包含
Linux
库文件在/usr/local/lib
包含了静态库和动态库
ghost@ghost-machine:~$ ll /usr/local/lib/libproto*
-rw-r--r-- 1 root root 71893246 12月 13 20:40 /usr/local/lib/libprotobuf.a
-rwxr-xr-x 1 root root 988 12月 13 20:40 /usr/local/lib/libprotobuf.la*
-rw-r--r-- 1 root root 8994250 12月 13 20:40 /usr/local/lib/libprotobuf-lite.a
-rwxr-xr-x 1 root root 1023 12月 13 20:40 /usr/local/lib/libprotobuf-lite.la*
lrwxrwxrwx 1 root root 26 12月 13 20:40 /usr/local/lib/libprotobuf-lite.so -> libprotobuf-lite.so.15.0.0*
lrwxrwxrwx 1 root root 26 9月 23 10:35 /usr/local/lib/libprotobuf-lite.so.14 -> libprotobuf-lite.so.14.0.0*
-rwxr-xr-x 1 root root 3454672 9月 23 10:35 /usr/local/lib/libprotobuf-lite.so.14.0.0*
lrwxrwxrwx 1 root root 26 12月 13 20:40 /usr/local/lib/libprotobuf-lite.so.15 -> libprotobuf-lite.so.15.0.0*
-rwxr-xr-x 1 root root 3579208 12月 13 20:40 /usr/local/lib/libprotobuf-lite.so.15.0.0*
lrwxrwxrwx 1 root root 21 12月 13 20:40 /usr/local/lib/libprotobuf.so -> libprotobuf.so.15.0.0*
lrwxrwxrwx 1 root root 21 9月 23 10:35 /usr/local/lib/libprotobuf.so.14 -> libprotobuf.so.14.0.0*
-rwxr-xr-x 1 root root 27813752 9月 23 10:35 /usr/local/lib/libprotobuf.so.14.0.0*
lrwxrwxrwx 1 root root 21 12月 13 20:40 /usr/local/lib/libprotobuf.so.15 -> libprotobuf.so.15.0.0*
-rwxr-xr-x 1 root root 28002752 12月 13 20:40 /usr/local/lib/libprotobuf.so.15.0.0*
-rw-r--r-- 1 root root 105031320 12月 13 20:40 /usr/local/lib/libprotoc.a
-rwxr-xr-x 1 root root 1004 12月 13 20:40 /usr/local/lib/libprotoc.la*
lrwxrwxrwx 1 root root 19 12月 13 20:40 /usr/local/lib/libprotoc.so -> libprotoc.so.15.0.0*
lrwxrwxrwx 1 root root 19 9月 23 10:35 /usr/local/lib/libprotoc.so.14 -> libprotoc.so.14.0.0*
-rwxr-xr-x 1 root root 34372592 9月 23 10:35 /usr/local/lib/libprotoc.so.14.0.0*
lrwxrwxrwx 1 root root 19 12月 13 20:40 /usr/local/lib/libprotoc.so.15 -> libprotoc.so.15.0.0*
-rwxr-xr-x 1 root root 34319440 12月 13 20:40 /usr/local/lib/libprotoc.so.15.0.0*
ghost@ghost-machine:~$
windows
默认编译成静态库,编译成动态库会覆盖静态库的lib
编译成动态库需要在cmake时加入描述:
-Dprotobuf_BUILD_SHARED_LIBS=ON
或
-Dprotobuf_BUILD_SHARED_LIBS=YES
再通过nmake即可生成动态库
所包含的静态库:
release版本:
libprotobuf.lib
debug版本:
libprotobufd.lib
动态库:
release版本:
libprotoc.lib
libprotobuf.lib
libprotobuf.dll
debug版本:
libprotocd.lib
libprotobufd.lib
libprotobufd.dll
执行文件:
libprotobuf.dll
libprotoc.dll
libprotobufd.dll
libprotocd.dll
版本编译
不同版本的文件都不能共用,每次升级版本库都需要重新编译生成新的库文件和执行文件,不然执行文件生成的cc和h文件里面有版本检查。
#define GOOGLE_PROTOBUF_MIN_PROTOC_VERSION 3005000
#if 3004000 < GOOGLE_PROTOBUF_MIN_PROTOC_VERSION
#error This file was generated by an older version of protoc which is
#error incompatible with your Protocol Buffer headers. Please
#error regenerate this file with a newer version of protoc.
#endif
3.5版本不兼容3.4版本