3

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?

podington
  • 179
  • 1
  • 8

2 Answers2

3

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
ivg
  • 34,431
  • 2
  • 35
  • 63
  • Weird, that doesn't seem to work for me. I tried the same example too. Do you think this is because of Windows? I'm using Cygwin to currently handle development since Windows isn't OCaml friendly. – podington Oct 26 '16 at 22:42
  • 1
    I guess you are using MinGW OCaml, which is a Windows application. There, `Sys.command` uses Windows (I guess `cmd.exe`) to execute commands, and shell scripts like `test.sh` are not recognisable as a command nor batch file by Windows. – camlspotter Oct 27 '16 at 00:36
2

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.

podington
  • 179
  • 1
  • 8