I am currently teaching myself ocaml
for my programming language class and I am having trouble compiling multiple files in ocaml
.
I have defined a function in my get_file_buffer.ml
file
Source code of 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. *)
;;
I am trying to call the function in my main.ml
Source code of 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 *)
In order to compile the program I am using a MakeFile
Makefile content
.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
When I run the test
rule of my Makefile
I get the error:
Error: Unbound module Get_file_buffer
.
I have been trying to use these question as references: Compiling multiple Ocaml files and Calling functions in other files in OCaml.
Yet I have not been able to get the program to compile correctly. How would I correctly compile the above code to make the program run correctly?