protobuf的测试文件,test.proto:
syntax = "proto3";
package mytest;
message LoginRequest
{
string name=1;
string pwd=2;
}
message LoginResponse
{
int32 errcode=1;
string errmsg=2;
bool success=3;
}
使用protoc编译,protoc test.proto --cpp_out=./
生成test.pb.h和test.pb.cc。
测试代码
main.cc
#include "test.pb.h"
#include <iostream>
#include <string>
using namespace mytest;
int main()
{
LoginRequest req;
req.set_name("xiaoxie");
req.set_pwd("123456");
std::string send_str;
if (req.SerializeToString(&send_str))
{
std::cout << send_str.c_str() << std::endl;
}
return 0;
}
使用g++进行编译,库的顺序非常重要。
g++ -o main288 main.cc test.pb.cc -lprotobuf \
-labsl_log_internal_check_op \
-labsl_log_internal_conditions \
-labsl_log_internal_message \
-labsl_log_internal_nullguard \
-labsl_examine_stack \
-labsl_log_internal_format \
-labsl_log_internal_proto \
-labsl_log_internal_log_sink_set \
-labsl_log_sink \
-labsl_log_initialize \
-labsl_log_internal_globals \
-labsl_log_globals \
-labsl_raw_hash_set \
-labsl_hash \
-labsl_city \
-labsl_low_level_hash \
-labsl_statusor \
-labsl_status \
-labsl_cord \
-labsl_cordz_info \
-labsl_cord_internal \
-labsl_cordz_functions \
-labsl_exponential_biased \
-labsl_cordz_handle \
-labsl_crc_cord_state \
-labsl_crc32c \
-labsl_crc_internal \
-labsl_strerror \
-labsl_str_format_internal \
-labsl_synchronization \
-labsl_stacktrace \
-labsl_symbolize \
-labsl_debugging_internal \
-labsl_demangle_internal \
-labsl_graphcycles_internal \
-labsl_kernel_timeout_internal \
-labsl_malloc_internal \
-labsl_time \
-labsl_time_zone \
-lutf8_validity \
-labsl_strings \
-labsl_int128 \
-labsl_strings_internal \
-labsl_base \
-labsl_spinlock_wait \
-labsl_throw_delegate \
-labsl_raw_logging_internal \
-labsl_log_severity