google proto buffer
学习资料
(3条消息) GOOGLE PROTOBUF开发者指南_weixin_33892359的博客-CSDN博客
(1条消息) protobuf基本用法详解_langzi989的专栏-CSDN博客_protobuf使用详解
C++中protobuf 的交叉编译使用详解
https://www.jb51.net/article/254913.htm
编译方法:
#修改的文件
3rdparty/protobuf-v3.14.0/cmake/CMakeLists.txt
#ARM,交叉编译
SET(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_SYSTEM_PROCESSOR arm)
SET(TOOLCHAIN_DIR "/home/rk/workdir/rock/rv1126_rv1109_linux_sdk_v1.8.0_20210224/prebuilts/gcc/linux-x86/arm/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf")
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
SET(CMAKE_C_COMPILER ${TOOLCHAIN_DIR}/bin/arm-linux-gnueabihf-gcc)
SET(CMAKE_CXX_COMPILER ${TOOLCHAIN_DIR}/bin/arm-linux-gnueabihf-g++)
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fno-aggressive-loop-optimizations -ffunction-sections")
#set(CMAKE_CXX_STANDARD 11)
#set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_definitions(-D_GLIBCXX_USE_C99=1)
//动态编译
cmake -Dprotobuf_BUILD_TESTS=OFF -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../../../../install ../..
实验单个结构体信息
//system.proto
syntax = "proto3";message LoginParam{
string strUserName = 1;
string strPasswd = 2;
}
#include <signal.h>
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
#include <memory>
#include "system.pb.h"
using namespace std;
void print_msg(const LoginParam& msg)
{
std::cout << msg.strusername() << std::endl;
std::cout << msg.strpasswd() << std::endl;
}
int test_read( )
{
//清单 3. Reader
LoginParam msg;
{
std::fstream input("./log", ios::in | ios::binary);
if (!msg.ParseFromIstream(&input)) //解析stream -> msg
{
cerr << "Failed to parse address book." << endl;
return -1;
}
}
print_msg(msg);
return 0;
}
int test_write( )
{
LoginParam msg;
msg.set_strusername("username");
msg.set_strpasswd("passwd");
// Write the new address book back to disk.
std::fstream output("./log", ios::out | ios::trunc | ios::binary);
if (!msg.SerializeToOstream(&output)) //编码msg ->stream
{
cerr << "Failed to write msg." << endl;
return -1;
}
return 0;
}
int main()
{
test_write( );
test_read( );
return 0;
}