ubuntu - 在 native 模式下运行 erlang 代码

标签 ubuntu erlang erlang-shell

我无法在 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/

相关文章:

linux - 在 VIM 上保存文件时自动 rsync 不起作用,因为 rsync 提示密码

ssl - 无法连接到 SSL LDAP 服务器

Erlang 接收消息——内部是怎么做的?

erlang shell 模块功能帮助

erlang - 设置 Erlang 节点失败并显示 "Can' t set long node name!请检查您的配置”

python - 无法导入 MongoClient

python - 乌类图14.04 : ImportError: No module named client

node.js - 通过npm安装Realm时出错: npm ERR!在realm@1.2.0安装脚本失败 'node-pre-gyp install --fallback-to-build'

unicode - Erlang操作系统:cmd() command with UTF8 binary

erlang - 如何使用来自 erlang shell 的参数生成进程