How can I open modules defined outside my project root directory in my ocaml file?

491 views Asked by At

I'm not experienced in ocaml, my situation here is that I'm writing my ocaml code, I try to access the types that defined in other .ml files, which locate outside of my project root directory. I tried to set up _oasis, however when I specify the library field, the Path value is invalid as I try to use "../../dir". Does anyone know how can I achieve this goal? I don't want to copy them into my project root directory, as I was looking through those tutorials, haven't found a solution yet, thanks!

1

There are 1 answers

2
ivg On

The idea of oasis is that you have one _oasis file in the root directory of your project, and that file defines the whole project. Hence, all the paths are specified relative to the root folder (the one that contains _oasis)

In your case, the project structure could look like this:

my_project/
|
+- _oasis
+-lib/
| |
| +- types/
|    |
|    +- common.ml
+-src/
  |
  +- app/
     |
     +- app_main.ml

And an _oasis file that corresponds to it would be:

OASISFormat: 0.4
Name:        myproject
Version:     0.1
Synopsis:    captures the world
Authors:     robots
Maintainers: humans
License:     MIT
Copyrights:  (C) Acme corp
Plugins:     META (0.4)
BuildTools: ocamlbuild


Library "common_types"
  Path: lib/types
  FindlibName: acme-common-types
  Modules: Common

Executable "control-people"
  Path: src/app
  MainIs: app_main.ml
  Install: true
  BuildDepends: common_types

Finally, since merlin now is a tool de facto, you shall also consider teaching him your project structure, by adding the .merlin file to the root folder of your project with the following contents:

 B _build/
 B _build/lib/types
 B _build/src/app
 S lib/types
 S src/app

If you have any external dependencies, you can add them to .merlin using PKG <findlib-name-of-the-dep>, e.g., PKG core_kernel. And, of course, you should add your external dependencies to you libraries and applications in the _oasis file, by using the BuildDepends clause, e.g., BuildDepends: core_kernel.