Build the helloworld project of Google Protocol Buffer / protobuf from scratch (super detailed)

Build Google Protocol Buffer / protobuf helloworld project from scratch

Build the helloworld project of Protocol Buffer / protobuf from scratch

PS: If you are too lazy to read the text, the download link of the VS project generated by the process described in this article is provided at the end of the article, which can be downloaded and used directly.

foreword

  • The environment of this article:

    Win10(Windows SDK version 10.0.17763.0) + VS2017 + protobuf-3.19.0

  • Overall steps:

    • Download protobufsource code
    • run cmaketo generateprotobuf的vs工程
    • vs compile protobuf project (generate required *.lib file and protoc.exe)
    • Write the project's own .proto file and run it protoc.exeto generate the corresponding .h and .cc files
    • Introduce protobuf into your own project use

1. Download protobuf

  • download protobuf

Download address: https://github.com/protocolbuffers/protobuf/releases

The current latest version is 3.19.0, download directly from the download pageprotobuf-cpp-3.19.0.zip

download_protobuf.png

Notice:

If you don't want to compile by yourself, but want to download the compiled release file of the project, after importing protobuf into your own project, you will be prompted that the file is missing when compiling again.

download_protobuf_release.png

The error message is:

fatal error C1083: 无法打开包括文件: “google/protobuf/port_def.inc”: No such file or directory

  
  
   
   
  • 1

Look under the folder of the actual protoc-3.19.0-win64.zipdecompression directory , and there is indeed no corresponding file. Compared with the source code , the folder in the compressed package does have some files less.includeprotoc-3.19.0-win64.zipincludeprotobuf-cpp-3.19.0.zip

So in the end, you still have to download the source code and compile it yourself, and there are related files in the src directory of the source code.

  • download cmake

Download address https://cmake.org/download/

The current latest version is 3.22.0, download directly from the download pagecmake-3.22.0-rc1-windows-x86_64.zip

download_cmake.png

protobuf-cpp-3.19.0.zipYou need to use cmake to generate the vs project, so you need to download the cmake tool.

If you already have the cmake tool on your computer, you don’t need to download it, just use the cmake tool on your computer.

2. cmake generates protobuf project

cmake-3.22.0-rc1-windows-x86_64.zipAfter decompression , run cmake-gui.exe
the following steps in the pop-up window. (In the following example, the directory after protobuf decompression is E:/TestProj/protobuf-cpp-3.19.0)

  • step 1

    In the pop-up interface of cmake-gui.exe, configure according to the figure below. (here the compilation platform is selected as VS2017and x64)

cmake_step1-comment.png

  • step 2

You can change the default build configuration in the red background frame area. After the change is complete, click Configurethe button to update the configuration, and then click Generatethe button to generate the vs project.

cmake_step2-comment.png

  • step 3

    After the VS project is successfully generated, you can click to Open Projectopen the VS project of protobu.

cmake_step3-comment.png

In the last step, you can directly click Open Projectthe button, and cmake can directly call VS2017 to open the VS solution project file of protobuf.

You can also go to E:\TestProj\protobuf-cpp-3.19.0\buildthe directory yourself to VS2017open protobuf.slnthe file.
open_project.png

3. vs compile protobuf project

libprotobufCompile and compile these two projects separately in the opened protobuf project protoc. (Other projects compile or not, please see your actual situation). Under normal circumstances, only compiling libprotobufand protoctwo projects can already meet the core needs.

  • Take debug as an example, generate the following files: libprotobufd.lib, libprotocd.lib and protoc.exe

insert image description here

The file generated by the debug version is as follows

insert image description here

  • Taking release as an example, generate the following files: libprotobuf.lib, libprotoc.lib and protoc.exe

insert image description here

The files generated by the release version are as follows

insert image description here

4. Write the project's own .proto file

.protoFor the syntax of the file, please check other blogs or the official guide https://developers.google.com/protocol-buffers/docs/proto3

In this case proto3, consider the helloworld case. proto2Please modify the case yourself.

// helloworld.proto

syntax = 'proto3';

package hellopb;
message helloworld
{
int32 id = 1; // ID
string str = 2; // str
}

explain:

Define a package name called hellopb, corresponding to C++namespace

Define a helloworldmessage type with a message name of , which has 2 members, the id of type int32, and the member str of type string.

Use protoc.exeto process the message and generate a C++ compiler-recognizable .hand .ccfile

The generation command is as follows: (Pay attention to the paths of .exe and .proto files when using).

protoc.exe -I=. --cpp_out=. ./helloworld.proto

If you don't know the parameter meaning of proto.exe, you can use protoc.exe --helpthe command to view the help document.

