protobuf-master :编译篇

article/2025/8/30 8:08:18

protobuf的编译其实相对简单,这里搬一下protobuf的ReadMe就很清楚了~ 如果是平时接触开源项目无从下手的小伙伴,也建议从阅读ReadMe开始哦!

This directory contains CMake files that can be used to build
protobuf with MSVC on Windows. You can build the project from
Command Prompt and using an Visual Studio IDE.

我们需要安装cmake工具以及vs studio的IDE。

You need to have CMake, Visual
Studio and optionally
Git installed on your computer before
proceeding.

Most of the instructions will be given to the Сommand Prompt, but
the same actions can be performed using appropriate GUI tools.

Environment Setup

Open the appropriate Command Prompt from the Start menu.

For example x86 Native Tools Command Prompt for VS 2019:
C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional>

Change to your working directory:

C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional>cd C:\Path\to
C:\Path\to>

Where C:\Path\to is path to your real working directory.

Create a folder where protobuf headers/libraries/binaries will be installed after built:

C:\Path\to>mkdir install

If cmake command is not available from Command Prompt, add it to system PATH variable:

C:\Path\to>set PATH=%PATH%;C:\Program Files (x86)\CMake\bin

If git command is not available from Command Prompt, add it to system PATH variable:

C:\Path\to>set PATH=%PATH%;C:\Program Files\Git\cmd

Good. Now you are ready to continue.

Getting Sources

You can get the latest stable source packages from the release page:

https://github.com/protocolbuffers/protobuf/releases/latest

For example: if you only need C++, download protobuf-cpp-[VERSION].tar.gz; if
you need C++ and Java, download protobuf-java-[VERSION].tar.gz (every package
contains C++ source already); if you need C++ and multiple other languages,
download protobuf-all-[VERSION].tar.gz.

Or you can use git to clone from protobuf git repository.

 C:\Path\to> git clone -b [release_tag] https://github.com/protocolbuffers/protobuf.git

Where [release_tag] is a git tag like v3.0.0-beta-1 or a branch name like master
if you want to get the latest code.

Go to the project folder:

 C:\Path\to>cd protobufC:\Path\to\protobuf>

Remember to update any submodules if you are using git clone (you can skip this
step if you are using a release .tar.gz or .zip package):

C:\Path\to> git submodule update --init --recursive

Now go to cmake folder in protobuf sources:

C:\Path\to\protobuf>cd cmake
C:\Path\to\protobuf\cmake>

Good. Now you are ready to CMake configuration.

CMake Configuration

CMake supports a lot of different
generators
for various native build systems.
We are only interested in
Makefile
and
Visual Studio
generators.

We will use shadow building to separate the temporary files from the protobuf source code.

Create a temporary build folder and change your working directory to it:

 C:\Path\to\protobuf\cmake>mkdir build & cd buildC:\Path\to\protobuf\cmake\build>

The Makefile generator can build the project in only one configuration, so you need to build
a separate folder for each configuration.

To start using a *Release* configuration:C:\Path\to\protobuf\cmake\build>mkdir release & cd releaseC:\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.

To use *Debug* configuration:C:\Path\to\protobuf\cmake\build>mkdir debug & cd debugC:\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.

To create *Visual Studio* solution file:C:\Path\to\protobuf\cmake\build>mkdir solution & cd solutionC:\Path\to\protobuf\cmake\build\solution>cmake -G "Visual Studio 16 2019" ^-DCMAKE_INSTALL_PREFIX=../../../../install ^../..

It will generate Visual Studio solution file protobuf.sln in current directory.

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.

To make a Visual Studio file for Visual Studio 16 2019, create the Visual Studio
solution file above and edit the CMakeCache file.

C:Path\to\protobuf\cmake\build\solution\CMakeCache

Then create the Visual Studio solution file again

Compiling

To compile protobuf:

 C:\Path\to\protobuf\cmake\build\release>nmake

or

  C:\Path\to\protobuf\cmake\build\debug>nmake

