2

I've recently been trying to get OCaml to work, and the interpreter works fine, I just can't build native executable's with it. Here is my program:

let () = print_endline "Hello, World!"

Now if I run ocaml program.ml I get the expected output of Hello, World!. However if I run ocamlbuild program.native I get:

mkdir 'C:\Users\User\_build'
'''C:\Program Files\OCaml\bin/ocamldep.opt' -modules program.ml > program.ml.depends
Exception Sys_error("program.ml.depends: No such file or directory").

Yet if I look in the directory C:\Users\User\ I can see that program.ml.depends exists. Running it again I get:

SANITIZE: a total of 1 file that should probably not be in your source tree
  has been found. A script shell file "C:\\Users\\User\\_build/sanitize.sh"
  is being created. Check this script and run it to remove unwanted files or
  use other options (such as defining hygiene exceptions or using the
  -no-hygiene option).
IMPORTANT: I cannot work with leftover compiled files.
ERROR: Leftover dependency files:
  File program.ml.depends in . has suffix .ml.depends
Exiting due to hygiene violations.

So it does exist!?!?!?! The contents are my_prog.ml:. Side-note, the _build directory contains the files: _digests, _log, program.ml, and sanatize.sh. Below is the output when I run the commands with -verbose 10:

mkdir 'C:\Users\User\_build'
Doing sanity checks
include directories are: [ "." ]
==> program.native
====> program.cmx
======> program.mlpack
======> program.ml
program.ml exists in source dir -> import it
======> program.ml.depends
========> program.ml
program.ml already built
start rule ocaml dependencies ml (%=program )
dyndeps: {.  .}
mid rule ocaml dependencies ml (%=program ): cache miss: a product is not in build dir (program.ml.depends)
'''C:\Program Files\OCaml\bin/ocamldep.opt' -modules program.ml > program.ml.depends
resource_changed: program.ml.depends
end rule ocaml dependencies ml (%=program )
======> program.cmi
========> program.mli
==========> program.mly
========> program.mlpack
program.mlpack already failed
========> program.ml
program.ml already built
========> program.ml.depends
program.ml.depends already built
start rule ocaml: ml -> cmo & cmi (%=program )
Exception Sys_error("program.ml.depends: No such file or directory").

And if I force it:

include directories are: [ "." ]
==> program.native
====> program.cmx
======> program.mlpack
======> program.ml
program.ml exists and up to date
======> program.ml.depends
program.ml.depends exists in source dir -> import it
======> program.cmi
========> program.mli
==========> program.mly
========> program.mlpack
program.mlpack already failed
========> program.ml
program.ml already built
========> program.ml.depends
program.ml.depends already built
start rule ocaml: ml -> cmo & cmi (%=program )
dyndeps: {.  .}
mid rule ocaml: ml -> cmo & cmi (%=program ): cache miss: a product is not in build dir (program.cmo)
'''C:\Program Files\OCaml\bin/ocamlc.opt' -c -o program.cmo program.ml
resource_changed: program.cmo
resource_changed: program.cmi
end rule ocaml: ml -> cmo & cmi (%=program )
start rule ocaml: ml & cmi -> cmx & o (%=program )
dyndeps: {.  .}
mid rule ocaml: ml & cmi -> cmx & o (%=program ): cache miss: a product is not in build dir (program.cmx)
'''C:\Program Files\OCaml\bin/ocamlopt.opt' -c -o program.cmx program.ml
resource_changed: program.cmx
resource_changed: program.o
end rule ocaml: ml & cmi -> cmx & o (%=program )
====> program.o
program.o already built
start rule ocaml: cmx* & o* -> native (%=program )
prepare_libs: "program.native" -> [  ]
caml_transitive_closure ~caml_obj_ext:"cmx" ~pack_mode:false
  ~used_libraries:[  ] [ "program.cmx" ]
packages: {.  .}
dependency_map: {::}
used_files: {. program.cmx .}
open_packages: [  ]
lib_index: {::}
dependencies: {::}
caml_transitive_closure: [ "program.cmx" ] -> [  ]
Failure: Link list cannot be empty.

I run the same command again:

include directories are: [ "." ]
==> program.native
====> program.cmx
program.cmx exists in source dir -> import it
====> program.o
program.o exists in source dir -> import it
start rule ocaml: cmx* & o* -> native (%=program )
prepare_libs: "program.native" -> [  ]
caml_transitive_closure ~caml_obj_ext:"cmx" ~pack_mode:false
  ~used_libraries:[  ] [ "program.cmx" ]
packages: {.  .}
dependency_map: {::}
used_files: {. program.cmx .}
open_packages: [  ]
lib_index: {::}
dependencies: {::}
caml_transitive_closure: [ "program.cmx" ] -> [ "program.cmx" ]
link: [ "program.cmx" ] -o program.native
dyndeps: {.  .}
mid rule ocaml: cmx* & o* -> native (%=program ): cache miss: a product is not in build dir (program.native)
'''C:\Program Files\OCaml\bin/ocamlopt.opt' program.cmx -o program.native
resource_changed: program.native
end rule ocaml: cmx* & o* -> native (%=program )

At this point I do get a program.native file, however it doesn't work and when I open it in a text editor it starts with !<symlink>. After using readlink it points to _build/program.native which doesn't exist. At this point I'm stumped and have no idea where to go next. It's a shame that getting this programming language to work has been so difficult.

I would prefer not to have to dual boot/install a vm just to compile OCaml.

One last thing, how would I go about installing OPAM?

Matthew
  • 160
  • 9
  • 1
    Just as a side note, try to look at OcpWin: https://www.typerex.org/ocpwin.html – ivg Feb 05 '16 at 14:08

1 Answers1

5

1) Don't install OCaml to a path that contains white-space characters (like 'C:\Program Files...'), non-ascii characters or other special characters like '~#?...'). Your build path shouldn't contain such characters, too. It would lead to problems sooner or later, because all common build tools either rely on cygwin (and therefore unix conventions) or are too stupid to quote properly. ( Also, your build path looks dubious. 'C:\Users\User' usually contains a lot of other files, that are not related to your project. Put your files to a dedicated folder. )

2) If you want to compile to native code, a C toolchain (either mingw-w64 or msvc) must be present in your $PATH.

3) You can use my opam and OCaml builds that I've customized for cygwin and the mingw-w64 compiler: https://github.com/fdopen/opam-repository-mingw . It works well enough, if the requirements are met (all necessary cygwin packages are installed, no white-spaces in your username/build paths, no alternative OCaml installation in your $PATH)

rafix
  • 1,739
  • 9
  • 9