{"id":3917,"date":"2019-10-13T01:30:46","date_gmt":"2019-10-12T17:30:46","guid":{"rendered":"https:\/\/damogame.cn\/wordpress\/?p=3917"},"modified":"2019-10-13T01:30:46","modified_gmt":"2019-10-12T17:30:46","slug":"%e5%9c%a8windows%e4%b8%8b%e4%bd%bf%e7%94%a8protobuf%e7%9a%84%e7%a4%ba%e4%be%8b","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/3917","title":{"rendered":"\u5728Windows\u4e0b\u4f7f\u7528Protobuf\u7684\u793a\u4f8b"},"content":{"rendered":"<p><a href=\"https:\/\/github.com\/stonejiang208\/windows-protobuf\/blob\/master\/win-protobuf.md\">\u539f\u6587\u5730\u5740<\/a><\/p>\n<p>&nbsp;<\/p>\n<h2><a id=\"user-content-\u6458\u8981\" class=\"anchor\" href=\"https:\/\/github.com\/stonejiang208\/windows-protobuf\/blob\/master\/win-protobuf.md#%E6%91%98%E8%A6%81\" aria-hidden=\"true\"><\/a>\u6458\u8981<\/h2>\n<p>Protobuf\u5168\u79f0\u4e3aGoogle ProtoBuf,\u5b83\u662f\u7531Google\u5f00\u6e90\u7684\u9879\u76ee\uff0c\u7c7b\u4f3c\u4e8e\u63a5\u53e3\u5b9a\u4e49\u8bed\u8a00(IDL),\u4f5c\u4e3a\u4e00\u79cd\u6570\u636e\u4ea4\u6362\u683c\u5f0f,\u9002\u7528\u4e8e\u8de8\u5e73\u53f0\u6570\u636e\u901a\u8baf\u65f6\u7684\u6d88\u606f\u7f16\u7801\u548c\u89e3\u7801\u3002\u672c\u6587\u6f14\u793a\u4e86\u5728Windows\u4e0b\u5982\u4f55\u5728C++\u8bed\u8a00\u73af\u5883\u4e0b\u4f7f\u7528Protobuf\u3002\u5173\u952e\u6b65\u9aa4\u4e3a\uff1a<\/p>\n<ol>\n<li>\u5b9a\u4e49\u6570\u636e\u4ea4\u6362\u683c\u5f0f(proto\u6587\u4ef6)<\/li>\n<li>\u5229\u7528protoc\u5b9e\u7528\u5de5\u5177\u628aproto\u6587\u4ef6\u7f16\u8bd1\u6210C++\u5934\u6587\u4ef6\u548c\u5e93\u6587\u4ef6<\/li>\n<li>\u5229\u7528mwc\u6784\u5efa\u5de5\u5177\u628a\u751f\u6210\u7684\u5934\u6587\u4ef6\u548c\u5e93\u6587\u4ef6\u7f16\u8bd1\u6210\u52a8\u6001\u94fe\u63a5\u5e93<\/li>\n<li>\u5728\u9879\u76ee\u5de5\u7a0b\u4e2d\u5f15\u7528\u8fd9\u4e2a\u5e93\u8fdb\u884c\u6d88\u606f\u7f16\u7801\u548c\/\u6216\u89e3\u7801<\/li>\n<\/ol>\n<p>\u5728\u4f7f\u7528\u524d\uff0c\u9700\u8981\u5148\u6784\u5efaprotoc\u5b9e\u7528\u5de5\u5177\u548cprotobuf\u5f00\u53d1\u5e93\u3002<\/p>\n<h2><a id=\"user-content-\u6f14\u793a\u73af\u5883\" class=\"anchor\" href=\"https:\/\/github.com\/stonejiang208\/windows-protobuf\/blob\/master\/win-protobuf.md#%E6%BC%94%E7%A4%BA%E7%8E%AF%E5%A2%83\" aria-hidden=\"true\"><\/a>\u6f14\u793a\u73af\u5883<\/h2>\n<pre><code>* Windows 7\r\n* Visual Studio 2015 \u793e\u533a\u7248\r\n* ACE 6.4.4\r\n* mwc 4.1.25 (in ACE )\r\n* Active Perl 5.22.1\r\n* git 2.9.0 windows.1\r\n* sed 4.2.2 (in git)\r\n* CMake 3.9.1\r\n* Protobuf 3.4.0 (\u5f53\u524d\u6700\u65b0\u53d1\u884c\u7248)\r\n<\/code><\/pre>\n<h2><a id=\"user-content-\u9884\u5907\u73af\u5883\" class=\"anchor\" href=\"https:\/\/github.com\/stonejiang208\/windows-protobuf\/blob\/master\/win-protobuf.md#%E9%A2%84%E5%A4%87%E7%8E%AF%E5%A2%83\" aria-hidden=\"true\"><\/a>\u9884\u5907\u73af\u5883<\/h2>\n<ul>\n<li>\u5b89\u88c5Active Perl\n<p>mwc\u4f9d\u8d56Perl<\/li>\n<li>\u5b89\u88c5 git\n<p>\u5728\u7f16\u8bd1\u5e93\u65f6\uff0c\u9700\u8981\u5229\u7528git\u73af\u5883\u4e0b\u7684sed\u5de5\u5177\uff0c\u4fee\u6539\u751f\u6210\u7684\u5934\u6587\u4ef6.<\/li>\n<li>\u5b89\u88c5 CMake\n<p>\u6784\u5efaprotoc\u5de5\u5177\u53ca\u5f00\u53d1\u5e93\u65f6\u9700\u8981\u7528\u5230CMake\u5de5\u5177\u3002<\/li>\n<li>\u5728Windows\u4e0b\u6b63\u786e\u7f16\u8bd1ACE (x64)<\/li>\n<\/ul>\n<h2><a id=\"user-content-\u4ece\u6e90\u4ee3\u7801\u4e2d\u6784\u5efaprotoc\u53ca\u5f00\u53d1\u5e93\" class=\"anchor\" href=\"https:\/\/github.com\/stonejiang208\/windows-protobuf\/blob\/master\/win-protobuf.md#%E4%BB%8E%E6%BA%90%E4%BB%A3%E7%A0%81%E4%B8%AD%E6%9E%84%E5%BB%BAprotoc%E5%8F%8A%E5%BC%80%E5%8F%91%E5%BA%93\" aria-hidden=\"true\"><\/a>\u4ece\u6e90\u4ee3\u7801\u4e2d\u6784\u5efaprotoc\u53ca\u5f00\u53d1\u5e93<\/h2>\n<h3><a id=\"user-content-\u6b65\u9aa4\" class=\"anchor\" href=\"https:\/\/github.com\/stonejiang208\/windows-protobuf\/blob\/master\/win-protobuf.md#%E6%AD%A5%E9%AA%A4\" aria-hidden=\"true\"><\/a>\u6b65\u9aa4<\/h3>\n<ol>\n<li>\u4ecegithub\u4e0a\u83b7\u5f97protobuf\u7684\u6e90\u4ee3\u7801\n<pre><code>mkdir demo\r\nmkdir build\r\ncd demo\r\ngit clone --recursive -b v3.4.0 https:\/\/github.com\/google\/protobuf.git\r\n<\/code><\/pre>\n<\/li>\n<li>\u5229\u7528CMake\u751f\u6210\u57fa\u4e8eVisual Studio 2015\u7684\u89e3\u51b3\u65b9\u6848\u5de5\u7a0b\u6587\u4ef6\n<pre><code>cd build\r\ncmake -G \"Visual Studio 14 2015 Win64\" -DCMAKE_INSTALL_PREFIX=\/usr\/local  -Dprotobuf_BUILD_TESTS=OFF -Dprotobuf_BUILD_SHARED_LIBS=ON ..\/demo\/protobuf\/cmake\r\n<\/code><\/pre>\n<\/li>\n<li>\u7528Visual Studio\u6253\u5f00\u4e0a\u4e00\u6b65\u751f\u6210\u7684protobuf.sln\u6587\u4ef6\uff0c\u7f16\u8bd1\u751f\u6210\u3002\u6267\u884cINSTALL\u5de5\u7a0b\uff0c\u53ef\u4ee5\u5c06\u7f16\u8bd1\u7684\u7ed3\u679c\u8f93\u51fa\u5230\/usr\/local\u76ee\u5f55\u4e0b<\/li>\n<\/ol>\n<h3><a id=\"user-content-\u8bf4\u660e\" class=\"anchor\" href=\"https:\/\/github.com\/stonejiang208\/windows-protobuf\/blob\/master\/win-protobuf.md#%E8%AF%B4%E6%98%8E\" aria-hidden=\"true\"><\/a>\u8bf4\u660e<\/h3>\n<ol>\n<li>\u8bbe\u7f6e\u751f\u6210\u7684\u8def\u5f84\u4e3a\u5f53\u524d\u76d8\u7b26\u7684\/usr\/local\uff0c\u8fd9\u662f\u6211\u7684\u4e2a\u4eba\u559c\u597d\uff0c\u76ee\u7684\u662f\u8ddflinux\u4e0b\u7684\u8def\u5f84\u4e00\u81f4\uff0c\u56e0\u4e3a\u7edd\u5927\u6570\u65f6\u5019\uff0c\u8fd8\u662f\u5de5\u4f5c\u5728linux\u73af\u5883\u4e0b\u7684\u3002<\/li>\n<li>\u751f\u6210\u52a8\u6001\u94fe\u63a5\u5e93 -Dprotobuf_BUILD_SHARED_LIBS=ON<\/li>\n<li>\u4e0d\u7f16\u8bd1\u6d4b\u8bd5\u4ee3\u7801 -Dprotobuf_BUILD_TESTS=OFF<\/li>\n<\/ol>\n<h2><a id=\"user-content-\u4f7f\u7528protobuf\u7684\u4e3b\u8981\u6b65\u9aa4\" class=\"anchor\" href=\"https:\/\/github.com\/stonejiang208\/windows-protobuf\/blob\/master\/win-protobuf.md#%E4%BD%BF%E7%94%A8protobuf%E7%9A%84%E4%B8%BB%E8%A6%81%E6%AD%A5%E9%AA%A4\" aria-hidden=\"true\"><\/a>\u4f7f\u7528protobuf\u7684\u4e3b\u8981\u6b65\u9aa4<\/h2>\n<ol>\n<li>\u5b9a\u4e49\u6570\u636e\u4ea4\u6362\u683c\u5f0f\u6587\u4ef6\n<pre><code>syntax=\"proto3\";\r\npackage GP.Msg;\r\n\r\n message Header\r\n {\r\n     uint32 cmd = 1;   \/\/\u6d88\u606f\u53f7\r\n     uint32 sn = 2;    \/\/\u6d88\u606f\u5e8f\u53f7\r\n     string token = 3; \/\/\u6d88\u606f\u4ee4\u724c\r\n }\r\n<\/code><\/pre>\n<\/li>\n<li>\u751f\u6210\u52a8\u6001\u94fe\u63a5\u5e93\n<p>\u5229\u7528ACE\u63d0\u4f9b\u7684\u5de5\u5177generate_export_file.pl\u751f\u6210\u5bfc\u51fa\u6587\u4ef6\uff0c\u8fd9\u4e2a\u6587\u4ef6\u540e\u9762\u8981\u7528\u5230\u3002<\/p>\n<pre><code> cd libDemo\r\n generate_export_file.pl GP_PROTOC &gt; GP_Protoc_Export.h\r\n<\/code><\/pre>\n<p>\u5229\u7528protoc\u5b9e\u7528\u5de5\u5177\u751f\u6210c++\u5934\u6587\u4ef6\u548c\u6e90\u6587\u4ef6,\u53c2\u89c1libDemo\/g.sh\u3002\u8fd9\u4e00\u6b65\u7531\u4e8e\u8981\u4f7f\u7528sed\uff0c\u6240\u4ee5\u9700\u8981\u8fdb\u5165\u5230git bash\u73af\u5883\u4e2d\u6765\u8fd0\u884c\u3002<\/p>\n<pre><code> protoc  --cpp_out=dllexport_decl=GP_PROTOC_Export:. Demo.proto\r\n sed -i '21 c #include \"GP_Protoc_Export.h\"' Demo.pb.h\r\n<\/code><\/pre>\n<p>protoc\u6709dllexport_decl\u6307\u4ee4\u7528\u4e8e\u65b9\u4fbf\u751f\u6210\u7684\u4ee3\u7801\u80fd\u7f16\u8bd1\u6210\u52a8\u6001\u5e93\uff0c\u4f46\u662f\u6211\u8fd8\u4e0d\u77e5\u9053\u7528\u4ec0\u4e48\u6307\u4ee4\u6765\u5728\u751f\u6210\u7684*.pb.h\u4e2d\u589e\u52a0\u5305\u542b\u7684\u5934\u6587\u4ef6\uff0c\u6240\u4ee5\u53ea\u597d\u7528sed\u6765\u5f3a\u884c\u4fee\u6539\u3002\u5982\u679c\u60a8\u77e5\u9053\u6709\u66f4\u597d\u7684\u529e\u6cd5\uff0c\u8bf7\u544a\u8bc9\u6211\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\u5229\u7528mwc\u5de5\u5177\u751f\u6210\u6784\u5efa\u6587\u4ef6\u3002mwc\u5de5\u5177\u7c7b\u4f3c\u4e8eCMake\uff0c\u672c\u6587\u4ef6\u6682\u4e0d\u591a\u4f5c\u8ba8\u8bba\uff0c\u5982\u679c\u6709\u9700\u8981\u66f4\u8be6\u7ec6\u7684\u4ecb\u7ecd\uff0c\u8bf7\u7ed9\u6211\u7559\u8a00\u3002\u8fd9\u91cc\u76f4\u63a5\u7ed9\u51fa\u64cd\u4f5c\u6307\u4ee4\u3002<\/p>\n<pre><code> rem \u5f53\u524d\u8def\u5f84 libDemo\r\n mwc.pl -type vc14\r\n<\/code><\/pre>\n<p>\u4ee5\u4e0a\u6307\u4ee4\u80fd\u751f\u6210 libDemo.sln\u6587\u4ef6,\u7528Visual Studio\u7f16\u8bd1\u53ef\u5f97libDemo.dll, libDemo.lib<\/li>\n<li>\u5728\u81ea\u5df1\u7684\u5de5\u7a0b\u4e2d\u4f7f\u7528\u52a8\u6001\u5e93\u8fdb\u884c\u7f16\u7801\u6216\u89e3\u7801\n<pre><code> rem \u5f53\u524d\u8def\u5f84 DemoApp\r\n mwc.pl -type vc14\r\n<\/code><\/pre>\n<p>\u4ee5\u4e0a\u6307\u4ee4\u80fd\u751f\u6210 DemoApp.sln\u6587\u4ef6,\u7528Visual Studio\u7f16\u8bd1\u53ef\u5f97DemoApp.exe,\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre><code>    C:\\workspace\\win-protobuf\\windows-protobuf\\src\\lib&gt;DemoApp.exe\r\n    cmd: 1\r\n    sn: 123\r\n    token: \"abcde\"\r\n\r\n    \ufffd\ufffd{\ufffd\ufffdabcde\r\n\r\n    cmd = 1\r\n\r\n    sn =123\r\n\r\n    token =abcde\r\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<h2><a id=\"user-content-\u793a\u4f8b\u4ee3\u7801\" class=\"anchor\" href=\"https:\/\/github.com\/stonejiang208\/windows-protobuf\/blob\/master\/win-protobuf.md#%E7%A4%BA%E4%BE%8B%E4%BB%A3%E7%A0%81\" aria-hidden=\"true\"><\/a>\u793a\u4f8b\u4ee3\u7801<\/h2>\n<p><a href=\"https:\/\/github.com\/stonejiang208\/windows-protobuf\">https:\/\/github.com\/stonejiang208\/windows-protobuf<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740 &nbsp; \u6458\u8981 Proto<\/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-3917","post","type-post","status-publish","format-standard","hentry","tag-02-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/3917","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=3917"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/3917\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=3917"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=3917"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=3917"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}