Erlang 中的编译错误:"cannot parse file, giving up"

Compile error in Erlang: "cannot parse file, giving up"

我刚刚开始研究 Erlang 并尝试编译我的第一个程序。与教程相比,一切看起来都不错,但我似乎无法摆脱这个错误。

这是我的代码,保存在 'useless.erl':

-module(useless).
-export([add/2, hello/0, greet_and_add_two/1]).

add(A,B) ->
A + B. 

%% Shows greetings.
%% io:format/1 is the standard function used to output text.
hello() ->
io:format("Hello, world!~n").

greet_and_add_two(X) ->
hello(),
add(X,2).

我将目录更改为 useless.erl 的目录:

cd("C:/Users/CP/Documents/Erlang").

然而,当我运行

c(useless).

我明白了

useless.erl:1: cannot parse file, giving up
useless.erl:1: no module definition
error

如果您的文件看起来不错,但 Erlang 编译器抱怨它无法解析该文件,则您的代码可能包含无效编码。

根据 documentation Erlang 仅接受 UTF-8Latin-1 编码的源代码。

The valid encodings are Latin-1 and UTF-8, where the case of the characters can be chosen freely

请注意,您仍然只能在字符串和注释中使用扩展字符集:

In Erlang/OTP R16B the syntax of Erlang tokens was extended to handle Unicode. The support is limited to string literals and comments.

如果出现解析错误或非法字符错误,您应该

  1. 确保您的文件以 UTF-8Latin-1 编码保存
  2. 确保不要在注释和字符串之外使用 UTF 文字
  3. 对于较旧的 Erlang 版本(Erlang 17 之前),您可以尝试在文件的前两行中明确指定编码

    %% coding: utf-8
    %%% or
    %% For this file we have chosen encoding = Latin-1
    %%% or
    %% -*- coding: latin-1 -*-