-1
我想使用谷歌協議緩衝在C++ Ubuntu上在第一步驟i創建通過調用.proto文件谷歌協議緩衝液C++上ubutu
package business;
message Employee
{
required string first_name = 1;
required string last_name = 2;
required string email = 3;
}
message Company
{
required string name = 1;
optional string url = 2;
repeated Employee employee = 3;
}
我可以很容易地轉換爲C++數據訪問類:
protoc -I=. --cpp_out=. business.proto
這一步protoc後創建到文件
business.pb.h
business.pb.cc
當我要編譯這段代碼我看到錯誤
#include <iostream>
#include <fstream>
#include "business.pb.h"
using namespace std;
/// Saves a demo company object to 'company.bin'.
void save()
{
business::Company company;
company.set_name("Example Ltd.");
company.set_url("http://www.example.com");
// 1st employee
{
business::Employee *employee = company.add_employee();
employee->set_first_name("John");
employee->set_last_name("Doe");
employee->set_email("[email protected]");
}
// 2nd employee
{
business::Employee *employee = company.add_employee();
employee->set_first_name("Jane");
employee->set_last_name("Roe");
employee->set_email("[email protected]");
}
fstream output("company.bin", ios::out | ios::trunc | ios::binary);
company.SerializeToOstream(&output);
}
/// Loads a demo company object from 'company.bin' and dumps its data.
void load()
{
business::Company company;
fstream input("company.bin", ios::in | ios::binary);
company.ParseFromIstream(&input);
cout << "Company: " << company.name() << "\n";
cout << "URL: " << (company.has_url() ? company.url() : "N/A") << "\n";
cout << "\nEmployees: \n\n";
for(int i = 0, n = company.employee_size(); i < n; ++i)
{
const business::Employee &employee = company.employee(i);
cout << "First name: " << employee.first_name() << "\n";
cout << "Last name: " << employee.last_name() << "\n";
cout << "Email: " << employee.email() << "\n";
cout << "\n";
}
}
int main()
{
save();
load();
return 0;
}
用於編譯我用這個命令
g++ p1.cpp business.pb.cc `pkg-config --cflags --libs protobuf`
,但我看到這個錯誤 http://i.stack.imgur.com/soQ3Z.png
請把它變成SSCCE,*請*提供輸出*文本*。 –
如何將其變爲SSCCE? –