基本Ocaml:我该如何编译?

时间:2014-10-05 21:22:30

标签: ocaml opam

刚刚从ocaml开始,我正在与各种编译器和工具进行斗争。例如。 ocamloptocamlcocamlbuildcorebuild等等。那么,我该如何编译以下内容呢?

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

utop我可以#require "re2"然后从那里开始。

如果没有包含正则表达式模块,我只会使用corebuild ls.native,假设上面的代码放在ls.ml中。

[编辑]

到目前为止已经尝试了

ocamlbuild -use-ocamlfind -package core -package re2

吐出的

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.

所以经过一些谷歌搜索后,我被引导到this blog我试过

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

,在失败之前吐出超过6000行的make输出:

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

所以我不确定下一步该尝试什么。

2 个答案:

答案 0 :(得分:6)

我在64位机器上使用Ubuntu 14.04。我放弃了ocaml的apt-get版本:

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 

然后我根据说明here从源代码安装了opam。

接下来,我使用opam install core utop re2

安装了core,utop和re2

最后我跑了ocamlbuild -use-ocamlfind -package re2 -package core -tag thread ls.native

构建了所需的可执行文件。

答案 1 :(得分:2)

我建议你使用绿洲工具。人们可能认为,这并不难。但它消除了你的所有图书馆负担。

让我们为您的项目创建一个简单的_oasis文件:

OASISFormat: 0.4
Name:        ls
Version:     0.1
Synopsis:    Testing oasis
Authors:     Fizz_ed
License:     MIT
Plugins:     META (0.4), DevFiles (0.4)
BuildTools: ocamlbuild, camlp4o
BuildDepends: core, camlp4, threads, 
              sexplib.syntax, 
              bin_prot.syntax, 
              comparelib.syntax, 
              herelib, 
              herelib.syntax

Executable "ls"
  Path: .
  MainIs: ls.ml
  CompiledObject: best
  BuildDepends: re2

在您创建此文件后,运行oasis setup命令(如果未安装oasis,请使用opam或您的软件包管理器安装)。

之后,您将拥有一个共同的configure脚本和makefile。所以你只需要输入

./configure
make 

并且您的文件将被编译。

或者你可以直接使用ocamlbuild,它也会起作用,因为oasis创建了所有必要的文件

ocamlbuild ls.native

关于该文件的一些解释。 preamle包含构建核心样式应用程序所需的一切。

可执行部分描述了您的可执行文件,并且还为re2库添加了依赖项。

更新:解决了一些问题。

相关问题