在main.c中,我调用getToken函数,该函数包含在flex生成的lex.yy.c中。我想用makefile来编译它们
CC = gcc
TARGET = lexic
OBJS = util.o main.o
$(TARGET) : lex.yy.c util.o main.o
$(CC) lex.yy.c -ll util.o main.o
util.o : globals.h util.h util.c
$(CC) -c -o util.o util.c
main.o : globals.h util.h main.c
$(CC) -o main.o main.c
我这样做了,但是编译器找不到getToken函数的位置。
我该如何解决?
答案 0 :(得分:0)
问题是您没有构建名为main.o
的对象文件,而是您的规则尝试使用该名称构建可执行文件。
解决问题的最简单,最直接的方法是在构建时添加-c
标志:
main.o : globals.h util.h main.c
$(CC) -c -o main.o main.c
# ^^
# Note flag added
“更好”并且至少更简单的方法是依靠make
所拥有的implicit rules来创建对象和可执行文件。
然后,您可以简单地让Makefile
看起来像这样:
# CFLAGS is the C compiler flags
# Add flags to enable verbose warnings (always a good idea)
CFLAGS = -Wall -Wextra -pedantic
TARGET = lexic
# The libraries to link the target application with
LDLIBS = -ll
# By default the make program uses the first target
default: $(TARGET)
# Because of the implicit rules, make will be able to link the executable by itself
$(TARGET): lex.yy.o util.o main.o
# Also because of implicit rules, object files will be created automatically as well
# But we list them here to specify their header-file dependencies
util.o main.o: globals.h util.h
重要说明:Lex文件(包括Flex)的隐含规则是从文件X.c
创建源文件X.l
。因此,除非您的Lex文件的名称为lex.yy.l
,否则您需要在上面的lex.yy.o
中更改对象文件Makefile
的名称。