You can refer to the figure below:

insert image description here

5. Introduce protobuf into your own project

Take the Debug version as an example here

  • Generate helloworld project

    Use vs2017 to generate a helloworld test project yourself.

  • copy dependencies

    Copy the generated libprotobufd.lib、libprotocd.lib和protoc.exefiles to the lib directory of the project. (Note: This article uses a static library. If you need a dynamic library, you need to choose to generate a dll dynamic library in the cmake stage)

  • copy header file

    Copy the folder E:\TestProj\protobuf-cpp-3.19.0\protobuf-3.19.0\srcunder the source code to the folder of the helloworld projectgoogleinclude

Text description:

  • In the VS project, configure the following project properties:

[配置属性]->[C/C++]->[常规]->[附加包含目录]: Add googlethe path of the folder, which is under the include of the project by default

[配置属性]->[连接器]->[常规]->[附加库目录]: Add libprotobufd.liband libprotocd.libthe path where the file is located

[配置属性]->[连接器]->[输入]->[附加依赖项]: add libprotobufd.libandlibprotocd.lib

  • Configure in code page:

#include "helloworld.pb.h" directly in the cpp file that needs to write the message

It can also be configured according to the example diagram below.

Project file directory

insert image description here

The directory after copying the files is:

insert image description here

solution

insert image description here

Modify additional include directories

insert image description here

Modify the add-on library directory

insert image description here

Modify additional dependencies

insert image description here

// main.cpp

#include <iostream>
#include <fstream>

# include “msg/helloworld.pb.h” // Include the generated header file

int main ( int argc , char * argv [ ] )
{
// Message encapsulation
hellopb :: helloworld msgwrite ;
msgwrite . set_id ( 1001 ) ;
msgwrite . set_str ( "hello world" ) ;
char buff [ 1024 ] = { 0 } ;
msgwrite.SerializeToArray ( buff , 1024 ) ; _ _ // serialize message

<span class="token comment">//解析消息</span>
hellopb<span class="token operator">::</span>helloworld msgread<span class="token punctuation">;</span>
msgread<span class="token punctuation">.</span><span class="token function">ParseFromArray</span><span class="token punctuation">(</span>buff<span class="token punctuation">,</span> <span class="token number">1024</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
std<span class="token operator">::</span>cout <span class="token operator">&lt;&lt;</span> <span class="token string">"id:"</span> <span class="token operator">&lt;&lt;</span> msgread<span class="token punctuation">.</span><span class="token function">id</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&lt;&lt;</span> std<span class="token operator">::</span>endl<span class="token punctuation">;</span>
std<span class="token operator">::</span>cout <span class="token operator">&lt;&lt;</span> <span class="token string">"str:"</span> <span class="token operator">&lt;&lt;</span> msgread<span class="token punctuation">.</span><span class="token function">str</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&lt;&lt;</span> std<span class="token operator">::</span>endl<span class="token punctuation">;</span>

}

The output after running is:

id:1001
str:hello world

Precautions

Q: An error occurs when compiling after adding dependencies and librarieserror LNK2038

1>------ 已启动生成: 项目: ProtocolBuf, 配置: Debug x64 ------
1>libprotobufd.lib(arenastring.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MTd_StaticDebug”不匹配值“MDd_DynamicDebug”(main.obj 中)
1>libprotobufd.lib(message_lite.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MTd_StaticDebug”不匹配值“MDd_DynamicDebug”(main.obj 中)
1>libprotobufd.lib(common.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MTd_StaticDebug”不匹配值“MDd_DynamicDebug”(main.obj 中)
... ...

A:

This problem protobuf.slnis caused by the inconsistency between the runtime library in the project properties of the next compilation libprotobufand the two projects and the runtime library of the helloworld project. Just modify the runtime library of the two to be consistent.protoc

The modified path is: [Configuration Properties]->[C/C++]->[Code Generation]->[Running Library]

libprotobufAnd protocthe default runtime library of the project's project properties is:

Debug : multi-threaded debugging (/MTd)

Release: multi-threaded (/MT)

insert image description here

The project resources described in this article have been uploaded to CSDN and can be downloaded on demand.
The project contains 4 versions of x86/x64 Debug/Release, all of which have been compiled and can be used immediately.
Download link: google protobuf beginner helloworld VS2017 + protobuf-3.19.0 project example

This is my first article in 2022, and it is also the first time to repost someone else's article.

Original source: https://blog.csdn.net/shadow_2011/article/details/121017458

Guess you like

Origin blog.csdn.net/jianjianshini/article/details/125538104