# Compile multiple ocaml files

**URL:** <https://discuss.ocaml.org/t/compile-multiple-ocaml-files/6520>\
**Category:** Learning\
**Created:** [September 29, 2020, 8:40am UTC](https://discuss.ocaml.org/t/compile-multiple-ocaml-files/6520 "2020-09-29T08:40:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bartalor](https://avatars.discourse-cdn.com/v4/letter/b/46a35a/32.png) [@bartalor](https://discuss.ocaml.org/u/bartalor)\
**Post date:** [September 29, 2020, 8:40am UTC](https://discuss.ocaml.org/t/compile-multiple-ocaml-files/6520/1 "2020-09-29T08:40:24Z")

</div>

I’m sorry if similar question was already answered, I didn’t find any.  
I wanted to ask how to compile multiple ocaml files.  
I think kind of know how to do that using ocamlc command, but I get syntax error when (assume my files are file1.ml, file2.ml) in file1.ml I put the line  
#use file2.ml;;  
I need this line to use functions I declared in file2,  
I know I can write file2.func but I really prefer not to.

---

<div class="post-metadata">

**Author:** ![bnguyenvanyen](https://avatars.discourse-cdn.com/v4/letter/b/7ea924/32.png) [@bnguyenvanyen](https://discuss.ocaml.org/u/bnguyenvanyen)\
**Post date:** [September 29, 2020, 9:27am UTC](https://discuss.ocaml.org/t/compile-multiple-ocaml-files/6520/2 "2020-09-29T09:27:11Z")

</div>

Hi, the `#` directives are for running in the toplevel (for instance utop).  
You might want to take a look at dune to build your programs more easily: [https://dune.build/](https://dune.build/)

To do it by hand, you can take a look at the relevant parts of the OCaml manual, about ocamlc (bytecode) and ocamlopt (native)

> **[Chapter 9  Batch compilation (ocamlc)](https://caml.inria.fr/pub/docs/manual-ocaml/comp.html)**

  

> **[Chapter 12  Native-code compilation (ocamlopt)](https://caml.inria.fr/pub/docs/manual-ocaml/native.html)**

It is good practice to write `File2.func` to refer to `func` defined in `File2`.  
If `File2` is `Too_long_to_type`, you can declare a module alias

```auto
module A = Too_long_to_type

```

And then refer to the function `func` as `A.func`.  
Otherwise, for everything that is defined in `File2` to be directly accessible, you can `open File2`. This is sometimes justified, but it might make the code less clear.

---

<div class="post-metadata">

**Author:** ![bartalor](https://avatars.discourse-cdn.com/v4/letter/b/46a35a/32.png) [@bartalor](https://discuss.ocaml.org/u/bartalor)\
**Post date:** [September 29, 2020, 10:52am UTC](https://discuss.ocaml.org/t/compile-multiple-ocaml-files/6520/4 "2020-09-29T10:52:56Z")

</div>

Thanks for the reply, for now I do want to use the toplevel because I’m a student and some of the files we were introduced to were written in toplevel.

I still find it hard how to compile files using toplevel, from a search on google I can see that the directive for toplevel is ocaml (instead of ocamlc),  
and it’s written like that:  
ocaml options objects  
when objects are file that end with .cmi or .cma  
so how do I compile my .ml files?

---

<div class="post-metadata">

**Author:** ![bnguyenvanyen](https://avatars.discourse-cdn.com/v4/letter/b/7ea924/32.png) [@bnguyenvanyen](https://discuss.ocaml.org/u/bnguyenvanyen)\
**Post date:** [September 29, 2020, 11:28am UTC](https://discuss.ocaml.org/t/compile-multiple-ocaml-files/6520/5 "2020-09-29T11:28:16Z")

</div>

Ok, so the toplevel works more like an interpreter, it is not used to compile files.  
So going back to your example, you might define some functions in `file.ml`,  
which you then want to use in some other code that depends on it.  
You would start a toplevel session (by calling `ocaml` or `utop` on the command line),  
and then in the interactive session, you can type `#use "file.ml"` to use these definitions.  
However you cannot use that to compile the code that you passed through the toplevel.  
I’m not sure if that clarifies anything or not…  
There’s just been a nice reply to another topic that might help you:

> [@How to debug when I need to open several modules?](https://discuss.ocaml.org/t/how-to-debug-when-i-need-to-open-several-modules/5868/2):
>
> Hi @Hsingi5 Sorry this question has been left unanswered, welcome to the OCaml community! I’m going through learning posts for a different project and came across this. I really hope you got your question answered, but seeing as I’m going through this I thought I would add it here in case it helps others. Utop Solution If you are looking for trying out packages installed using opam (like batteries in your example) then I highly recommend using [utop](https://github.com/ocaml-community/utop) the universal toplevel for OCaml. $ opam ins…

---

<div class="post-metadata">

**Author:** ![Cjen1](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/cjen1/32/1731_2.png) [@Cjen1](https://discuss.ocaml.org/u/Cjen1)\
**Post date:** [September 29, 2020, 1:11pm UTC](https://discuss.ocaml.org/t/compile-multiple-ocaml-files/6520/6 "2020-09-29T13:11:58Z")

</div>

The other option that may make things a touch easier is to start looking into the dune build system. You can define a library and then open it in utop via: `dune utop` (as far as I can remember).

If you haven’t used it already the [dev.realworldocaml.org](http://dev.realworldocaml.org) is a useful resource, the chapter dealing with how the compilation actually works is: [https://dev.realworldocaml.org/files-modules-and-programs.html](https://dev.realworldocaml.org/files-modules-and-programs.html).

---

<div class="post-metadata">

**Author:** ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)\
**Post date:** [September 29, 2020, 7:30pm UTC](https://discuss.ocaml.org/t/compile-multiple-ocaml-files/6520/7 "2020-09-29T19:30:01Z")

</div>

Hi, did you take a look at [https://caml.inria.fr/pub/docs/manual-ocaml/comp.html](https://caml.inria.fr/pub/docs/manual-ocaml/comp.html) ?
