我无法在 native 模式下运行以下代码。当我尝试时显示错误消息:
Warning: this system is not configured for native-code compilation. escript: exception error: no function clause matching test_escript_1383_893414_479613:main([]) (./test, line 5) in function escript:run/2 (escript.erl, line 747) in call from escript:start/1 (escript.erl, line 277) in call from init:start_it/1 in call from init:start_em/1
如何配置我的系统以在 native 模式下运行?
代码:
#!/usr/bin/env escript
-mode(native). %% to fun faster
main([NStr]) ->
N = list_to_integer(NStr),
IJ = [{I, J} || I <- lists:seq(1, N), J <- lists:seq(1, N)],
lists:foreach(fun({I, J}) -> put_data(I, J, true) end, IJ),
solve(N, 1, [], 0).
solve(N, J, Board, Count) when N < J ->
print(N, Board),
Count + 1;
solve(N, J, Board, Count) ->
F = fun(I, Cnt) ->
case get_data(I, J) of
true ->
put_data(I, J, false),
Cnt2 = solve(N, J+1, [I|Board], Cnt),
put_data(I, J, true),
Cnt2;
false -> Cnt
end
end,
lists:foldl(F, Count, lists:seq(1, N)).
put_data(I, J, Bool) ->
put({row, I }, Bool),
put({add, I+J}, Bool),
put({sub, I-J}, Bool).
get_data(I, J) ->
get({row, I}) andalso get({add, I+J}) andalso get({sub, I-J}).
print(N, Board) ->
Frame = "+-" ++ string:copies("--", N) ++ "+",
io:format("~s~n", [Frame]),
lists:foreach(fun(I) -> print_line(N, I) end, Board),
io:format("~s~n", [Frame]).
print_line(N, I) ->
F = fun(X, S) when X == I -> "Q " ++ S;
(_, S) -> ". " ++ S
end,
Line = lists:foldl(F, "", lists:seq(1, N)),
io:format("| ~s|~n", [Line]).
最佳答案
如果您使用的是 Ubuntu 软件包,请安装 erlang-base-hipe
而不是 erlang-base
(一个替换另一个)。 HIPE 代表“高性能 Erlang”。
native 编译不是唯一的编译类型。如果您使用 -mode(compile)
相反,脚本将在运行之前编译为 BEAM 字节码。无论您的 Erlang 安装是否支持 HIPE,这都有效,并且在大多数情况下都足够快。
您还会收到第二条错误消息:
escript: exception error: no function clause matching test__escript__1383__893414__479613:main([]) (./test, line 5)
这与 native 编译无关。这只是意味着您使用零参数调用 escript,但它只接受一个参数。您可以通过在
main
中添加第二个子句来使其对用户更加友好。功能:main(Args) ->
io:format("expected one argument, but got ~b~n", [length(Args)]),
halt(1).
关于ubuntu - 在 native 模式下运行 erlang 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19912177/