对“yylex()”的未定义引用

发布于 2024-08-05 16:10:29 字数 953 浏览 1 评论 0原文

我正在尝试使用 flex 和 bison 创建一种简单的脚本语言。现在,我只是想让计算器工作。

但我无法编译它。当我在 .l 和 .ypp 文件上运行此 makefile: 时

OBJECTS = hug.tab.o hug.yy.o
PROGRAM = hug.exe

CPP = g++
LEX = flex
YACC = bison

.PHONY: all clean

all: $(OBJECTS)
$(CPP) $^ -o $(PROGRAM)

clean:
$(RM) *.o *.output *.tab.* *.yy.* $(PROGRAM)

%.tab.o: %.tab.cpp
$(CPP) -c -o $@ $<

%.tab.cpp: %.ypp
$(YACC) -vd $<

%.yy.o: %.yy.c
$(CPP) -c -o $@ $<

%.yy.c: %.l
$(LEX) -o $@ $<

%.o: %.cpp
$(CPP) -c -o $@ $<

,出现此错误:

undefined reference to `yylex()'

如果我为 all 发出命令,如下所示:

$(CPP) $^ -o $(PROGRAM) -lfl

它表示找不到 -lfl。如果我这样做:

$(CPP) $^ -o -lfl $(PROGRAM)

它会返回到未定义的引用错误。

抱歉我对此一无所知。

编辑:我已经安装了flex。我尝试将其从 -lfl 更改为 C:/GnuWin32/lib/libfl.a (我尝试使用 Windows,因为 Linux 在我的计算机上有奇怪的问题,而且我还没有 Mac),但它仍然有同样的错误。

I'm trying to use flex and bison to create a simple scripting language. Right now, I'm just trying to get a calculator working.

I can't get it to compile, though. When I run this makefile:

OBJECTS = hug.tab.o hug.yy.o
PROGRAM = hug.exe

CPP = g++
LEX = flex
YACC = bison

.PHONY: all clean

all: $(OBJECTS)
$(CPP) $^ -o $(PROGRAM)

clean:
$(RM) *.o *.output *.tab.* *.yy.* $(PROGRAM)

%.tab.o: %.tab.cpp
$(CPP) -c -o $@ 
lt;

%.tab.cpp: %.ypp
$(YACC) -vd 
lt;

%.yy.o: %.yy.c
$(CPP) -c -o $@ 
lt;

%.yy.c: %.l
$(LEX) -o $@ 
lt;

%.o: %.cpp
$(CPP) -c -o $@ 
lt;

on my .l and .ypp files, I get this error:

undefined reference to `yylex()'

And if I make the command for all like this:

$(CPP) $^ -o $(PROGRAM) -lfl

it says it couldn't find -lfl. And if I make it like this:

$(CPP) $^ -o -lfl $(PROGRAM)

it goes back to the undefined reference error.

Sorry I'm kind of clueless about this.

EDIT: I have flex installed. I tried changing it from -lfl to C:/GnuWin32/lib/libfl.a (I'm trying to use Windows because Linux has odd problems on my computers and I don't have a Mac yet), but it still has the same error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

愛上了 2024-08-12 16:10:29

问题是您正在使用 g++(将其视为 C++)而不是 gcc 来编译ug.yy.c。这是定义 yylex 的文件,因此通过将其编译为 C++,您最终会得到 C++ yylex 函数,而其他文件正在寻找 C yylex 函数。

尝试将 extern "C" int yylex(); 粘贴到 Hug.l 文件的第一部分,以便它将为 yylex 使用 C 链接而不是 C++

The problem is that you're compiling hug.yy.c with g++ (treating it as C++) instead of gcc. This is the file that defines yylex, so by compiling it as C++ you end up with a C++ yylex function while the other files are looking for a C yylex function.

Try sticking extern "C" int yylex(); into the first section of your hug.l file so that it will use C linkage for yylex instead of C++

勿忘初心 2024-08-12 16:10:29

基本上,执行以下任一操作:

  1. 在 Flex 文件中添加 %option noyywrap 并删除命令中的 -lfl 部分即可。

  2. #define yylex as extern "C" 也可以工作。因为,它可以防止 C++ 与名称混淆。

  3. 安装 flex-old 而不是 flex。 flex-old 默认情况下解决了像解决方案 2 一样的问题,所以你不必担心它。

Basically, do either of the following:

  1. Adding %option noyywrap in the flex file and removing the -lfl part in the command would be OK.

  2. #define yylex as extern "C" works too. Since, it prevents C++ mixing up with the name.

  3. Install flex-old rather than flex. flex-old bydefault solves the problem like solution 2 so u dont have to bother about it.

苍暮颜 2024-08-12 16:10:29

在 .l 文件中添加 %option noyywrap 并删除 -lfl 选项对我有用。

flex 2.6.4

野牛 3.8.2

Adding %option noyywrap in .l file and removing the -lfl option works for me.

flex 2.6.4

bison 3.8.2

捎一片雪花 2024-08-12 16:10:29

你安装了flex库吗?如果是,请尝试类似的方法

$(CPP) $^ /path/of/flex/lib/libfl.a -o $(PROGRAM)

have you installed the flex library ? if yes, try something like

$(CPP) $^ /path/of/flex/lib/libfl.a -o $(PROGRAM)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文