开发者

Compile from within the OCaml repl

I am using emacs with tuare开发者_JS百科g mode and I always have the ocaml repl opened inside emacs. I was wondering if there's a way of compiling basic OCaml test files from within the repl so I don't have to run the compilation as a system command.


To my knowledge there isn't, but you can easily run compilation from within emacs.

If you use the M-x compile command (C-c C-c using the tuareg mode), the compilation is run in emacs "compile mode" which is rather nice to work with. In particular, it allows you to jump directly at the erroneous positions in the code in case of compilation error (M-x goto-next-error or "C-x `").

With the compile command, you choose the (shell) command to run the compilation. You can invoke the ocaml compiler directly (typically, ocamlc -c foo.ml), or use ocamlfind to facilitate compilation using external libraries, and even make or ocamlbuild to automate most of the compilation for you. Typically, if you have a small project using different source files, with a main.ml file, ocamlbuild main.byte will do the job of building an executable from it.

Edit : of course, you could call a system command from within the toplevel, using the Sys.command function. But I don't see the point.


By "repl" you mean the "read-eval-print loop", that you may have gotten into for instance running run-caml in Emacs.

There is an Emacs command, compile, for compiling regardless of what you are doing or the files you are editing. It is usually bound to C-c C-c, but doesn't appear to be in my version of inf-caml. Nevertheless, you may bind it to any convenient sequence (probably not C-c C-c as that is used to interrupt the OCaml toplevel).

You can change the command launched by compile (the default is make -k). Your commandline should be retained within an Emacs session. If you do not like that it is lost from session to session, write a Makefile to make the default command the correct one. That's the easiest way.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