Warning: behaviour my_behaviour undefined
在make:all()时出现Warning: behaviour my_behaviour undefined这样的警告,回头检查了一遍代码,没发现有什么问题;代码完全是按照教程来的,不可能出错。
上网查,也没找到解决方法。没办法,只能看源码啦。
查找路径: make:all/0 --> make:all/1 --> make:do_make_files/2 --> make:process/3 --> make:recompilep/4 --> make:recompile/4 --> compile:file/2 --> compile:do_compile/2 --> compile:internal/2 --> compile:passes/2 --> compile:passes_1/1 --> compile:standard_passes/0 --> compile:lint_module/1 --> erl_lint:module/3 --> erl_lint:forms/2 --> erl_lint:post_traversal_check/2 --> erl_lint:check_behaviour/1 --> erl_lint:behaviour_check/2 --> erl_lint:all_behaviour_callbacks/3 --> erl_lint:behaviour_callbacks/3
即:
behaviour_callbacks(Line, B, St0) ->
try B:behaviour_info(callbacks) of
Funcs when is_list(Funcs) ->
All = all(fun({FuncName, Arity}) ->
is_atom(FuncName) andalso is_integer(Arity);
({FuncName, Arity, Spec}) ->
is_atom(FuncName) andalso is_integer(Arity)
andalso is_list(Spec);
(_Other) ->
false
end,
Funcs),
MaybeRemoveSpec = fun({_F,_A}=FA) -> FA;
({F,A,_S}) -> {F,A};
(Other) -> Other
end,
if
All =:= true ->
{[MaybeRemoveSpec(F) || F <- Funcs], St0};
true ->
St1 = add_warning(Line,
{ill_defined_behaviour_callbacks,B},
St0),
{[], St1}
end;
undefined ->
St1 = add_warning(Line, {undefined_behaviour_callbacks,B}, St0),
{[], St1};
_Other ->
St1 = add_warning(Line, {ill_defined_behaviour_callbacks,B}, St0),
{[], St1}
catch
_:_ ->
St1 = add_warning(Line, {undefined_behaviour,B}, St0),
{[], St1}
end.
假如make的时候在搜索路径找不到行为定义模块(即my_behaviour模块), 那么try B:behaviour_info(callbacks) of 这一句会抛出异常,也就加入undefined_behaviour的警告,最后在erl_lint:format_error/1中会打印这个警告:
format_error({undefined_behaviour,Behaviour}) ->
io_lib:format("behaviour ~w undefined", [Behaviour]);
知道了原因,解决办法就不难得出了。
可以在make:all().之前先加一些搜索路径, 使用code:add_patha/1 或者erl –pa ./ebin –eval “make:all()”.
还有一种情况是:
behaviour使用模块在behaviour定义模块之前编译时,B:behaviour_info(callbacks)这一句也会抛出异常,同样会产生behaviour xxx undefined的警告。
这时就需要确保behaviour定义模块在behaviour使用模块编译之前编译,暂时想到两种方法:一是先编译behaviour定义模块,将其产生的beam文件放到ebin目录下,然后再make:all(); 二是: 修改Emakefile文件,定义多个规则以实现先编译behaviour定义模块。
浙公网安备 33010602011771号