Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error linking module in ocaml

I am a complete beginner with Ocaml programming and I am having trouble linking a module into my program. Actually I am doing some regular expression checking and I have written a function that basically tokenizes a string based on a separator string using the Str module . So i use the functions defined in the library like this:

Str.regexp_string /*and so on*/

However, when I try to compile the ml file, I get an error suggesting that I have an undefined global Str . We use List functions by typing in List.length and so on just like I did for Str without having to explicitly include the specific module. I tried

open Str;;
include Str;; /*None of these work and I still get the same error*/

However if in the toplevel I use

load "str.cma" /*Then the program works without problems*/

I want to include the module in the ml file because I have to in the end link 3 cmo's to get the final executable(which is not run in the toplevel). I know this is a really basic question but I am having trouble solving it. Thanks in advance.

like image 598
Shashish Chandra Avatar asked Apr 12 '13 12:04

Shashish Chandra


2 Answers

Just add str to the libraries field of your dune file.

like image 61
David 天宇 Wong Avatar answered Oct 11 '22 18:10

David 天宇 Wong


You don't need to add anything in your file foo.ml. You do need to tell the compiler where to find the Str module when compiling foo.ml . To do so, add it to the command line used to compile foo.ml:

ocamlc str.cma foo.ml

or

ocamlopt str.cmxa foo.ml

List and other modules from the standard library are accessible by default, so you don't need to tell the compiler about those often used modules.

like image 21
jrouquie Avatar answered Oct 11 '22 19:10

jrouquie