首页 > 其他分享 >ProtoBuf_Win编译安装

ProtoBuf_Win编译安装

时间:2023-05-26 14:24:38浏览次数:54  
标签:cmake protobuf .. Win 编译 build install Path ProtoBuf

ProtoBuf Windows编译和安装

1.先安装cmake

protocol buffer 的编译需要安装cmake,可到 https://cmake.org/下载并安装。

2.下载ProtoBuf

protobuf v3.19.4开源链接:Release Protocol Buffers v3.19.4 · protocolbuffers/protobuf (github.com)

3.解压之后,按照cmake文件夹中的readme,即可完成编译

具体也可以参考4的这篇博文。

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.

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 protobuf
 C:\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 build
 C:\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 release
 C:\Path\to\protobuf\cmake\build\release>cmake -G "NMake Makefiles" ^
 -DCMAKE_BUILD_TYPE=Release ^
 -DCMAKE_INSTALL_PREFIX=../../../../install ^
 ../..
cmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../../../../install ../..
cmake -G “NMake Makefiles” -DCMAKE_BUILD_TYPE=Release -Dprotobuf_BUILD_TESTS=OFF -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 debug
 C:\Path\to\protobuf\cmake\build\debug>cmake -G "NMake Makefiles"
 -DCMAKE_BUILD_TYPE=Debug
 -DCMAKE_INSTALL_PREFIX=../../../../install ../..
cmake -G “NMake Makefiles” -DCMAKE_BUILD_TYPE=Debug -Dprotobuf_BUILD_TESTS=OFF -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 solution
 C:\Path\to\protobuf\cmake\build\solution>cmake -G "Visual Studio 16 2019" ^
 -DCMAKE_INSTALL_PREFIX=../../../../install ^
 ../..
cmake -G "Visual Studio 14 2015" -Dprotobuf_BUILD_TESTS=OFF -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.cc
 Note: 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

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.git
 C:\Path\to>cd zlib

Compiling and Installing:

 C:\Path\to\zlib>mkdir build & cd build
 C:\Path\to\zlib\build>mkdir release & cd release
 C:\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.

4.可参考这篇博文

protobuf windows编译和安装_wx637304bacd051的技术博客_51CTO博客

在caffe框架中,使用的数据格式是google的 protocol buffer。对这个不了解,所以,想简单学习一下。简单来说,Protocol Buffer 是一种轻便高效的结构化数据存储格式,可以用于结构化数据序列化和反序列化。一提到序列化就想到了JSON,不错,两者很相似。如果对JSON不熟悉,那么XML应该知道吧。由于它是一种二进制的格式,比使用 xml 进行数据交换快许多。可以把它用于分布式应用之间的数据通信或者异构环境下的数据交换。作为一种效率和兼容性都很优秀的二进制数据传输格式,可以用于诸如网络传输、配置文件、数据存储等诸多领域。

protocol buffer 的编译需要安装cmake,可到 https://cmake.org/下载并安装。

下载protocol buffer包,可直接到google官网下载, http://code.google.com/p/protobuf/,但是我上不了google,所以我在 https://github.com/google/protobuf这儿下载。

下载解压后,如我的目录:D:\caffe\protoBuf下,会有一个cmake文件夹,编译的文件都放在这儿了。打开VS的命令提示行,如

C:\Program Files (x86)\Microsoft Visual Studio 11.0>
进入编译目录

C:\Program Files (x86)\Microsoft Visual Studio 11.0>cd d:\caffe\protoBuf\cmake
新建目录build,用于存放makefile文件。注意,可编译为DEBUG版本和release版本,但是分别编译。

d:\caffe\protobuf\cmake>mkdir build & cd build
如果编译debug版本

d:\caffe\protobuf\cmake\build>mkdir debug & cd debug

d:\caffe\protobuf\cmake\build\debug>cmake -G “NMake Makefiles” -DCMAKE_BUILD_TYPE=Debug -Dprotobuf_BUILD_TESTS=OFF

-DCMAKE_INSTALL_PREFIX=../../../install ../..
如果编译release版本

d:\caffe\protobuf\cmake\build>mkdir release & cd release

d:\caffe\protobuf\cmake\build\release>cmake -G “NMake Makefiles” -DCMAKE_BUILD_TYPE=Release -Dprotobuf_BUILD_TESTS=OFF

-DCMAKE_INSTALL_PREFIX=../../../install ../..
仔细一比较两种模式,就能看出区别在哪了。后面我就只以debug为例了,基本上是一样的操作

接下来最重要的一步,编译

d:\caffe\protobuf\cmake\build\debug>nmake
很简单,直接输入nmake回车就可以了,然后慢慢等待。

编译完成后,进行安装