And wait for the compilation to finish.

If you prefer to use the IDE:

  • Open the generated protobuf.sln file in Microsoft Visual Studio.
  • Choose “Debug” or “Release” configuration as desired.
  • From the Build menu, choose “Build Solution”.

And wait for the compilation to finish.

Testing

To run unit-tests, first you must compile protobuf as described above.
Then run:

 C:\Path\to\protobuf\cmake\build\release>nmake check

or

 C:\Path\to\protobuf\cmake\build\debug>nmake check

You can also build project check from Visual Studio solution.
Yes, it may sound strange, but it works.

You should see output similar to:

 Running main() from gmock_main.cc[==========] Running 1546 tests from 165 test cases....[==========] 1546 tests from 165 test cases ran. (2529 ms total)[  PASSED  ] 1546 tests.

To run specific tests:

 C:\Path\to\protobuf>cmake\build\release\tests.exe --gtest_filter=AnyTest*Running main() from gmock_main.ccNote: Google Test filter = AnyTest*[==========] Running 3 tests from 1 test case.[----------] Global test environment set-up.[----------] 3 tests from AnyTest[ RUN      ] AnyTest.TestPackAndUnpack[       OK ] AnyTest.TestPackAndUnpack (0 ms)[ RUN      ] AnyTest.TestPackAndUnpackAny[       OK ] AnyTest.TestPackAndUnpackAny (0 ms)[ RUN      ] AnyTest.TestIs[       OK ] AnyTest.TestIs (0 ms)[----------] 3 tests from AnyTest (1 ms total)[----------] Global test environment tear-down[==========] 3 tests from 1 test case ran. (2 ms total)[  PASSED  ] 3 tests.

Note that the tests must be run from the source folder.

If all tests are passed, safely continue.

Installing

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

You can also build project INSTALL from Visual Studio solution.
It sounds not so strange and it works.

This will create the following folders under the install location:

  • bin - that contains protobuf protoc.exe compiler;
  • include - that contains C++ headers and protobuf *.proto files;
  • lib - that contains linking libraries and CMake configuration files for protobuf package.

Now you can if needed:

  • Copy the contents of the include directory to wherever you want to put headers.
  • Copy protoc.exe wherever you put build tools (probably somewhere in your PATH).
  • Copy linking libraries libprotobuf[d].lib, libprotobuf-lite[d].lib, and libprotoc[d].lib wherever you put libraries.

To avoid conflicts between the MSVC debug and release runtime libraries, when
compiling a debug build of your application, you may need to link against a
debug build of libprotobufd.lib with “d” postfix. Similarly, release builds should link against
release libprotobuf.lib library.

DLLs vs. static linking

默认的编译是编译静态库,如果要编译动态库,需要添加 -Dprotobuf_BUILD_SHARED_LIBS=ON

Static linking is now the default for the Protocol Buffer libraries. Due to
issues with Win32’s use of a separate heap for each DLL, as well as binary
compatibility issues between different versions of MSVC’s STL library, it is
recommended that you use static linkage only. However, it is possible to
build libprotobuf and libprotoc as DLLs if you really want. To do this,
do the following:

  • Add an additional flag -Dprotobuf_BUILD_SHARED_LIBS=ON when invoking cmake
  • Follow the same steps as described in the above section.
  • When compiling your project, make sure to #define PROTOBUF_USE_DLLS.

When distributing your software to end users, we strongly recommend that you
do NOT install libprotobuf.dll or libprotoc.dll to any shared location.
Instead, keep these libraries next to your binaries, in your application’s
own install directory. C++ makes it very difficult to maintain binary
compatibility between releases, so it is likely that future versions of these
libraries will not be usable as drop-in replacements.

If your project is itself a DLL intended for use by third-party software, we
recommend that you do NOT expose protocol buffer objects in your library’s
public interface, and that you statically link protocol buffers into your
library.

ZLib support

If you want to include GzipInputStream and GzipOutputStream
(google/protobuf/io/gzip_stream.h) in libprotobuf, you will need to do a few
additional steps.

