flex を使用して語彙アナライザー用のメイク ファイルを作成したいのですが、多くのメイク ファイルのテンプレートを試しましたが、うまくいきませんでした。
lex -t lexical.l > lexical.c
cc -c -o lexical.o lexical.c
cc -o lexy lexical.o -ll
flex を使用して語彙アナライザー用のメイク ファイルを作成したいのですが、多くのメイク ファイルのテンプレートを試しましたが、うまくいきませんでした。
lex -t lexical.l > lexical.c
cc -c -o lexical.o lexical.c
cc -o lexy lexical.o -ll
GNU make を使用している場合は、makefile はまったく必要ありません。ビルトイン ルールがユース ケースをカバーします。
組み込みのルールを表示して、make が '%.l' を '%.c' に変換する方法を知っているかどうかを確認してみましょう。
$ make -p | grep -A6 '\.l\.c'
make: *** No targets specified and no makefile found. Stop.
.l.c:
# Implicit rule search has not been done.
# Modification time never checked.
# File has not been updated.
# recipe to execute (built-in):
@$(RM) $@
$(LEX.l) $< > $@
します。同様の方法で、GNU make が '%.c' から '%.o' をビルドし、'%.o' から '%' 実行可能ファイルをビルドする方法を知っていることを確認できます。
現在のディレクトリに があり、makefile がないと仮定して、lexical.l
make build がどのようになるかを見てみましょうlexical
。
$ make -n lexical
rm -f lexical.c
lex -t lexical.l > lexical.c
cc -c -o lexical.o lexical.c
cc lexical.o -o lexical
rm lexical.c lexical.o
偉大な。私たちが見逃しているのは、-ll
あなたが求めたリンクのフラグだけです. に追加しましょうLDLIBS
。
$ make -n lexical LDLIBS=-ll
rm -f lexical.c
lex -t lexical.l > lexical.c
cc -c -o lexical.o lexical.c
cc lexical.o -ll -o lexical
rm lexical.c lexical.o
ほら!その結果、メイクファイルは次のように短くすることができます
LDLIBS=-ll
all: lexical
出発点は
LEX = lex
.l.c:
$(LEX) -t $< >$@
.c.o:
$(CC) -o $@ -c $<
lexy: lexical.o
$(CC) -o $@ $^ -ll
これは、ルールや依存関係の追跡などで拡張する必要がありますがclean
、Makefile がどのように機能するかを理解できるはずです。
GNU make は、これに必要なルールをすでに定義しています。これを次の名前のファイルに入れるだけですMakefile
LDLIBS = -ll
lexy: lexical.o
lexical.o: lexical.l
そして走る
$メイク
これで完了です。