{"id":2318,"date":"2018-11-29T22:51:04","date_gmt":"2018-11-29T14:51:04","guid":{"rendered":"https:\/\/damogame.cn:15443\/wordpress\/?p=2318"},"modified":"2018-11-30T14:08:03","modified_gmt":"2018-11-30T06:08:03","slug":"protobuf-c-c%e5%ae%9e%e6%88%98%e7%ac%94%e8%ae%b01","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/2318","title":{"rendered":"Protobuf C\/C++\u5b9e\u6218\u7b14\u8bb0(1)"},"content":{"rendered":"<div class=\"postText\">\n<div id=\"cnblogs_post_body\" class=\"blogpost-body\">\n<p>\u524d\u8a00:<br \/>\nProtobuf\u4f5c\u4e3a\u6570\u636e\u4ea4\u6362\u683c\u5f0f, \u88ab\u5f88\u591a\u4eba\u559c\u6b22. \u6570\u636e\u538b\u7f29\u6bd4\u9ad8, \u5411\u540e\u517c\u5bb9\u6027\u5f3a, \u6027\u80fd\u4f18\u5f02, \u800c\u4e14\u5bf9\u5e73\u53f0\u4e2d\u6027, \u652f\u6301\u591a\u8bed\u8a00(C\/C++, JAVA, Python). \u4f18\u70b9\u592a\u591a, \u5b9e\u5728\u4e0d\u80dc\u679a\u4e3e(\u5c45\u5bb6\u65c5\u884c, \u6740\u4eba\u653e\u706b\u5fc5\u5907\u826f\u836f, oh yeah! ^_^).<br \/>\n\u672c\u7bc7\u6587\u7ae0\u7740\u91cd\u8bb0\u5f55Linux\u4e0b\u5bf9C\/C++\u7248Protobuf\u7684\u7f16\u8bd1\/\u94fe\u63a5\u548cAPI\u4f7f\u7528.<\/p>\n<p><strong>Protobuf\u4e0b\u8f7d\u548c\u5b89\u88c5<\/strong><br \/>\n\u8ba9\u6211\u4eec\u4f7f\u7528protobuf 3.6.1\u4f5c\u4e3a\u6837\u4f8b\u6765\u5c55\u793a.<\/p>\n<p><a href=\"https:\/\/github.com\/protocolbuffers\/protobuf\/releases\">https:\/\/github.com\/protocolbuffers\/protobuf\/releases<\/a><br \/>\n\u4e0b\u8f7d\u94fe\u63a5:\u3000<a href=\"https:\/\/github.com\/protocolbuffers\/protobuf\/releases\/download\/v3.6.1\/protobuf-all-3.6.1.tar.gz\">https:\/\/github.com\/protocolbuffers\/protobuf\/releases\/download\/v3.6.1\/protobuf-all-3.6.1.tar.gz<\/a><br \/>\n\u4ee5\u4e0b\u662f\u76f8\u5173\u7684\u547d\u4ee4\u548c\u64cd\u4f5c<br \/>\n1).\u00a0<strong>\u4e0b\u8f7d\u548c\u89e3\u538b<\/strong><br \/>\nwget https:\/\/github.com\/protocolbuffers\/protobuf\/releases\/download\/v3.6.1\/protobuf-all-3.6.1.tar.gz<br \/>\ntar -jxvf protobuf-all-3.6.1.tar.gz<br \/>\n2).\u00a0<strong>\u7f16\u8bd1\u548c\u5b89\u88c5<\/strong><br \/>\n.\/configure &#8211;disable-shared<br \/>\nmake &amp;&amp; make install<br \/>\n3).\u00a0<strong>\u76ee\u5f55\u7ed3\u6784<\/strong><br \/>\ntree -L 2 # \u4e24\u5c42\u7684\u76ee\u5f55\u7ed3\u6784(bin\/inculde\/lib), \u5982\u4e0b\u6240\u793a:<br \/>\n<img decoding=\"async\" src=\"https:\/\/images0.cnblogs.com\/blog\/305284\/201409\/261639264677136.png\" alt=\"\" \/><br \/>\n\u8bc4\u6ce8: bin\/protoc \u662fpb\u751f\u6210\u5de5\u5177, include, lib\u5219\u662f\u5bf9\u5e94\u7684\u5934\u6587\u4ef6\u548c\u76f8\u5e94\u7684\u9759\u6001\/\u52a8\u6001\u5e93<\/p>\n<p><strong>\u5b9e\u6218\u6f14\u793a<\/strong><br \/>\n\u8ba9\u6211\u4eec\u6765\u7f16\u8f91\u4e00\u4e0bmsg.proto\u6587\u4ef6<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">message msg_t {\r\n\u3000\u3000required int32 id = 1;\r\n}<\/pre>\n<p>\u8bc4\u6ce8: \u7b80\u5355\u5b9a\u4e49\u4e86msg_t\u7c7b<br \/>\n<strong>\u501f\u52a9protoc\u6765\u751f\u6210\u76f8\u5e94\u8bed\u8a00\u7248\u672c\u7684\u5e8f\u5217\/\u53cd\u5e8f\u5217\u4ee3\u7801<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">protoc --cpp_out=.\/ msg.proto<\/pre>\n<p>\u8bc4\u6ce8: &#8211;cpp_out\u6307\u5b9a\u4e86c\/c++\u7248\u672c\u4ee3\u7801\u7684\u8f93\u51fa\u8def\u5f84<br \/>\n\u6700\u7ec8\u751f\u6210 msg.pb.cc msg.pb.h \u4e24\u6587\u4ef6<br \/>\n\u7f16\u5199\u5982\u4e0b\u6d4b\u8bd5\u4ee3\u7801:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">#include \"msg.pb.h\"\r\n#include &lt;stdio.h&gt;\r\n#include &lt;assert.h&gt;\r\n\r\nint\u00a0main() {\r\n\u3000\u3000char\u00a0buf[1024] = {'\\0'};\r\n\u3000\u3000int\u00a0buf_len = 0;\r\n\r\n\u3000\u3000msg_t msg1;\r\n\u3000\u3000msg1.set_id(1001);\r\n\r\n\u3000\u3000\/\/ *) serialize phrase =&gt; object to byte array\r\n\u3000\u3000msg1.SerializeToArray(buf,\u00a0sizeof(buf));\r\n\u3000\u3000buf_len = msg1.ByteSize();\r\n\r\n\u3000\u3000msg_t msg2;\r\n\u3000\u3000\/\/ *) deserialize phrase =&gt; byte array to object\r\n\u3000\u3000msg2.ParseFromArray(buf, buf_len);\r\n\r\n\u3000\u3000assert(msg1.id() == msg2.id());\r\n\u3000\u3000return\u00a00;\r\n}<\/pre>\n<div class=\"cnblogs_Highlighter sh-gutter\">\n<div>\n<div id=\"highlighter_132162\" class=\"syntaxhighlighter cpp\"><\/div>\n<\/div>\n<\/div>\n<p>\u8fdb\u884c\u7f16\u8bd1\u5e76\u8fd0\u884c<br \/>\ng++ -o app app.cpp msg.pb.cc -I\/path\/to\/protobuf\/include -L\/path\/to\/protobuf\/lib -lprotobuf -lpthread<br \/>\n.\/app<br \/>\n\u8bc4\u6ce8: \/path\/to\u4e3a\u5177\u4f53protobuf\u7684\u5b89\u88c5\u76ee\u5f55<\/p>\n<p><strong>\u94fe\u63a5\u65b9\u5f0f<\/strong><br \/>\n<strong>\u9759\u6001\u94fe\u63a5<\/strong>\u8fd8\u662f<strong>\u52a8\u6001\u94fe\u63a5<\/strong>? \u8fd9\u662f\u4e2a\u95ee\u9898!<br \/>\n\u5728\u6307\u5b9a\u7684protobuf\u5e93\u8def\u5f84\u4e2d, \u5982\u679c\u5b58\u5728\u52a8\u6001\u8fde\u63a5\u5e93, \u5219\u7f16\u8bd1\u7684\u7a0b\u5e8f<strong>\u4f18\u5148\u9009\u62e9\u52a8\u6001\u94fe\u63a5<\/strong>, \u5426\u5219\u5219<strong>\u91c7\u7528\u9759\u6001\u94fe\u63a5\u7684\u65b9\u5f0f<\/strong>.<br \/>\n\u8ba9\u6211\u4eec\u7528\u56fe\u6765\u5bf9\u6bd4\u8bf4\u660e<br \/>\n<strong>\u52a8\u6001\u94fe\u63a5\u65b9\u5f0f<\/strong><br \/>\n\u5728protobuf\u7684lib\u76ee\u5f55\u4e2d, \u82e5\u5b58\u5728\u52a8\u6001\u8fde\u63a5\u5e93(so\u6587\u4ef6)<br \/>\n<img decoding=\"async\" src=\"https:\/\/images0.cnblogs.com\/blog\/305284\/201409\/261647174209215.png\" alt=\"\" \/><br \/>\n\u5219\u7f16\u8bd1\u540e\u7684app\u53ef\u6267\u884c\u7a0b\u5e8f<br \/>\n\u4f7f\u7528ldd app\u5206\u6790, \u5b58\u5728\u5982\u4e0b\u4f9d\u8d56\u9879<br \/>\n<img decoding=\"async\" src=\"https:\/\/images0.cnblogs.com\/blog\/305284\/201409\/261647290604238.png\" alt=\"\" \/><br \/>\n\u8bc4\u6ce8: \u7ea2\u7ebf\u533a\u57df\u6807\u660e\u4e86\u5f15\u7528\u4e86<strong>\u52a8\u6001\u8fde\u63a5\u5e93libprotobuf.so.7<\/strong><br \/>\n\u76f4\u63a5\u6267\u884c\u4e8c\u8fdb\u5236app\u6587\u4ef6, \u9047\u5230\u5982\u4e0b\u9519\u8bef<\/p>\n<div class=\"cnblogs_Highlighter sh-gutter\">\n<div>\n<div id=\"highlighter_158963\" class=\"syntaxhighlighter cpp\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"cpp plain\">.\/app: error\u00a0<\/code><code class=\"cpp keyword bold\">while<\/code>\u00a0<code class=\"cpp plain\">loading shared libraries: libprotobuf.so.7: cannot open shared object file: No such file or directory<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<\/div>\n<p>\u663e\u7136\u8fd9\u8fb9\u9700\u8981\u8bbe\u5b9a<strong>LD_LIBRARY_PATH\u53d8\u91cf<\/strong><\/p>\n<div class=\"cnblogs_Highlighter sh-gutter\">\n<div>\n<div id=\"highlighter_902460\" class=\"syntaxhighlighter cpp\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"cpp plain\">export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:\/path\/to\/protobuf\/lib<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<\/div>\n<p>\u8bc4\u6ce8: \/path\/to\/protobuf\/lib\u4e3a\u5b9e\u9645\u7684protobuf\u5b89\u88c5\u8def\u5f84<br \/>\n<strong>\u9759\u6001\u94fe\u63a5\u65b9\u5f0f<\/strong><br \/>\n\u7b80\u5355\u7684\u5728lib\u76ee\u5f55\u79fb\u9664\u6240\u6709\u52a8\u6001\u8fde\u63a5\u5e93(so)\u6587\u4ef6,<br \/>\n\u7136\u540e\u8fdb\u884c\u7f16\u8bd1, \u4f7f\u7528<strong>ldd\u5206\u6790<\/strong><br \/>\n\u76f4\u63a5\u6267\u884capp\u5c31\u53ef\u4ee5\u4e86<\/p>\n<p><strong>\u603b\u7ed3:<\/strong><br \/>\n\u8fd9\u8fb9\u4e3b\u8981\u8bb2\u8ff0\u4e86protobuf\u7684\u7f16\u8bd1\/\u5b89\u88c5, \u4ee5\u53ca\u5c0fdemo\u7f16\u5199, \u91cd\u8981\u7684\u8bb2\u8ff0\u4e86\u9759\u6001\u94fe\u63a5\u548c\u52a8\u6001\u94fe\u63a5\u7684\u533a\u522b. \u7f51\u4e0a\u8d44\u6599\u591a\u4ee5\u52a8\u6001\u94fe\u63a5\u5c45\u591a, \u4f46\u5b9e\u9645\u4e0a\u9759\u6001\u94fe\u63a5\u7684\u65b9\u5f0f\u7684\u9700\u8981\u66f4\u76f4\u63a5\u4e9b.<\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00: Protobuf\u4f5c\u4e3a\u6570\u636e\u4ea4\u6362\u683c\u5f0f<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[],"tags":[118],"class_list":["post-2318","post","type-post","status-publish","format-standard","hentry","tag-02-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/2318","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=2318"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/2318\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=2318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=2318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=2318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}