3

I just installed google protocol buffer on my ubuntu1604:

sudo apt install protobuf-compiler

And tried a quick test, 1 proto file, 1 cpp file to use it, try to see the encode/decode results:

$ 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;
}

I tried to compile it, failed to find include files:

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.
Hind Forsum
  • 9,717
  • 13
  • 63
  • 119

2 Answers2

6

You did not install associated protobuf header and lib, which is independent from protobuf-compiler. Run: sudo apt install libprotobuf-dev

xosp7tom
  • 2,131
  • 1
  • 17
  • 26
  • 2
    While this code snippet may solve the question, including an explanation of *how* and *why* this solves the problem [would really help](//meta.stackexchange.com/q/114762) to improve the quality of your post. Remember that you are answering the question for readers in the future, not just the person asking now! Please [edit] your answer to add explanation, and give an indication of what limitations and assumptions apply. – Toby Speight Jan 12 '17 at 09:36
  • 2
    This missing package is what I needed. Thanks! – Hind Forsum Jan 12 '17 at 15:11
  • 1
    This was also the solution for "google/protobuf/any.proto: File not found." when compiling a python protobuf on ubuntu. Commenting in case it helps others find this answer. – knuckles Apr 22 '20 at 14:44
0

When you are in Linux environment, you can:

  1. download directly any version from protocol buffer github release website
  2. Unzip the package
  3. Move protoc-version-arch/include/google to /usr/local/include or other similar place
hang cui
  • 1
  • 1