【发布时间】:2018-03-05 10:07:13
【问题描述】:
我目前正在为我的编程语言课自学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
当我运行 Makefile 的 test 规则时,我收到错误:
Error: Unbound module Get_file_buffer.
我一直在尝试将这些问题用作参考: Compiling multiple Ocaml files 和 Calling functions in other files in OCaml.
但我还没有让程序正确编译。如何正确编译上述代码,使程序正常运行?
【问题讨论】:
-
与您的问题无关,您的
read_test规则调用mail.cmx而不是main.cmx。
标签: compiler-errors compilation ocaml