如何为Ocaml项目生成正确的makefile

问题描述 投票:1回答:1

我正在学习编译器的工作方式。我阅读了一个教程,介绍如何使用Ocamllex和Ocamlyacc从源代码中读取输入,生成标记并生成合成树,以便稍后计算程序的执行。我在学习过程中经常重新编译代码,我决定创建一个makefile来自动化这一步。由于我是Ocaml和makefile的新手,因此我努力使makefile工作。

到目前为止,我的谷歌研究可以创建这个makefile,但我得到的最新错误是“make:***没有规则来制作目标'lexer.mli',需要'依赖'。停止。”

# 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

如何为此任务创建正确的makefile?

makefile compiler-construction ocaml ocamllex ocamlyacc
1个回答
0
投票

Ocamllex不会生成任何mli文件,您应该从ocamllex生成的文件列表中删除lexer.mli。

请注意,如果您的目标不是学习Makefile,那么让沙丘(一个特定于ocaml的构建系统)处理构建过程会容易得多。

同样,ocamlyacc在menhir的特征方面已被取代。你可能想看看。

© www.soinside.com 2019 - 2024. All rights reserved.