flex: undefined reference to `yywrap' only when compiling/linking separately
Asked Answered
S

5

8

I've been learning to use Flex (the lexical analyser) and I've been compiling with the following command:

gcc -lfl -o test lex.yy.c

and all is well. However, I want to link it with other files, so I compile and link it separately with

gcc -c lex.yy.c

followed by

gcc -lfl -o test lex.yy.o

but gcc tells me that there is an undefined reference to yywrap(). So, what's going on here?

I'm using Flex 2.5.35, gcc 4.7.2 and ld 2.22

Sheaves answered 26/12, 2012 at 15:54 Comment(2)
did you try to add -lfl at the end instead of beginning?Cetus
okay, i will post that as answer.Cetus
C
22

add -lfl at the end instead of beginning.

Cetus answered 28/12, 2012 at 12:47 Comment(0)
D
4

Use gcc lex.yy.c -ll. Otherwise it will yield an undefined reference to yywrap.

Distillation answered 4/11, 2014 at 11:51 Comment(0)
P
2

Alternatively, if you don't want to use the library, just #define yywrap() 1 in your .l file, or provide a yywrap() method that returns 1.

It's documented.

Partizan answered 12/1, 2013 at 2:17 Comment(0)
A
1

My solution was:

sudo apt-get install flex

Accept answered 23/5, 2013 at 13:2 Comment(1)
Please explain how you had already generated a lex.yy.c file without having flex already installed.Partizan
A
1

I created a file addon.c

int yywrap (void )
{
    return 1;
}

and added it to the target.

Arabelle answered 24/10, 2016 at 22:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.