4

The Ocaml manual contains an exercise (here) in which library object files are loaded in the toplevel loop (the ocaml interactive interpreter) in the following way:

#load "dynlink.cma";;
#load "camlp4o.cma";;

I'm trying to replicate the subsequent code in a compilable source file, and the code requires the above library object files. Can I load these files with a line of code within the source file and compile it with ocamlc? I've tried "#load", "load", "#use", "use", "#require", "require", and all these proceded by "#directory" and "directory". I know that you can include modules with "include ;;", but this shouldn't work either, because they're just library files, not modules. I've tried to find a way to do this in the manual, but to no avail.

Do I need to reference the files in the compilation command? If so, how do I do this?

voutasaurus
  • 2,968
  • 2
  • 24
  • 37

2 Answers2

2

Directives starting with a # character are used only in the toplevel and are not strictly part of the OCaml language. In a file that you want to compile, you don't use # directives. See the OCaml manual Chapter 9. The #load directives are for loading a library. When compiling a file, you have to tell the compiler to use the library (on the command line, not in the file). It's good to learn the compiler commands directly at first, but eventually you should use ocamlfind and oasis, which make compilation much easier.

Ashish Agarwal
  • 3,000
  • 16
  • 18
1

I'm assuming your source is written using extensions implemented by camlp4o. To compile your source, you can say:

ocamlc -pp camlp4o -o myfile myfile.ml

I believe the complexities of the #load command are required only when you want to use the extensions in the toplevel (the interpreter).

Jeffrey Scofield
  • 65,646
  • 2
  • 72
  • 108