如何在OCaml中编译多个文件?

龟人

我目前正在自学ocaml编程语言课程,无法在中编译多个文件ocaml

我在get_file_buffer.ml文件中定义了一个函数

源代码 get_file_buffer.ml

(* 
   Creating a function that will read all the chars 
   in a file passed in from the command argument.
   And store the results in a char list. 
*)

let read_file char_List =
    let char_in = open_in Sys.argv.(1) in   (* Creating a file pointer/in_channel *)
  try
    while true do
      let c = input_char char_in in         (* Getting char from the file *)
            char_List := c :: !char_List    (* Storing the char in the list *)
    done
  with End_of_file ->
        char_List := List.rev !char_List;   (* End of file was reaching, reversing char list *)
        close_in char_in;                   (* Closing the file pointer/in_channel *)

    (* Need to figure out how to catch if the file was not openned. *)
;; 

我正在尝试在我的函数中调用该函数 main.ml

源代码 main.ml

(* Storing the result of read_file to buffer which buffer is a char list reference *)
let buffer = ref [] in
      Get_file_buffer.read_file(buffer);

      print_string "\nThe length of the buffer is: ";
      print_int (List.length !buffer); (* Printing length of the list *)
      print_string ("\n\n");
      List.iter print_char !buffer;    (* Iterating through the list and print each element *)

为了编译程序,我正在使用 MakeFile

Makefile内容

.PHONY: all
all: test

#Rule that tests the program
test: read_test
    @./start example.dat

#Rules that creates executable
read_test: main.cmx get_file_buffer.cmx
    @ocamlc -o start get_file_buffer.cmx mail.cmx

#Rule that creates main object file
main.cmx: main.ml
    @ocamlc -c main.ml

#Rule that creates get_file_buffer object file
get_file_buffer.cmx: get_file_buffer.ml
    @ocamlc -c get_file_buffer.ml

当我运行自己的test规则时,Makefile出现错误:Error: Unbound module Get_file_buffer

我一直试图使用这些问题作为参考:编译多个Ocaml文件,在OCaml的其他文件中调用函数

但是我还无法使程序正确编译。如何正确编译以上代码以使程序正确运行?

比卡勒·林

而不是一个一个地构建* .ml文件。您有两个更好的选择,既有效又有效。

像这样将ocamlbuild与Makefile一起使用。

重命名main.mlstart.ml并使用以下Makefile

$猫Makefile

.PHONY: all test

all: start test

test: start
    @./start.native get_file_buffer.ml

start:
    ocamlbuild start.native

$使....

使用dune(以前的jbuilder),这是当今ocaml中最一致的构建工具

一种。jbuild在与文件相同的目录中创建一个*.ml文件。

$猫jbuild

(jbuild_version 1)

(executable
 ((name start)))

$ jbuilder构建start.exe

$ jbuilder exec-./start.exe get_file_buffer.ml

如果您愿意,可以通过创建make来驱动dune/jbuilderMakefile

$猫makefile

.PHONY: all test

all: start test

test: start
    jbuilder exec -- ./start.exe get_file_buffer.ml

start:
    jbuilder build start.exe

$使

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章