Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic Ocaml: How do I compile this?

Tags:

ocaml

opam

Just beginning with ocaml and am struggling with the various compilers and tooling. E.g. ocamlopt, ocamlc, ocamlbuild, corebuild and so on. So, how do I compile the following?

open Core.Std

module Regex = Re2.Regex

let ls pattern = 
  let pat = Regex.create_exn pattern in
  let matcher = Regex.matches pat in
  Sys.ls_dir "." 
  |> List.filter ~f:matcher
  |> List.iter ~f:(fun s -> print_string s; print_newline ())

let () =
  match In_channel.input_line stdin with
  | None -> print_string "No Input"
  | Some pat -> ls pat

In utop I can just #require "re2" and go from there.

Without the inclusion of the regular expressions module I would just use corebuild ls.native, assuming that the above code is placed into ls.ml.

[edit]

Have so far tried

ocamlbuild -use-ocamlfind -package core -package re2

which spit out

ocamlfind ocamldep -package core -package re2 -modules ls.ml > ls.ml.depends
ocamlfind ocamlc -c -package core -package re2 -o ls.cmo ls.ml
+ ocamlfind ocamlc -c -package core -package re2 -o ls.cmo ls.ml
ocamlfind: Error from package `threads': Missing -thread or -vmthread switch
Command exited with code 2.

So after some googling I was led to this blog I tried

ocamlbuild -tag thread -use-ocamlfind -package core -package re2

which spits out over 6000 lines of what looks like make output before failing with:

collect2: error: ld returned 1 exit status
File "caml_startup", line 1:
Error: Error during linking
Command exited with code 2.

so I'm not sure what to try next.

like image 897
fizz_ed Avatar asked Apr 20 '26 11:04

fizz_ed


1 Answers

I am using Ubuntu 14.04 on a 64bit machine. I ditched the apt-get versions of ocaml:

sudo apt-get remove --purge ocaml ocaml-base-nox ocaml-compiler-libs \ 
                            ocaml-interp ocaml-native-compilers \ 
                            ocaml-nox campl4 ocaml-base ocaml-docs opam 

Then I installed opam from source according to the instructions here.

Next I installed core, utop, and re2 with opam install core utop re2

And finally I ran ocamlbuild -use-ocamlfind -package re2 -package core -tag thread ls.native

which built the desired executable.

like image 108
fizz_ed Avatar answered Apr 22 '26 06:04

fizz_ed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!