【发布时间】:2011-06-03 17:14:42
【问题描述】:
快速提问。如何在 ocaml 中运行可执行文件?我相信这是可能的,但我不知道如何。
请提供示例代码。
【问题讨论】:
-
查看有关
Unix模块的手册。 caml.inria.fr/pub/docs/manual-ocaml/manual035.html -
或
Sys.command用于最简单的情况
快速提问。如何在 ocaml 中运行可执行文件?我相信这是可能的,但我不知道如何。
请提供示例代码。
【问题讨论】:
Unix 模块的手册。 caml.inria.fr/pub/docs/manual-ocaml/manual035.html
Sys.command 用于最简单的情况
如果您只想运行一个程序而不与它通信,请使用Sys.command。
let exit_code = Sys.command "c:\\path\\to\\executable.exe /argument" in
(* if exit_code=0, the command succeeded. Otherwise it failed. *)
对于更复杂的情况,您需要使用functions in the Unix module。尽管有这个名字,most of them 在 Windows 下工作。例如,如果您需要从命令中获取输出:
let ch = Unix.open_process_in "c:\\path\\to\\executable.exe /argument" in
try
while true do
let line = input_line ch in …
done
with End_of_file ->
let status = close_process_in ch in …
【讨论】: