在 ubuntu 上编译 protobuf 客户端代码,但未找到包含文件

compile protobuf client code on ubuntu, but include file is not found

我刚刚在我的 ubuntu1604 上安装了 google protocol buffer:

sudo apt install protobuf-compiler

并尝试了一个快速测试,1 个 proto 文件,1 个 cpp 文件来使用它,尝试查看 encode/decode 结果:

$ cat 1.proto
package x;
message my{
required string name=1;
required int32 id=2;
optional string email=3;
}

$ cat 1.cpp
#include"1.pb.cc"
#include<string>
#include<iostream>
using namespace std;
using namespace x;
int main()
{
  my p;
  p.set_name("tom");
  p.set_id(18);
  p.set_email("aa@bb.com");
  string s;
  my.SerializeToString(&s);
  return 0;
}

我尝试编译它,找不到包含文件:

protoc 1.proto --cpp_out=./
g++ 1.cpp 1.pb.cc -lprotobuf

In file included from 1.pb.cc:5:0,
             from 1.cpp:1:
1.pb.h:9:42: fatal error: google/protobuf/stubs/common.h: No such file or directory
compilation terminated.
In file included from 1.pb.cc:5:0:
1.pb.h:9:42: fatal error: google/protobuf/stubs/common.h: No such file or directory
compilation terminated.

您没有安装关联的 protobuf header 和独立于 protobuf-compiler 的库。 运行: sudo apt install libprotobuf-dev