Undefined reference to 'yylex()'
Asked Answered
C

4

18

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 $@ $<

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

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

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

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

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.

Crow answered 26/9, 2009 at 1:2 Comment(2)
You need to read the documentation for the C/C++ compiler that you're using. "-o" is an option that produces an output file; the very next argument must specify the name of that file. "-l" specifies a library; the documentation will tell you the naming convention and location where the compiler will look for this library. As a guess, you haven't installed the flex library.Flinn
i am getting same problem. My program is compiling with gcc, problem with g++ version.Photofinishing
G
18

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++

Gabriellia answered 7/12, 2011 at 23:18 Comment(0)
M
7

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.

Mahalia answered 16/7, 2019 at 16:16 Comment(2)
Can you summarize the solution in your answer? It would be helpful in case the link is no longer valid.Amortizement
My bad :' . Edited the answer.Mahalia
I
3

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

flex 2.6.4

bison 3.8.2

Invisible answered 10/7, 2022 at 8:54 Comment(0)
F
1

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

$(CPP) $^ /path/of/flex/lib/libfl.a -o $(PROGRAM)
Foxy answered 27/9, 2009 at 11:2 Comment(1)
I have it 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.Crow

© 2022 - 2024 — McMap. All rights reserved.