Basic Module importing with Open Module

I was trying to import a module I have called common.ml with the following syntax:

Open Common;;

based on the tutorial/docs https://ocaml.org/learn/tutorials/modules.html I was reading they suggest to compile the modules. Thus I did:

$ ocamlopt -c common.ml

I got an error but I assume its safe to ignore it

File "common.ml", line 7, characters 2-25:
7 |   Pervasives.print_string (str));;
      ^^^^^^^^^^^^^^^^^^^^^^^
Alert deprecated: module Stdlib.Pervasives
Use Stdlib instead.

If you need to stay compatible with OCaml < 4.07, you can use the
stdlib-shims library: https://github.com/ocaml/stdlib-shims

however, when I try to run my file it can’t find the common module:

$ ocaml testing_imports.ml
File "./testing_imports.ml", line 1:
Error: Reference to undefined global `Common'

its contents is only the import statement:

open Common;;

What am I doing wrong? Am I not understanding how imports work in OCAML? I feel the tutorial doesn’t really explain what Im suppose to do…specially how OCAML finds modules (in general). Like how does it know its on the same directory? What if its else where? I’d like to start with something very simple first to see the basics and perhaps expand later.

There are two OCaml compilers: ocamlc and ocamlopt. The former compiles to bytecode objects (.cmo files) and, unless you specify the -c flag, bytecode executables. The latter compiles to native objects (.o files) and executables, and also produces .cmx files that are used for storing OCaml-specific info about the object files.

The ocaml toplevel only works with bytecode, so if you want your toplevel to see your Common module it needs to have been compiled with ocamlc.
In addition, your open Common statement will only work if your toplevel has access to the implementation of the Common module. For modules of the standard library (like Printf in the tutorial example), they are included by default. For your own modules, you have to either give them as command-line options when starting the toplevel (ocaml common.cmo testing_imports.ml) or load them dynamically during the toplevel session using the #load directive. In your case, that would mean starting your testing_imports.ml file with a line #load "common.cmo";;.

Hope that answers your question.