d:\caffe\protobuf\cmake\build\debug>nmake install
会在最上层根目录(d:\caffe\protobuf)下生成一个install的文件夹,里面有三个文件夹,bin,include和lib。如果对vs比较熟悉的话,就知道这三个文件夹代表着什么了。bin文件里面protoc.exe, include里面是包含头文件,lib里面是三个静态链接库文件。至此,编译就结束了。在你的项目里面设置好包含目录和库目录就可以使用了。

标签:cmake,protobuf,..,Win,编译,build,install,Path,ProtoBuf
From: https://www.cnblogs.com/miaochuanjie/p/17434578.html

相关文章

  • windows环境下 java 使用sqlite-jdbc 加载mod_spatialite用于地理空间处理
    由于项目需要,将sqlite中的数据使用空间函数(st_astext(),st_geomfromtext()等)处理空间坐标数据,这就需要加载mod_spatialite组件,从网上找了很多方法,也问了gpt,也从官网上(http://www.gaia-gis.it/gaia-sins/spatialite-cookbook/html/java.html)找到了加载mod_spatialite组件的示......
  • 最完美WIN10_Pro_22H2.19045.3031软件选装纯净版VIP41.7
    【系统简介】=============================================================1.本次更新母盘来自网络某大神。进一步精简优化调整。2.只为呈现最好的作品,手工精简优化部分较多。3.OS版本号为19045.3031。个别要求高的就下MSDN吧,里面啥功能都有。4.集成《DrvCeo-2.13.0.8》网卡版、......
  • windows WSL安装hedgedoc
    GitWSL似乎默认装好了git,用下面命令查一下可以查到root@DESKTOP-FTUL9EC:~#git--versiongitversion2.25.1Docker17.03.1orhigher看了官方文档,安装dockerengine需要如下ubuntu版本:UbuntuLunar23.04UbuntuKinetic22.10UbuntuJammy22.04(LTS)UbuntuF......
  • 关于开启windows自带虚拟机
    开启Windows自带虚拟机这次在工作中写了一个打印的工具,开始时并未考虑周全,用的.netfromwork4.8的框架,结果安装到win7的机器上,直接打不开了,而目前公司里已经找不到win7系统的电脑了,很多硬件已经不再支持win7,所以,需要安装一下虚拟机,进行测试。由于我的电脑是家庭版的所以......
  • nginx windows下重载配置失败
    正常操作命令是:cdC:\ProgramFiles\nginx&&nginx-sreload。但是把nginx安装为windows服务运行后,再执行重载就不好使了。会报错:nginx:[error]OpenEvent("Global\ngx_reload_4460")failed(5:Accessisdenied),即便是用管理员运行cmd权限也不够。 【解决办法】1.下载......
  • Graphpad Prism9.5.1 科学绘图软件 下载安装教程(含Win/Mac)
    软件下载AutodeskFlame2024formac软件介绍AutodeskFlame破解版由是Autodesk公司(译作欧特克)开发的一款高端电影剪辑和特效制作系统。Flame是用于高速合成、高级图形和客户驱动的交互设计的终极视觉特效制作系统。AutodeskFlame提供出色的性能和荣获奥斯卡奖的工具。从......
  • 如何在win10桌面添加备忘录?电脑桌面添加备忘录方法
    Win10系统电脑在国内的办公场景中普及率是非常高的,绝大多数从事普通工作的职场人士都会使用win10系统电脑来办公。不过有不少上班族表示,自己在使用电脑办公时,如果想要随手记录一些工作注意事项、工作常规流程、待办的工作任务等,只能够打开手机在备忘录中记录这些事情,简直太不方便......
  • 如何让python程序在windows下开机启动
    说明简述:通过将启动脚本写入注册表\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run来实现开机自启问题:这种开机启动比较奇怪,运行路径是cmd.exe的路径,所以执行需要自启的程序时,读取其他相对路径的文件会有错误解决:执行启动前,切一下目录启动脚本importos......
  • Windows——on-screen menus are locked怎么解除
    出现OSDLOCKED是菜单被锁的,具体的清除方法如下:1、如果OSD已锁定,请持续按住Menu按钮(最左边第一个)10秒钟就可以解除锁定OSD。2、如果OSD未锁定,请持续按住Menu按钮10秒钟就可以锁定OSD。 简介:OSD是OnScreenDisplay的缩写,即屏幕菜单式调节显示。是在显示器......
  • windows server2016 操作系统修改默认远程端口
    一、需求   远程端口,windows默认的3389.linux的22,这种都是知名端口,如果IP地址暴露,很可能会被攻击,这时候就需要更改端口号。二、操作步骤2.1打开注册表   快捷键WIN+R,命令行窗口输入regedit2.2进入以下路径  这里是默认端口,修改为自己除1024以后,以及未被......