OCaml - compiling a program as a library -
i have ocaml program(with main method - generates executable) , want use library.
i compiling program this: ocamlc -i somedir -g -unsafe lotsofcmofiles -o outputfile , program works fine.
now i'm removing line makes executable(something let _ = ...) , adding -a parameter compile command: ocamlc -a -i somedir -g -unsafe lotsofcmofiles -o outputfile.cma
but somehow can't load generated .cma file ocamltop , ocamlbrowser shows empty list. when try load ocamltop:
# #load "outputfile.cma";; error: reference undefined global `xyz' and i'm 100% sure xyz.cmo included in lotsofcmofiles.
am giving parameter wrong while compiling? or else, should load program in ocamltop ? (i'll use library in program, i'm giving ocamltop outputs example)
any helps appreciated.
edit: can managed compile , load @cago, can load library, , when don't remove main let _ = ... line it's automatically run when load .cma.
but still can't open modules. strangely, doesn't raise exception
open main but when call function module main:
# somefun;; error: reference undefined global `main' and ocamlbrowse still shows empty list. why that?
edit2: realized open main doesn't fail because have main module in same folder(even though didn't explicitly load it). if move .cma file somewhere else , load it, works(ie. main function runs automatically), can't open modules though ocamlobjinfo shows modules.
edit3: -i doesn't help:
$ ocaml ocaml version 4.00.1 # #load "lib.cma";; ok # open lib;; error: unbound module lib # $ ocaml -i libfolder ocaml version 4.00.1 # #load "toylib.cma";; ok # open lib;; # fun;; error: reference undefined global `lib'
some of cmo in lotsofcmofiles need know module xyz. need take care of dependency between cmo files.
for example:
toto.ml:
let x = "toto" titi.ml:
let y = toto.x ^ " titi" ocamlc -c toto.ml ocamlc -c titi.ml ocamlc -a titi.cmo toto.cmo -o lib.cma (* here probleme *) # #load "lib.cma" error: reference undefined global `toto' because titi depends on toto need change order of cmos:
ocamlc -a toto.cmo titi.cmo -o lib.cma # #load "lib.cma" # titi.y;; - : string = "toto titi" edit:
if cma in subdirectory example, when call ocaml need specify path:
ocaml -i subdir/ (* subdir contains lib.cma *) # #load "lib.cma" # toto.x;; - : string = "toto"
Comments
Post a Comment