Installation and use of Google protobuf

Source: Internet
Author: User

[CPP]View Plaincopy
  1. More recently, the Protobuf interface has been provided for the needs of the work, and the processes and methods used are summarized as follows:
  2. Download protobuf-2.3.0:
  3. http://protobuf.googlecode.com/files/protobuf-2.3.0.zip
  4. Installation:
  5. Unzip Protobuf-2.3.0.zip
  6. CD protobuf-2.3.0
  7. ./configure
  8. Make
  9. Make check
  10. Make install
  11. Results:
  12. Libraries has been installed in:
  13. /usr/local/lib
  14. Head files Hava been installed in:
  15. /usr/local/include/google/
  16. protobuf/
  17. Start writing. proto file:
  18. Basemessage.proto:
  19. Message Messagebase
  20. {
  21. required Int32 opcode = 1;
  22. //Other:sendmgrid, Sendid, Recvmgrid, Recvid, ...
  23. }
  24. Message Basemessage
  25. {
  26. Required Messagebase msgbase = 1;
  27. }
  28. Basemessage.proto is the basis for other message proto files, taking the c2s_getcontainerinfo of the container module as an example:
  29. Containermessage.proto:
  30. Import "Basemessage.proto";
  31. Message c2sgetcontainerinfomsg
  32. {
  33. Required Messagebase msgbase = 1;
  34. Optional Int32 containertype = 2;
  35. }
  36. . proto File Authoring rules:
  37. 1) All messages need to contain the msgbase, and the number is 1, namely:
  38. Required Messagebase msgbase = 1;
  39. 2) In addition to msgbase this written required, all other items are written in optional.
  40. Compiling the. Proto file
  41. Protoc-i=. --cpp_out=. Basemessage.proto
  42. Protoc-i=. --cpp_out=. Containermessage.proto
  43. Generate Basemessage.pb.h, BaseMessage.pb.cc
  44. Containermessage.pb.h, ContainerMessage.pb.cc
  45. Add them to the project file.
  46. Write C + + code:
  47. 1) Send message:
  48. C2sgetcontainerinfomsg msg;
  49. Msg.mutable_msgbase ()->set_opcode (c2s_getcontainerinfo);
  50. Msg.set_containertype (1);
  51. std::string out = Msg. Serializeasstring ();
  52. Send (SOCKFD, Out.c_str (), Out.size (), 0);
  53. 2) Receive messages
  54. Char buf[maxbuf + 1];
  55. int Len;
  56. Bzero (buf, maxbuf + 1);
  57. Len = recv (new_fd, buf, maxbuf, 0);
  58. if (len > 0)
  59. {
  60. printf ("%d received message succeeded: '%s ',%d bytes of data/n",
  61. NEW_FD, buf, Len);
  62. Basemessage basemsg;
  63. std::string data = BUF;
  64. basemsg.parsefromstring (data);
  65. int opcode = Basemsg.mutable_msgbase ()->opcode ();
  66. printf ("opcode=%d/n", opcode);
  67. switch (opcode)
  68. {
  69. Case C2s_getcontainerinfo:
  70. {
  71. C2sgetcontainerinfomsg msg;
  72. Msg. parsefromstring (data);
  73. printf ("containertype=%d/n", Msg.containertype ());
  74. Break ;
  75. }
  76. Default:
  77. {
  78. Break ;
  79. }
  80. }
  81. }
  82. Else
  83. {
  84. if (Len < 0)
  85. printf ("message received failed!") The error code is%d, and the error message is '%s '/n ',
  86. errno, Strerror (errno));
  87. Close (NEW_FD);
  88. return-1;
  89. }
  90. Compiling C + + code:
  91. Need to link lib:
  92. Protobuf
  93. Pthread
  94. Reference:
  95. 1,http://www.360doc.com/content/10/0822/16/11586_47942017.shtml
  96. 2,http://code.google.com/p/protobuf/

Original address: http://blog.csdn.net/ganghust/article/details/6115283

Project home: http://code.google.com/p/protobuf/

Download: Http://code.google.com/p/protobuf/downloads/list protobuf-2.4.1.tar.gz

1,./configure (Note: The default may be installed in the/usr/local directory, you can add--PREFIX=/USR to specify the installation to/usr/lib, you can eliminate the path settings, the path settings see Linux command Pkg-config)

2. Make

3. Make check

4. Make install (requires Superuser root privileges)

Installation and use of Google protobuf

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.