Linux installation Protobuf Tutorial + example (verbose)

Source: Internet
Author: User

(. 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

    1. Package LM;
    2. Message HelloWorld
    3. {
    4. Required Int32 id = 1; Id
    5. Required String str = 2; Str
    6. Optional int32 opt = 3; Optional field
    7. }
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
  1. #include "Msg.pb.h"
  2. #include <fstream>
  3. #include <iostream>
  4. using namespace Std;
  5. int main (void)
  6. {
  7. Lm::helloworld MSG1;
  8. MSG1.SET_ID (101);
  9. Msg1.set_str ("Hello");
  10. FStream output ("./log", Ios::out | ios::trunc | ios::binary);
  11. if (!MSG1. Serializetoostream (&output)) {
  12. Cerr << "Failed to write Msg." << Endl;
  13. return-1;
  14. }
  15. return 0;
  16. }
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
  1. #include "Msg.pb.h"
  2. #include <fstream>
  3. #include <iostream>
  4. using namespace Std;
  5. void listmsg (const Lm::helloworld & msg) {
  6. cout << msg.id () << Endl;
  7. cout << msg.str () << Endl;
  8. }
  9. int main (int argc, char* argv[]) {
  10. Lm::helloworld MSG1;
  11. {
  12. FStream input ("./log", Ios::in | ios::binary);
  13. if (!MSG1. Parsefromistream (&input)) {
  14. Cerr << "Failed to the parse address Book." << Endl;
  15. return-1;
  16. }
  17. }
  18. Listmsg (MSG1);
  19. }
Compile: g++ msg.pb.cc reader.cc-o reader ' pkg-config--cflags--libs protobuf '-lpthread execute./reader output: 101hello

8> Write Makefile file

      1. All:write Reader
      2. Clean
      3. Rm-f Write reader msg.*.cc msg.*.h *.O Log
      4. Proto_msg:
      5. Protoc--cpp_out=. Msg.proto
      6. write:msg.pb.cc write.cc
      7. g++ msg.pb.cc write.cc-o write ' pkg-config--cflags--libs protobuf '
      8. reader:msg.pb.cc reader.cc
      9. g++ msg.pb.cc reader.cc-o reader ' pkg-config--cflags--libs protobuf '

Linux installation Protobuf Tutorial + example (verbose)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.