要測試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。