1. 程式人生 > >centos 7.2 下為erlang添加protobuffs

centos 7.2 下為erlang添加protobuffs

per 代碼 brush 協議 讀取 ger required mpi 會有

安裝前提: 1.已經安裝好erlang otp 2.配置了rebar (配置方法:http://www.cnblogs.com/panfeng412/archive/2011/08/14/2137990.html 3.安裝git (centos:yum install git ubantu:sudo apt install git) 在rebar.config中的deps中加入protobuffs 技術分享
{deps, [{protobuffs,".*",{git,"https://github.com/basho/erlang_protobuffs.git",""}},
         {‘goldrush‘,".*",{git,"https://github.com/extend/goldrush.git","mastter"}},
         {‘proper‘, ".*", {git,"https://github.com/manopapad/proper.git", "master"}},
         {‘lager‘,".*",{git, "https://github.com/erlang-lager/lager.git","master"}}]}.

  

然後執行 ./rebar get-deps 在get-deps過程中會下載 meck(protobuffs的依賴庫)和protobuffs兩個依賴庫。 到這裏之後你可以在src下新建一個test.proto文件 test.proto文件:
message Person {
     required int32 age = 1;
     required string name = 2;
 }
 
 message Family {
     repeated Person person =1;
 }

  

然後執行編譯命令 ./rebar clean compile 會發現在rebar 同級目錄生成了一個include文件夾,裏面包含一個test.hrl的文件,打開會看到如下: 技術分享
這就是proto編譯生成頭文件。 其實到這裏整個protobuffs就算可以用了,但我們為了結構的清晰不希望所有的文件都放在src中,(我希望我的協議文件都在在和src同級的proto文件夾下)。 因為rebar的文檔中並沒有寫如何配置,只是給出了src_dirs這個參數可以設置其他的源文件的地址,但直接在rebar.config中加入{src_dirs,["proto"]}並沒有用,然後在rebar源碼中找到關於對protobuffs的編譯代碼,rebar_proto_comiler.erl文件

  技術分享

這裏設置了讀取config的proto_opts項,然後下面會有src_dirs,這裏的如果沒有配置的時候默認是src文件夾,所以其實src_dirs是配在proto_opts下的,參照rebar.config.sample, 這是例子中給出的配置:   技術分享

然後我將自己的也配置成了下面這樣,

  技術分享

完成之後,在src同級目錄下新建一個proto文件夾,然後在裏面創建*.proto文件,再執行./rebar compile 的時候就會編譯proto下的協議文件。

centos 7.2 下為erlang添加protobuffs