csv 如何告诉OCaml编译器最近安装的模块的路径

5us2dqdw  于 2022-12-06  发布在  其他
关注(0)|答案(1)|浏览(113)

我已经通过运行以下命令在Mac上安装了OCaml:

$ brew install opam
$ opam init --bare -a -y
$ opam switch create cs3110-2022fa ocaml-base-compiler.4.14.0

运行任何只使用标准库模块的OCaml代码都可以很好地工作。然后我想做一些它没有涉及的事情,例如reading CSV files

$ opam install csv

让我们尝试编译this code

open Printf
open Csv

let embedded_csv = "\
\"Banner clickins\"
\"Clickin\",\"Number\",\"Percentage\",
\"brand.adwords\",\"4,878\",\"14.4\"
\"vacation.advert2.adwords\",\"4,454\",\"13.1\"
\"affiliates.generic.tc1\",\"1,608\",\"4.7\"
\"brand.overture\",\"1,576\",\"4.6\"
\"vacation.cheap.adwords\",\"1,515\",\"4.5\"
\"affiliates.generic.vacation.biggestchoice\",\"1,072\",\"3.2\"
\"breaks.no-destination.adwords\",\"1,015\",\"3.0\"
\"fly.no-destination.flightshome.adwords\",\"833\",\"2.5\"
\"exchange.adwords\",\"728\",\"2.1\"
\"holidays.cyprus.cheap\",\"574\",\"1.7\"
\"travel.adwords\",\"416\",\"1.2\"
\"affiliates.vacation.generic.onlinediscount.200\",\"406\",\"1.2\"
\"promo.home.topX.ACE.189\",\"373\",\"1.1\"
\"homepage.hp_tx1b_20050126\",\"369\",\"1.1\"
\"travel.agents.adwords\",\"358\",\"1.1\"
\"promo.home.topX.SSH.366\",\"310\",\"0.9\""

let csvs =
  List.map (fun name -> name, Csv.load name)
           [ "examples/example1.csv"; "examples/example2.csv" ]

let () =
  let ecsv = Csv.input_all(Csv.of_string embedded_csv) in
    printf "---Embedded CSV---------------------------------\n" ;
    Csv.print_readable ecsv;

  List.iter (
    fun (name, csv) ->
      printf "---%s----------------------------------------\n" name;
      Csv.print_readable csv
  ) csvs;
  printf "Compare (Embedded CSV) example1.csv = %i\n"
         (Csv.compare ecsv (snd(List.hd csvs)))

let () =
  (* Save it to a file *)
  let ecsv = Csv.input_all(Csv.of_string embedded_csv) in
  let fname = Filename.concat (Filename.get_temp_dir_name()) "example.csv" in
  Csv.save fname ecsv;
  printf "Saved CSV to file %S.\n" fname

其结果是:

$ ocamlopt csvdemo.ml -o csvdemo         
File "csvdemo.ml", line 2, characters 5-8:
2 | open Csv
         ^^^
Error: Unbound module Csv

如何告诉OCaml编译器在哪里找到Csv模块路径?

imzjd6km

imzjd6km1#

这似乎是使用Dune的一个很好的地方,但是稍微老一点的话,你可以使用ocamlfind来定位包。

% cat test2.ml
open Csv

let () = print_endline "hello"
% ocamlopt -I `ocamlfind query csv` -o test2 csv.cmxa test2.ml
% ./test2
hello
%

或者:

ocamlfind ocamlopt -package csv -o test2 cvs.cmxa test2.ml

相关问题