Search code examples
ocamlocaml-duneocaml-5

OCaml v5.0, "Module `Str' is unavailable"


Im trying to use the Ocaml Str library in Ocaml v5.0 (for regexp) by importing using "open Str" but I keep getting this error

"Module `Str' is unavailable"

followed by this alert

" OCaml's lib directory layout changed in 5.0. The str subdirectory has been automatically added to the search path, but you should add -I +str to the command-line to silence this alert (e.g. by adding str to the list of libraries in your dune file, or adding use_str to your _tags file for ocamlbuild, or using -package str for ocamlfind). "

I'm new to OCaml, any help would be greatly appreciated.

I've tried putting "add -I +str" into the command line however I got another error. I'm not sure how to add a library to my dune file so I haven't attempted this yet.


Solution

  • The Str library is a separate library, and it is not part of the standard library. Moreover, libraries are not imported with open statements in OCaml.

    You need to add a libraries stanza to your dune file:

    (libraries str)
    

    You can ignore the alert which is meant for previous users of the str library that were relying on the fact that the str library was previously co-located with the standard library.

    It is also generally advise to avoid the str library when possible and use one of the other regexp library from opam.