centos 7.2 下为erlang添加protobuffs

安装前提:
1.已经安装好erlang otp 
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下的协议文件。 

原文地址:https://www.cnblogs.com/ACshasow/p/6964512.html