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

Jens
  • 69,818
  • 15
  • 125
  • 179
Flukiluke
  • 85
  • 1
  • 1
  • 3

5 Answers5

22

add -lfl at the end instead of beginning.

Raj
  • 3,300
  • 8
  • 39
  • 67
4

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

honk
  • 9,137
  • 11
  • 75
  • 83
kartik7153
  • 333
  • 2
  • 11
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.

user207421
  • 305,947
  • 44
  • 307
  • 483
1

My solution was:

sudo apt-get install flex

Hugo
  • 2,569
  • 1
  • 19
  • 18
1

I created a file addon.c

int yywrap (void )
{
    return 1;
}

and added it to the target.

Th. Thielemann
  • 2,592
  • 1
  • 23
  • 38