简体   繁体   中英

How to generate a proper makefile for an Ocaml project

I am learning how to compilers work. I read a tutorial on how to use Ocamllex and Ocamlyacc to read an input from a source code, generate the tokens and generate a syntatic tree in order to compute the execution of the program later. I had to recompile the code often while I was on the learning process and I decided to create a makefile to automatize this step. Since I am new to both Ocaml and makefiles I am struggling quite a bit to make the makefile work.

From my google research so far I could create this makefile, but the latest error I got was "make: *** No rule to make target 'lexer.mli', needed by 'depend'. Stop.".

# The Caml compilers. You may have to add various -I options.

CAMLC = ocamlc
CAMLDEP = ocamldep
CAMLLEX = ocamllex
CAMLYACC = ocamlyacc

# Lex stuff
LEXSOURCES = lexer.mll
LEXGENERATED = lexer.mli lexer.ml

# Yacc stuff
YACCSOURCES = parser.mly
YACCGENERATED = parser.mli parser.ml

GENERATED = $(LEXGENERATED) $(YACCGENERATED)

# Caml sources
SOURCES =  $(GENERATED) calc.ml
# Caml object files to link
OBJS = lexer.cmo parser.cmo calc.cmo

# Name of executable file to generate
EXEC = calc

# This part should be generic
# Don't forget to create (touch) the file ./.depend at first use.

# Building the world
all: depend $(EXEC)

$(EXEC): $(GENERATED) $(OBJS)
    $(CAMLC) $(OBJS) -o $(EXEC)

.SUFFIXES:
.SUFFIXES: .ml .mli .cmo .cmi .cmx
.SUFFIXES: .mll .mly

.ml.cmo:
    $(CAMLC) -c $<

.mli.cmi:
    $(CAMLC) -c $<

.mll.ml:
    $(CAMLLEX) $<

.mly.ml:
    $(CAMLYACC) $<

# Clean up
clean:
    rm -f *.cm[io] *.cmx *~ .*~ #*#
    rm -f $(GENERATED)
    rm -f $(EXEC)

# Dependencies
depend: $(SOURCES) $(GENERATED) $(LEXSOURCES) $(YACCSOURCES)
    $(CAMLDEP) *.mli *.ml > .depend

include .depend

How can I create a proper makefile for this task?

Ocamllex does not generate any mli file, you should remove lexer.mli from the list of files generated by ocamllex.

Note that if your aim is not to learn Makefile, it would be much easier to let dune, an ocaml-specific build system, handle the build process.

Similarly, ocamlyacc has been superseded in term of features by menhir. You might want to have a look.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM