fla*_*isk 0 c lex makefile lexical-analysis
我想使用flex为我的词法分析器制作一个make文件,我已经尝试过很多make文件的模板,但它没有用,所以请帮我构建一个这里是编译代码的行:
lex -t lexical.l > lexical.c
cc -c -o lexical.o lexical.c
cc -o lexy lexical.o -ll
Run Code Online (Sandbox Code Playgroud)
如果您正在使用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) $< > $@
Run Code Online (Sandbox Code Playgroud)
确实如此.以类似的方式,您可以检查GNU make是否知道如何从'%.c'构建'%.o'和从'%.o'构建'%'可执行文件.
假设lexical.l当前目录中有一个并且没有makefile,那么让我们看看如何构建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
Run Code Online (Sandbox Code Playgroud)
大.我们所想念的只是-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
Run Code Online (Sandbox Code Playgroud)
瞧!因此,您的makefile可以简短
LDLIBS=-ll
all: lexical
Run Code Online (Sandbox Code Playgroud)