要测试Ubuntu上安装的protobuf,您可以使用以下步骤:
syntax = "proto3";
message TestMessage {
string id = 1;
string name = 2;
}
protoc --cpp_out=. test.proto
#include <iostream>
#include <fstream>
#include "test.pb.h"
int main() {
TestMessage test_message;
test_message.set_id("1");
test_message.set_name("Test");
std::ofstream output("test.bin", std::ios::out | std::ios::binary);
test_message.SerializeToOstream(&output);
output.close();
TestMessage new_test_message;
std::ifstream input("test.bin", std::ios::in | std::ios::binary);
new_test_message.ParseFromIstream(&input);
input.close();
std::cout << "Id: " << new_test_message.id() << std::endl;
std::cout << "Name: " << new_test_message.name() << std::endl;
return 0;
}
g++ test.cpp test.pb.cc -o test -lprotobuf
./test
如果您能够成功编译和执行程序,并输出正确的消息内容,则表示您已成功在Ubuntu上安装和测试protobuf。