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. 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. 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. 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.
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. Instead, an easy workaround is just to compile by appending the .exe tag to the file name you want to produce e.g. ocamlc -o prog.exe myfile.ml. Then, just use Sys.command "prog.exe {args}" instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With