开发者

Ocaml code which compiles, but does not go with toplevel

开发者 https://www.devze.com 2023-03-15 20:34 出处:网络
I am doing an exercise of a free course. The following ocaml code compiles when I \"make\", but it complains once I am trying to put it into toplevel. Why?

I am doing an exercise of a free course. The following ocaml code compiles when I "make", but it complains once I am trying to put it into toplevel. Why?

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 ()

But when I "c-c c-e" (from emacs) this code to toplevel, it gives me the error

....    
  let main () =

Error: Reference to undefined global `Localizing'

then, with this error information, I got an explanation from

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

which says,

Reference to undefined global mod You have neglected to load in memory an implementation for a module with #load.

Thus, I am trying to do: #load "localizing.cmo". But, now the problem is: There is no localizing.cmo Here is the search result.

bash-3.2$ ls localizing*
localizing.cmi  localizing.cmx  localizing.ml   localizing.mli  localizing.o

I hesite to put makefile here, the source of the problem should not come from the "Makefile", besides it's so big. Besides, I am not supposed to modify makefile because that's provided by the course's site.

What's the problem?? Thank you. Am I right to conclude that, Not all ocaml programs are supposed to be able to be executed at toplevel?


The problem is in the makefile, or at least the solution is, in part.

  • .cmi files are compiled interfaces (compiled from .mli).
  • .cmo files are bytecode compiled implementations (compiled from .ml with ocamlc).
  • .cmx and .o files are native code compiled implementations (compiled from .ml with ocamlopt).

You've compiled your program to native code. But the toplevel runs bytecode. You need to compile the module to bytecode, by adding something like this to your makefile:

default: localizing.cmo localizing.cmx
%.cmo: %.ml
        ocamlc -c $<
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号