Obtain a copy of the zlib library. The pre-compiled DLL at zlib.net works.
You need prepare it:

  • Make sure zlib’s two headers are in your C:\Path\to\install\include path
  • Make sure zlib’s linking libraries (*.lib file) is in your
    C:\Path\to\install\lib library path.

You can also compile it from source by yourself.

Getting sources:

 C:\Path\to>git clone -b v1.2.8 https://github.com/madler/zlib.gitC:\Path\to>cd zlib

Compiling and Installing:

 C:\Path\to\zlib>mkdir build & cd buildC:\Path\to\zlib\build>mkdir release & cd releaseC:\Path\to\zlib\build\release>cmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release ^-DCMAKE_INSTALL_PREFIX=../../../install ../..C:\Path\to\zlib\build\release>nmake & nmake install

You can make debug version or use Visual Studio generator also as before for the
protobuf project.

Now add bin folder from install to system PATH:

 C:\Path\to>set PATH=%PATH%;C:\Path\to\install\bin

You need reconfigure protobuf with flag -Dprotobuf_WITH_ZLIB=ON when invoking cmake.

Note that if you have compiled ZLIB yourself, as stated above,
further disable the option -Dprotobuf_MSVC_STATIC_RUNTIME=OFF.

If it reports NOTFOUND for zlib_include or zlib_lib, you might haven’t put
the headers or the .lib file in the right directory.

If you already have ZLIB library and headers at some other location on your system then alternatively you can define following configuration flags to locate them:

-DZLIB_INCLUDE_DIR=<path to dir containing zlib headers>
-DZLIB_LIB=<path to dir containing zlib>

Build and testing protobuf as usual.

Notes on Compiler Warnings

The following warnings have been disabled while building the protobuf libraries
and compiler. You may have to disable some of them in your own project as
well, or live with them.

  • C4018 - ‘expression’ : signed/unsigned mismatch
  • C4146 - unary minus operator applied to unsigned type, result still unsigned
  • C4244 - Conversion from ‘type1’ to ‘type2’, possible loss of data.
  • C4251 - ‘identifier’ : class ‘type’ needs to have dll-interface to be used by
    clients of class ‘type2’
  • C4267 - Conversion from ‘size_t’ to ‘type’, possible loss of data.
  • C4305 - ‘identifier’ : truncation from ‘type1’ to ‘type2’
  • C4355 - ‘this’ : used in base member initializer list
  • C4800 - ‘type’ : forcing value to bool ‘true’ or ‘false’ (performance warning)
  • C4996 - ‘function’: was declared deprecated

C4251 is of particular note, if you are compiling the Protocol Buffer library
as a DLL (see previous section). The protocol buffer library uses templates in
its public interfaces. MSVC does not provide any reasonable way to export
template classes from a DLL. However, in practice, it appears that exporting
templates is not necessary anyway. Since the complete definition of any
template is available in the header files, anyone importing the DLL will just
end up compiling instances of the templates into their own binary. The
Protocol Buffer implementation does not rely on static template members being
unique, so there should be no problem with this, but MSVC prints warning
nevertheless. So, we disable it. Unfortunately, this warning will also be
produced when compiling code which merely uses protocol buffers, meaning you
may have to disable it in your code too.

编译完成,如图:(x64 debug without zlib 版本)
在这里插入图片描述
下一篇,开始使用protobuf~~


http://chatgpt.dhexx.cn/article/C5mZtLSK.shtml

相关文章

ProtoBuf编码原理

背景 Protobuf是我们在网络传输中经常会用到的协议&#xff0c;优点是版本间兼容性强&#xff0c;对数据序列化时的极致压缩使得Protobuf包体积比xml、json等格式要小很多&#xff0c;节约流量。对于pb协议的具体使用方法&#xff0c;其官网有比较详细的说明&#xff0c;本文不…

windows protobuf编译

