Ocaml代码编译,但不与顶级

zel*_*ell 3 ocaml load undefined-reference ocaml-toplevel

我正在做一个免费课程.下面的ocaml代码在我"make"时编译,但是一旦我试图把它放到顶层就会抱怨.为什么?

let main () =
  (* Parsing arguments *)
  let f_name = ref "" in
  Arg.parse [ ] (fun s -> f_name := s) "Mini-Java analyzer";
  (* Parsing of the source file *)
  let simple_java_prog =
    if String.compare !f_name "" = 0 then failwith "no program file given";
    Localizing.current_file_name := !f_name;
    let f_desc = open_in !f_name in
    let lexbuf = Lexing.from_channel f_desc in
    let java_prog =
      try Java_parser.program Java_lexer.token lexbuf
      with
      | e ->
          Printf.printf "Exception during parsing: %s\n"
        (Printexc.to_string e);
          failwith "Stopped" in
    Simple_java_translate.tr_java_prog java_prog in
  Printf.printf "finished...\n"

 let _ = main ()
Run Code Online (Sandbox Code Playgroud)

但是,当我将"cc ce"(来自emacs)这个代码放到顶层时,它给了我错误

....    
  let main () =

Error: Reference to undefined global `Localizing'
Run Code Online (Sandbox Code Playgroud)

然后,有了这个错误信息,我得到了一个解释

http://caml.inria.fr/pub/docs/manual-ocaml/manual023.html

说,

对未定义的全局mod的引用 您忽略了在内存中加载带有#load的模块的实现.

因此,我正在尝试:#load"localizing.cmo".但是,现在的问题是:没有localizing.cmo这是搜索结果.

bash-3.2$ ls localizing*
localizing.cmi  localizing.cmx  localizing.ml   localizing.mli  localizing.o
Run Code Online (Sandbox Code Playgroud)

我希望将makefile放在这里,问题的根源不应该来自"Makefile",除了它太大了.此外,我不应该修改makefile,因为这是由课程的网站提供的.

有什么问题??谢谢.我是否正确地得出结论:并非所有的ocaml程序都应该能够在顶层执行?

Gil*_*il' 5

问题在于makefile,或者至少解决方案是部分问题.

  • .cmi文件是编译接口(编译自.mli).
  • .cmo文件字节码编译实现(编译自.mlocamlc).
  • .cmx.o文件是本地代码编译的实现(编译自.mlocamlopt).

您已将程序编译为本机代码.但是toplevel运行字节码.您需要将模块编译为字节码,方法是在makefile中添加以下内容:

default: localizing.cmo localizing.cmx
%.cmo: %.ml
        ocamlc -c $<
Run Code Online (Sandbox Code Playgroud)