xclerc / ocamljava

http://www.ocamljava.org
Other
172 stars 14 forks source link

Failing to link a module/dependency in an OCaml script #28

Closed PioBeat closed 5 years ago

PioBeat commented 5 years ago

Hello! I encountered a problem when trying to generate the corresponding Java code for an OCaml program that is defining a dependency.

Consider the following example. The script main.ml is referring to a library:

open Libs
let () =
    let a = SimpleLib.saySomething in
    print_endline a

The library called SimpleLib is located in the directory ./libs/. For SimpleLib.ml:

let saySomething =
  "Hello!"

And SimpleLib.mli:

val saySomething : string

This call from the command-line ocamljava -java-package wraptest -c main.ml is producing this error:

File "main.ml", line 2, characters 0-9:
Error: Unbound module Libs

How can I tell ocamljava to include this library? I use dune as build system which generates also a *.a and *.cmxa file.

The question on StackOverflow is related https://stackoverflow.com/questions/26227269/failing-to-link-standard-library-in-ocaml-java to my issue but unfortunately, the answers did not help.

My system details are:

$ ocaml --version
The OCaml toplevel, version 4.07.1
$ ocamljava -version
4.01.0
$ java -version
openjdk version "1.8.0_191"
OpenJDK Runtime Environment (build 1.8.0_191-8u191-b12-0ubuntu0.18.04.1-b12)
OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)
xclerc commented 5 years ago

Dune does not support ocamljava, so you will have to resort to either ocamlbuild or make.

From what I understand, you should be able to manually build your project through the following commands:

cd libs
ocamljava -c SimpleLib.mli
ocamljava -c SimpleLib.ml
cd ..
ocamljava -I libs -c main.ml
ocamljava -o myprog.jar libs/SimpleLib.cmj main.cmj

Hope this helps.

PioBeat commented 5 years ago

Thank you for your answer. The solution works!

The only thing I had to do additionally was to remove the open Libs statement at the beginning of the main.ml file. Is this considered as an error?

Otherwise, ocamljava successfully produces the *.jar.

xclerc commented 5 years ago

The only thing I had to do additionally was to remove the open Libs statement at the beginning of the main.ml file. Is this considered as an error?

Sorry, I overlooked this part; indeed, OCaml (at the language level) does not use the file hierarchy. Your SimpleLib module is then not part of a libs module, it is a toplevel module.