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

Hin*_*sum 3 c++ linux compilation include protocol-buffers

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

sudo apt install protobuf-compiler
Run Code Online (Sandbox Code Playgroud)

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

$ 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;
}
Run Code Online (Sandbox Code Playgroud)

我试图编译它,未能找到包含文件:

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.
Run Code Online (Sandbox Code Playgroud)

xos*_*tom 6

您没有安装相关的 protobuf 头文件和库,它们独立于 protobuf-compiler。跑: sudo apt install libprotobuf-dev

  • 虽然此代码片段可能会解决问题,包括对 *如何* 和 *为什么* 的解释,这会解决问题 [真的有帮助](//meta.stackexchange.com/q/114762) 以提高帖子的质量。请记住,您是在为未来的读者回答问题,而不仅仅是现在问的人!请[编辑]您的答案以添加解释,并说明适用的限制和假设。 (2认同)
  • 这个丢失的包正是我所需要的。谢谢! (2认同)