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
protobuf
source code - run
cmake
to generateprotobuf的vs工程
- vs compile protobuf project (generate required *.lib file and protoc.exe)
- Write the project's own .proto file and run it
protoc.exe
to generate the corresponding .h and .cc files - Introduce protobuf into your own project use
- Download
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
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.
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.zip
decompression directory , and there is indeed no corresponding file. Compared with the source code , the folder in the compressed package does have some files less.include
protoc-3.19.0-win64.zip
include
protobuf-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
protobuf-cpp-3.19.0.zip
You 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.zip
After 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
VS2017
andx64
)
- step 2
You can change the default build configuration in the red background frame area. After the change is complete, click Configure
the button to update the configuration, and then click Generate
the button to generate the vs project.
-
step 3
After the VS project is successfully generated, you can click to
Open Project
open the VS project of protobu.
In the last step, you can directly click
Open Project
the 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\build
the directory yourself toVS2017
openprotobuf.sln
the file.
3. vs compile protobuf project
libprotobuf
Compile 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 libprotobuf
and protoc
two projects can already meet the core needs.
- Take debug as an example, generate the following files: libprotobufd.lib, libprotocd.lib and protoc.exe
The file generated by the debug version is as follows
- Taking release as an example, generate the following files: libprotobuf.lib, libprotoc.lib and protoc.exe
The files generated by the release version are as follows
4. Write the project's own .proto file
.proto
For 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. proto2
Please 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
helloworld
message type with a message name of , which has 2 members, the id of type int32, and the member str of type string.
Use protoc.exe
to process the message and generate a C++ compiler-recognizable .h
and .cc
file
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 --help
the command to view the help document.
You can refer to the figure below:
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.exe
files 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\src
under the source code to the folder of the helloworld projectgoogle
include
Text description:
- In the VS project, configure the following project properties:
[配置属性]->[C/C++]->[常规]->[附加包含目录]
: Add
[配置属性]->[连接器]->[常规]->[附加库目录]
: Addlibprotobufd.lib
andlibprotocd.lib
the path where the file is located
[配置属性]->[连接器]->[输入]->[附加依赖项]
: addlibprotobufd.lib
andlibprotocd.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
The directory after copying the files is:
solution
Modify additional include directories
Modify the add-on library directory
Modify additional dependencies
// 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"><<</span> <span class="token string">"id:"</span> <span class="token operator"><<</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"><<</span> std<span class="token operator">::</span>endl<span class="token punctuation">;</span>
std<span class="token operator">::</span>cout <span class="token operator"><<</span> <span class="token string">"str:"</span> <span class="token operator"><<</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"><<</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.sln
is caused by the inconsistency between the runtime library in the project properties of the next compilation libprotobuf
and 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]
libprotobuf
And protoc
the default runtime library of the project's project properties is:
Debug : multi-threaded debugging (/MTd)
Release: multi-threaded (/MT)
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