protobuf编译 Protobuf下载地址&#xff1a;https://github.com/protocolbuffers/protobuf/releases 1、配置cmake: 2、点击生成&#xff0c;打开工程文件&#xff1a;略 3、编译protobuf: 4、安装完成展示&#xff1a; 测试&#xff1a; 1、创建在bin目录下创建build.bat…

【Protobuf】Protobuf协议

Protobuf协议 什么是Protobuf一、编写proto文件二、生成协议类三、编码解码3.1 编码方法3.2 解码方法 什么是Protobuf Protobuf是谷歌发布的一套协议格式&#xff0c;它规定了一系列编码和解码方法。 目前&#xff0c;网上已经有不少实现Protobuf编码解码的库&#xff0c;可以…

protobuf简介

文章目录 一、protobuf的定义二、protobuf的优缺点2.1、优点2.2、缺点 三、protobuf的使用流程3.1、protobuf在Linux下的安装过程3.2、定义proto文件3.3、protoc编译器3.4、调用接口进行序列化、反序列化 四、protobuf的应用场景五、protobuf与json和XML的对比 一、protobuf的定…

java中使用protobuf总结

基本没怎么接触过java编程&#xff0c;别的团队发过来一个用java编写的存储pb的文件&#xff0c;让拆分和解析&#xff0c;硬着头皮做一下&#xff0c;在此将步骤做个记录&#xff1a; 下载安装protobuf https://github.com/protocolbuffers/protobuf/tags?afterv3.6.1.2 编译…

protobuf 详解

protobuf简介 Protobuf是Protocol Buffers的简称&#xff0c;它是Google公司开发的一种数据描述语言&#xff0c;是一种轻便高效的结构化数据存储格式&#xff0c;可以用于结构化数据串行化&#xff0c;或者说序列化 。它很适合做数据存储或 RPC 数据交换格式。可用于通讯协议…

protobuf介绍和语法

目录 前言 语法 标识符 字段 字段类型 proto2和proto3区别 前言 Protobuf即Protocol Buffers&#xff0c;是Google公司开发的一种跨语言和平台的序列化数据结构的方式&#xff0c;是一个灵活的、高效的用于序列化数据的协议。 与XML和JSON格式相比&#xff0c;pr…

Protobuf:一种更小、更快、更高效的协议

C/CLinux服务器开发/后台架构师知识体系 Protobuf介绍 Protobuf (Protocol Buffers) 是谷歌开发的一款无关平台&#xff0c;无关语言&#xff0c;可扩展&#xff0c;轻量级高效的序列化结构的数据格式&#xff0c;用于将自定义数据结构序列化成字节流&#xff0c;和将字节流反…

win10商店打不开_win10应用商店闪退是咋回事呢

win10虽然具有闪电般的开机速度&#xff0c;并且还新增了很多功能。但比较是全新的操作系统&#xff0c;所以难免会存在一些故障&#xff0c;这里小编就给大家讲讲win10应用商店闪退打不开怎么解决。 方法一 1&#xff0c;首先&#xff0c;打开开始菜单&#xff0c;进入设置&am…

电脑安装Linux闪退,win10系统运行内置Linux系统闪退如何处理

我们在win10系统电脑的使用中&#xff0c;有小伙伴在Linux系统的使用中出现了问题&#xff0c; win10系统运行内置Linux系统闪退的情况出现了&#xff0c;这是什么原因导致的呢&#xff0c;我们在win10系统运行内置Linux系统闪退如何处理&#xff0c;今天小编就来跟大家分享一下…

Java版mc闪退_本文传授win10运行mc闪退的具体操作对策

我们在使用电脑的时候遇到了win10运行mc闪退问题确实比较难受&#xff0c;要是你的电脑技术没有达到一定的水平&#xff0c;可能就不能解决这个win10运行mc闪退的情况。我们应当如何处理这个问题呢&#xff1f;小编先给大伙说说简单的措施&#xff1a;1、确保电脑中安装了 .NET…

