简体   繁体   English

如何使用OCaml中的本地程序执行命令?

[英]How do I execute a command using a local program in OCaml?

So the default way to execute commands in OCaml is Sys.command s where s is the command in this case, but I come across an issue when the command involves a local program. 因此,在OCaml中执行命令的默认方式是Sys.command s,在这种情况下,其中s是命令,但是当命令涉及本地程序时,我遇到了一个问题。 For example, if I have an executable named prog I compiled from an ocaml file, and I use Sys.command "prog {args}" , I get back an error saying 'prog' is not recognized as an internal or external command, operable program or batch file. 例如,如果我有一个从ocaml文件编译的名为prog的可执行文件,并且使用Sys.command "prog {args}" ,则会返回一条错误消息,指出'prog' is not recognized as an internal or external command, operable program or batch file. And using the command ./prog {args} doesn't seem to change anything either, since then it says '.' is not recognized as an internal or external command, operable program or batch file. 并且使用命令./prog {args}似乎也没有任何改变,因为它随后显示为'.' is not recognized as an internal or external command, operable program or batch file. '.' is not recognized as an internal or external command, operable program or batch file. Any advice? 有什么建议吗?

Probably the problem is in the prog and lies outside of the OCaml, in other words, your prog is not a program, so the operating system can't run it. 问题可能出在prog并且位于OCaml之外,换句话说,您的prog不是程序,因此操作系统无法运行它。

Here is an example, that works perfectly for me: 这是一个示例,非常适合我:

$ cat > test << EOF
#!/bin/sh
echo hello
EOF
$ chmod a+x test
$ ocaml
#  Sys.command "./test";;
hello
- : int = 0

So, one of the above users was right: in Windows, the batch files produced by ocamlc cannot be executed. 因此,上述用户之一是正确的:在Windows中,无法执行ocamlc生成的批处理文件。 Instead, an easy workaround is just to compile by appending the .exe tag to the file name you want to produce eg ocamlc -o prog.exe myfile.ml . 相反,一种简单的解决方法是通过将.exe标记附加到要生成的文件名来进行编译,例如ocamlc -o prog.exe myfile.ml Then, just use Sys.command "prog.exe {args}" instead. 然后,只需使用Sys.command "prog.exe {args}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM