(. pb.h:9:42:fatal error:google/protobuf/stubs/common.h:no such file or directory
Look at this, you should know that the header file is not found, then you can use the-i parameter of g++:
-i/usr/local/lib/protobuf/include to command g++ to find the header file under/usr/local/lib/protobuf
The above/usr/local/lib/protobuf/is the installation address of my protobuf, please replace it with your
) 1 You can download the PROTOBUF source code on the website http://code.google.com/p/protobuf/downloads/list. Then unzip the installation to use it. The installation steps are as follows: Tar-xzf protobuf-2.1.0.tar.gz CD protobuf-2.1.0./configure--prefix=/usr/local/protobuf make make check ma Ke install 2 > sudo vim/etc/profile add export path= $PATH:/usr/local/protobuf/bin/export pkg_config_path=/usr/local/p rotobuf/lib/pkgconfig/Save Execution Source/etc/profile
Also add the above two lines of code in ~/.profile, otherwise the login user cannot find the PROTOC command
3 > Configuring the Dynamic link library path sudo vim/etc/ld.so.conf insert:/usr/local/protobuf/lib
4 > Su #root permissions ldconfig
5> Write message file: Msg.proto
- Package LM;
- Message HelloWorld
- {
- Required Int32 id = 1; Id
- Required String str = 2; Str
- Optional int32 opt = 3; Optional field
- }
Map the message file Msg.proto to the CPP file protoc-i=. --cpp_out=. Msg.proto can see that msg.pb.h and msg.pb.cc are generated.
6> the process of writing serialized messages write.cc
- #include "Msg.pb.h"
- #include <fstream>
- #include <iostream>
- using namespace Std;
- int main (void)
- {
- Lm::helloworld MSG1;
- MSG1.SET_ID (101);
- Msg1.set_str ("Hello");
- FStream output ("./log", Ios::out | ios::trunc | ios::binary);
- if (!MSG1. Serializetoostream (&output)) {
- Cerr << "Failed to write Msg." << Endl;
- return-1;
- }
- return 0;
- }
Compile write.cc g++ msg.pb.cc write.cc-o write ' pkg-config--cflags--libs protobuf '-lpthread execute write./write, you can see that the log was generated File
7> writes the deserialization process reader.cc
- #include "Msg.pb.h"
- #include <fstream>
- #include <iostream>
- using namespace Std;
- void listmsg (const Lm::helloworld & msg) {
- cout << msg.id () << Endl;
- cout << msg.str () << Endl;
- }
- int main (int argc, char* argv[]) {
- Lm::helloworld MSG1;
- {
- FStream input ("./log", Ios::in | ios::binary);
- if (!MSG1. Parsefromistream (&input)) {
- Cerr << "Failed to the parse address Book." << Endl;
- return-1;
- }
- }
- Listmsg (MSG1);
- }
Compile: g++ msg.pb.cc reader.cc-o reader ' pkg-config--cflags--libs protobuf '-lpthread execute./reader output: 101hello
8> Write Makefile file
- All:write Reader
- Clean
- Rm-f Write reader msg.*.cc msg.*.h *.O Log
- Proto_msg:
- Protoc--cpp_out=. Msg.proto
- write:msg.pb.cc write.cc
- g++ msg.pb.cc write.cc-o write ' pkg-config--cflags--libs protobuf '
- reader:msg.pb.cc reader.cc
- g++ msg.pb.cc reader.cc-o reader ' pkg-config--cflags--libs protobuf '
Linux installation Protobuf Tutorial + example (verbose)