9

I've got a file, test.lex, which I run through as

$ flex test.lex

That gives me lex.yy.c, which I try to compile with:

$ gcc lex.yy.c -lfl

This gives me the error ld: library not found for -lfl. I know the Flex specification is correct and lex.yy.c compiles fine on a Linux machine. Any suggestions?

Edit: I'm using the flex supplied by Apple.

Haden Pike
  • 259
  • 2
  • 7

2 Answers2

10

Some systems make libfl a separate package from flex, as it is rarely needed. The libfl library just contains two functions:

int main() {
    while (yylex());
    return 0;
}

int yywrap() {
    return 1;
}

Normally you'll want your own main function rather than the one from libfl, and defining yywrap yourself is trivial. Alternately, you can use %option noyywrap and not need it at all.

In your case, try just getting rid of the -lfl option. If you get an error about yywrap, add
%option noyywrap to the first section of your test.lex file.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
  • 1
    Interesting. I had to both add %option noyywrap to test.lex and leave off -lfl as one would expect, but I wonder why OS X couldn't find libfl? – Haden Pike Sep 26 '14 at 17:55
  • probably because you don't have it installed. – Chris Dodd Sep 26 '14 at 18:57
  • I assumed that since I have the flex binary, I have libfl. What I do have is libl.a, which seems to provide the same functionality. I'm using the flex supplied by Apple. – Haden Pike Sep 26 '14 at 19:51
4

old topic but accepted answer did not help me.
so I'm adding this answer.

on macos use -ll (as in "library lex").
valid for macos 10.14 mojave.

also as @Chris Dodd said you can get rid of this dependency by specifying %option noyywrap in .l file and providing own main routine .y file.

vigilancer
  • 1,456
  • 14
  • 15