Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include a local library to an executable with dune ocaml

I have a project structured like this :

root/
|—— dune-project
|—— lib/
|  |—— dune
|  |—— Readertsp.ml
|  |-- ...
|
|—— bin/
|  |—— dune
|  |—— bin.ml

bin.ml :

let city_config = "ch130" in
let path = Readertsp.open_path city_config in ();;

dune:

(executable
 (name MCTS_main)
 (libraries graphics mcts)
)

Readertsp.ml : https://pastebin.com/U0h69uRy

dune :

(library
 (name mcts)
 (modules Readertsp)
 (libraries graphics))

When I try dune build, I get this error :

File "tests/MCTS_main.ml", line 3, characters 0-19:
3 | Readertsp.open_path city_config;;
    ^^^^^^^^^^^^^^^^^^^
Error: Unbound module Readertsp

Do you know how to fix this ?

like image 567
Butanium Avatar asked Oct 15 '25 20:10

Butanium


1 Answers

Got a hint on the OCaml IRC/Discord.

My problem was that to access to the open_path function, I had to use

Mcts.Readertsp.Readertsp.open_path

Because if I don't add (wrapped false) to the dune mcts file, it puts all libraries in an only module called Mcts. With a dune file like this :

(library
 (wrapped false)
 (name mcts)
 (modules Readertsp)
 (libraries graphics))

I can call my function like this :

Readertsp.Readertsp.open_path

As highlighted by glennsl in the comment, my last mistake is that I was creating a module inside my Readertsp.ml file, which is already a module itself.

After deleting the module Readertsp = struct in my Readertsp.ml file, I can finally call

Readertsp.open_path
like image 112
Butanium Avatar answered Oct 19 '25 03:10

Butanium



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!