title: OCaml - compiling a program as a library
body: <p>I have an OCaml program(with a main method - it generates an executable) and I want to use it as a library.</p>
  
  <p>I was compiling my program like this: <code>ocamlc -I someDir -g -unsafe lotsOfCmoFiles -o outputFile</code> and the program works fine.</p>
  
  <p>Now I'm removing the line that makes it an executable(something like <code>let _ = ...</code>) and adding <code>-a</code> parameter to compile command: <code>ocamlc -a -I someDir -g -unsafe lotsOfCmoFiles -o outputFile.cma</code></p>
  
  <p>But somehow I can't load generated .cma file with <code>ocamltop</code> and <code>ocamlbrowser</code> shows an empty list. When I try to load from <code>ocamltop</code>:</p>
  
  <pre><code># #load "outputFile.cma";;
  Error: Reference to undefined global `Xyz'
  </code></pre>
  
  <p>And I'm 100% sure that xyz.cmo is included in <code>lotsOfCmoFiles</code>.</p>
  
  <p>Am I giving some parameter wrong while compiling? Or else, what should I do to load my program in ocamltop ? (I'll use this library in another program, I'm giving ocamltop outputs as an example)</p>
  
  <p>Any helps will be appreciated.</p>
  
  <p>EDIT: So I can finally managed to compile and load it thanks to @cago, now I can load my library, and when I don't remove the main <code>let _ = ...</code> line it's automatically run when I load the <code>.cma</code>.</p>
  
  <p>But I still can't open any modules. Strangely, this doesn't raise an exception</p>
  
  <pre><code>open Main
  </code></pre>
  
  <p>but then when I call a function from module Main:</p>
  
  <pre><code># someFun;;
  Error: Reference to undefined global `Main'
  </code></pre>
  
  <p>and <code>ocamlbrowse</code> still shows an empty list. now why is that?</p>
  
  <p>EDIT2: I realized <code>open Main</code> doesn't fail because I have a Main module in the same folder(even though I didn't explicitly load it). If I move my .cma file somewhere else and load it, it works(ie. main function runs automatically), but now I can't open any modules even though <code>ocamlobjinfo</code> shows the modules.</p>
  
  <p>EDIT3: -I doesn't help:</p>
  
  <pre><code>$ ocaml
          OCaml version 4.00.1
  
  # #load "lib.cma";;
  ok
  # open Lib;;
  Error: Unbound module Lib
  # 
  $ ocaml -I libFolder    
          OCaml version 4.00.1
  
  # #load "toylib.cma";;
  ok
  # open Lib;;
  # fun;;
  Error: Reference to undefined global `Lib'
  </code></pre>