Download source code
The protobuf version used by the author is protobuf-cpp-3.0.0-alpha-1. Download link
Compile the source code
Unzip the source code package.
The directory structure is shown above. Enter the vsprojects directory. Open the protobuf.sln project file. The project structure is shown in the figure below
Compile the three projects libprotobuf, libprotoc and protoc.
First compile libprotobuf. Two changes are needed.
1. Right-click the project properties. Add the _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS macro definition in the column of the preprocessor definition in the C/C++ preprocessor .
2. Choose the compiled version according to your needs.
Regarding the version of the runtime library. You can refer to https://blog.csdn.net/qq_33757398/article/details/82156956 this blog link.
Then compiling l ibprotoc also needs to make the above two changes.
After the compilation is complete. Also in the vsprojects directory. The X64 directory will appear. <font color = red>Because the author uses x64 compilation. So it appears in the x64 directory</font>
If you use win32 to compile. Then it should be in the Debug directory. As follows.
Enter the Debug directory in X64. Mainly find three files.
libprotobuf.lib libprotoc.lib protoc.exe
If it compiles successfully. There should be these three files in the directory. At this point, the compilation of protobuf has been completed,
Use of protobuf
Create a new Test.proto file in your project directory . The contents of the file are as follows:
syntax = "proto3";
package Test;
message UserAccount {
string usrName = 2;
string userPassword = 3;
}
message User {
User user = 1;
}
Then start cmd and switch the directory to your project directory. Then copy the three files just now to the project directory.
Then enter the cmd command
protoc --cpp_out=./ Test.proto //Note that there is a space between ./ and the file name
Press Enter to run. The Test.pb.h Test.pb.cc file is generated in the directory . Then add them to the project.
Then copy the header file of protobuf.
As shown in the figure above, the google directory in the src directory in the unzipped directory. Copy the protobuf directory to the project directory . Then add the header file under the include directory. Then link libprotobuf.lib to the project directory , as shown in the figure below
After completing the above several parts.
#include <QtWidgets/QApplication>
#include <string>
#include <iostream>
#include "Test.pb.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Test::UserAccount account1;
account1.set_usrname("testUser");
account1.set_userpassword("testUserPassWord");
std::string serializeToStr;
account1.SerializeToString(&serializeToStr);
std::cout << "Serialization:" << std::endl;
std::cout << serializeToStr << std::endl;
Test::UserAccount account2;
if (!account2.ParseFromString(serializeToStr))
{
std::cerr << "failed to parse student." << std::endl;
return -1;
}
std::cout << "Deserialization:" << std::endl;
std::cout << account2.usrname() << std::endl;
std::cout << account2.userpassword() << std::endl;
google::protobuf::ShutdownProtobufLibrary();
return a.exec();
}
Then run. Discovery console output
At this point, it's over.