Take the 2-minute tour ×

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.

share|improve this question
add comment

3 Answers

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


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.

share|improve this answer
I don't think your solution would work because he mentions that he wants to link the ".cmo" files ultimately generated. So if I try and compile a simple that uses some functions of the "Str" module and compile it like you said. If i load the ".cmo" in the toplevel, I still get an error saying undefined global Str –  Vivek Pradhan Apr 12 '13 at 13:32
The question is about linking a standalone program. The answer for toplevel is slightly different. In toplevel, you can load Str by saying #load "str.cma". –  Jeffrey Scofield Apr 12 '13 at 14:52
... or invoking the ocaml toplevel with ocaml str.cma. –  nlucaroni Apr 13 '13 at 3:27
add comment

I think you need to use '-cclib ' compiler directive. The module name shouldn't include the file ending like .cma. Below is what I did when trying to use the unix and threads modules. I think you need to use some combination of the 'custom' and 'cclib' compiler directives.

ocamlc -custom unix.cma threa.ml -cclib -lunix

Look at chapter 7 of this book for help: http://caml.inria.fr/pub/docs/oreilly-book/html/book-ora063.html

And look at coverage of compiler directives here: http://caml.inria.fr/pub/docs/manual-ocaml-4.00/manual022.html#c:camlc

share|improve this answer
add comment
ocamlc calc.ml str.cma -o calc
File "calc.ml", line 1:
Error: Error while linking calc.cmo:
Reference to undefined global `Str'

Code is very simple, to cut down scruff.

let split_into_words s = 
    Str.split ( Str.regexp "[ \n\t]+") s ;;

let _ = 
    split_into_words "abc def ghi" ;;

On ocaml 4.0.2. Obviously, there is a problem here, but I am too much of a beginner to understand what it is. From toplevel it seems work fine with #load "str.cma", so there is something here we don't understand. Anyone know what it is?

share|improve this answer
You need to put str.cma before calc.ml. –  Thomas Leonard Dec 14 '13 at 21:25
add comment

Your Answer


