How to Use C++ Protobuf on Windows

Words: 287

Refer to the following to configure Protobuf:

https://github.com/protocolbuffers/protobuf/tree/master/src#c-installation—windows

Required Environment

It is recommended to install the following environment in advance:

  • Windows 10
  • Git
  • CMake
  • Visual Studio 2019

Generator Tool

Download the protobuf generator tool from https://github.com/protocolbuffers/protobuf/releases/latest.

The file should be in the form protoc-$VERSION-win32.zip; extract bin/protoc.exe from it and place it under Dep/protobuf/bin.

Build Dependencies

Refer to the following for the dependencies required for compilation:

https://github.com/protocolbuffers/protobuf/blob/master/cmake/README.md

Download the protobuf source files from https://github.com/protocolbuffers/protobuf/releases/latest.

The file should be in the form protobuf-cpp-[VERSION].tar.gz.

Extract all files into a folder; here D:\Protobuf is used as an example.

Open cmake-gui; set the source code folder to D:\Protobuf\cmake and the build the binaries folder to the target folder, here using D:\Protobuf-build as an example.

Click Configure and select Visual Studio 2019.

Uncheck all options in the configuration.

Note the CMAKE_INSTALL_PREFIX value; this is where the target files will be placed after the install step is run later. Here D:/Protobuf-install is used as an example.

Click Generate to generate the solution, then Open Project to open the solution.

Select MinSizeRel and x64, then build the solution.

Right-click INSTALL, select Build, and the target files will be generated in the folder configured earlier.

Copy D:/Protobuf-install/include to the project’s /protobuf/include.

Copy D:/Protobuf-install/lib to the project’s /protobuf/lib.

Configure the Project

In a Visual Studio project, configure Protobuf as follows.

Configure Header File References

Right-click the project and open Properties, click C/C++, and in 附加包含目录, enter the protobuf header file directory, for example: \protobuf\include.

Configure Static Linking

Right-click the project and open Properties, click 链接器, and in 附加库目录, enter the protobuf dependency file directory, for example: \protobuf\lib.

Expand the 链接器 section, select 输入, and in 附加依赖项, enter the protobuf dependency file names, such as libprotobuf.lib and libprotobuf-lite.lib.

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...