(2022.5.27)【Win10】Windows10重置后微软商店闪退打不开、图片闪退打不开、UWP应用闪退打不开——可能的解决方案

更新日志 20220609 增加注意事项 注意事项 经过多为网友的反馈&#xff0c;目前这个方法是无法直接解决微软商店打不开的问题。因此&#xff0c;基于我目前的了解&#xff08;6月9日&#xff09;&#xff0c;如果大家遇到这个问题&#xff0c;真的只能重新 U 盘安装系统了。…

win10内置计算机和天气闪退,win10系统中天气闪退怎么办?Win10天气应用闪退问题解决方法...

win10系统中天气闪退怎么办&#xff1f;最近有部分用户在安装了win10系统后发现自带的天气应用出现闪退的情况&#xff0c;点击天气应用&#xff0c;发现它启动了很久&#xff0c;然后就自动关闭了。之后再点击天气应用就闪退&#xff0c;打不开。而尝试打开别的应用却可以正常…

解决WIN10下应用商店不能用,闪退的情况

解决WIN10下应用商店不能用,闪退的情况 先说下我的情况,也是博主手贱,经常看PC上的某个文件或者程序不顺眼的话就会想办法把它干掉,为此重装过几次系统… 这一次是装了win10的周年更新后,烦人的cortana,onedrive等一些我不想要的APP又回来了,在暴力清理这些APP的时候,需要特殊…

win10java闪退怎么办_Win10应用打不开或闪退怎么办?解决方案在此

可能有一些用户升级Win10之后遇到了应用商店、应用打不开或闪退的问题&#xff0c;此时可尝试通过下面的一些方法来解决。 1、点击任务栏的搜索(Cortana小娜)图标&#xff0c;输入Powershell&#xff0c;在搜索结果中右键单击Powershell&#xff0c;选择“以管理员身份运行”。…

win10的c语言程序闪退,Win10专业版软件打不开闪退怎么办?

现在用到最多的Win10系统是Win10专业版&#xff0c;用户重装Win10专业版系统的目的就是为了解决电脑遇到的问题&#xff0c;然而重装系统后还是会出现许许多多的问题&#xff0c;比如说部分软件打不开了&#xff0c;闪退的问题。如果您也遇到了相同的问题&#xff0c;下面就是小…

win10安装虚拟机闪退_win10应用商店战争机器4闪退,无法运行。

创建日期 2018/01/07 win10应用商店战争机器4闪退&#xff0c;无法运行。 日志名称: System 来源: Microsoft-Windows-DistributedCOM 日期: 2018-01-07 14:05:10 事件 ID: 10001 任务类别: 无 级别: 错误 关键字:…

右击计算机管理打开会闪退,win10应用商店为什么会闪退 win10应用商店出故障怎么修复...

win10系统有个应用商店&#xff0c;在商店里用户可以下载一些软件应用&#xff0c;很多用户反馈win10应用商店老是闪退&#xff0c;重启也没有用&#xff0c;这该怎么办&#xff1f;下面小编为大家科普下win10应用商店闪退的解决方案&#xff0c;希望可以帮助到大家。 win10应用…

win10的c语言程序闪退,win10内置应用出现闪退怎么回事? win10打开应用总闪退的解决方法...

Windows10操作系统新增加很多实用的功能&#xff0c;对大家操作电脑有很大帮助。Win10专业版系统自带有相机功能、地图功能、时钟功能&#xff0c;同时还有一个应用商店功能&#xff0c;有的小伙伴说打开内置应用时出现闪退&#xff0c;究竟是哪里出现问题&#xff1f;针对此问…

win10的c语言程序闪退,win10 1909系统出现应用闪退如何解决

许多用户在升级更新到win10 1909版本系统之后&#xff0c;反映说遇到这样一个问题&#xff0c;就是使用应用的时候会出现闪退的现象&#xff0c;该怎么处理呢&#xff0c;下面给大家带来win10 1909系统出现应用闪退的解决措施。 一、重装应用 将闪退的应用卸载之后重新安装一下…