Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

flex-lexer

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

like image 614
Flukiluke Avatar asked Dec 26 '12 15:12

Flukiluke


3 Answers

add -lfl at the end instead of beginning.

like image 68
Raj Avatar answered Nov 12 '22 07:11

Raj


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

like image 4
kartik7153 Avatar answered Nov 12 '22 09:11

kartik7153


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.

like image 2
user207421 Avatar answered Nov 12 '22 08:11

user207421