title: OCaml: How to Run a Script with Libraries Included
body: <p>I'm following the Real World OCaml book to learn OCaml and many of the programs require using the Jane Street Core library. When I use functions from this core library in the toplevel, it works great. There, I just use the following commands to open the Core library. </p>
  
  <pre><code>$ #use "topfind";;
  #thread;;
  #camlp4o;;
  #require "core.top";;
  #require "core.syntax";;
  open Core.Std;;
  </code></pre>
  
  <p>Then I can run this program line by line in the toplevel and functions like String.split work fine. </p>
  
  <pre><code># let languages = "OCaml,Perl,C++,C";;
  val languages : string = "OCaml,Perl,C++,C"
  # let dashed_languages =
  let language_list = String.split languages ~on:',' in
  String.concat ~sep:"-" language_list
  ;;
  val dashed_languages : string = "OCaml-Perl-C++-C"
  </code></pre>
  
  <p>But if I put it all in a script, how do I make the compiler recognize the core libraries? So if I'm running the same program in a script:</p>
  
  <pre><code>open Core.Std;;
  open Str;;
  
  let languages = "OCaml,Perl,C++,C";;
  let dashed_languages =
      let language_list = String.split languages ~on:',' in
      String.concat ~sep:"-" language_list
  ;;
  </code></pre>
  
  <p>Then I compile it:</p>
  
  <pre><code>ocamlopt -o test test.ml
  </code></pre>
  
  <p>I get an error:</p>
  
  <pre><code>Error: Unbound module Core
  </code></pre>
  
  <p>So clearly Core is not being recognized. I can't put the #use and #require commands in my script because OCaml doesn't recognize those. So how can I use Core? </p>