Quantcast
Channel: Active questions tagged ocaml - Stack Overflow
Viewing all articles
Browse latest Browse all 518

Compiling multifile projects in Ocaml, leading to Unbound Module error

$
0
0

Context

I am currently trying to learn the basics of OCaml (by doing the 2022 advent of code challenges). My aim is to get the basic down using only the OCaml standard library and writing my own make-files and not using package managers but instead directly running the commands like ocamlopt and ocamlc to compile and link a program together.

I'm getting to the point where I have enough program-logic abstracted out that it makes sense for me to start writing modules and popping into their own .ml files to be included into the main .ml file for my compiled programs. However, I am having trouble figuring out how to correctly issue command-line commands to execute this without error.

I need to have some source files pulled in from a certain (shared) directory that define a few helper functions & modules and then combined with a .ml file from a different directory which assembles those methods into a program to solve a problem.

The problem is if I ever try to pull in a module from another file I get the error Error: Unbound module <module name>, which I am unable to solve. To illustrate my problem and my attempt I have set up a toy example project that encapsulate the problem concretely. It is a simple "Hello World" split across two files using modules:

Project Directory Structure:


  • A
    • sourcea.ml
module ModuleA = structlet a = fun () -> print_endline "Hello World";;end
  • B
    • sourceb.ml
ModuleA.a();;
  • Output (empty)
  • MAKE
    • makefile
make:       ocamlc -i ../A/sourcea.ml > ../Output/sourcea.mli # construct the .mli file for sourcea    (cd ../Output &&   ocamlopt  -o sourcea  ../A/sourcea.ml) #compile sourcea.ml    (cd ../Output && ocamlopt -o program  ../Output/sourcea.cmx ../Output/sourcea.mli ../B/sourceb.ml  -I ../A ../B ../Output) # Attempt to compile the progtam using sourcea as a dependency for sourceb This line is where the error happens

I am trying to compile into native. I get the error

File "../B/sourceb.ml", line 1, characters 0-9:1 | ModuleA.a();;    ^^^^^^^^^Error: Unbound module ModuleA

I have looked up the documentation for ocamlopt and ocamlc and my current mental model is that I need to compile sourcea and produce its corresponding interface file and then use them as input for compiling togehter with sourceb.ml to produce an executable. The problem is that I can't figure out how to correctly command this to happen - and I have tried many permutations of commands and options that I think ought to be a correct way of commanding the compilation but none seems to work. I have even tried using -for-pack and -pack but that doesn't seem to work either.

Looking up solutions (both here and the internet in general) one finds people with similar basic difficulties. However, they are invariably using a third-party build system (usually Dune) or looking to make packages for opam, whereas I want to (at least for the time being) learn how to compile a Ocaml project directly.

The closet other question I can find is here, which offers the solution of -I-ing directories to search for include files for which I have tried to no avail.


To succinctly summerise my question I am asking how to compile together multiple (wlog. two) .ml files where the second depends on modules defined in the first without using external tooling (dune opam etc..) save for a makefile to execute the process. How would I go about altering my makefile to achieve this?


Viewing all articles
Browse latest Browse all 518

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>