Question

I cannot run the following code in native mode. The error message is displayed when I try:

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

How can I configure my system to run it in native mode?

Code:

#!/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]).
Was it helpful?

Solution

If you're using the Ubuntu packages, install erlang-base-hipe instead of erlang-base (one replaces the other). HIPE stands for "High-Performance Erlang".

Native compilation is not the only kind of compilation. If you use -mode(compile) instead, the script will be compiled to BEAM byte code before being run. This works regardless of whether your Erlang installation supports HIPE, and will be fast enough in most cases.

You also get a second error message:

escript: exception error: no function clause matching test__escript__1383__893414__479613:main([]) (./test, line 5)

This is not related to native compilation. It just means that you called the escript with zero arguments, but it only accepts exactly one argument. You could make it somewhat user-friendlier by adding a second clause to the main function:

main(Args) ->
    io:format("expected one argument, but got ~b~n", [length(Args)]),
    halt(1).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top