Flex and Yacc - Cannot find - lfl?
Asked Answered
P

9

12

Hi I'm learing Lex and yacc. I created the following lex program.

%{
#include <stdio.h>
%}

%%
[0123456789]+           printf("NUMBER\n");
[a-zA-Z][a-zA-Z0-9]*    printf("WORD\n");
%%

I'm trying to run it using the following commands:

  1. lex example1.l
  2. cc lex.yy.c -o example1 -ll

also tried cc lex.yy.c -o example1 -lfl

When I enter the second command form above, I get error:

D:\workdir\flexyacc\Test3>gcc lex.yy.c -o Test -lfl
C:\Dev-Cpp\bin\..\lib\gcc\mingw32\3.4.2\..\..\..\..\mingw32\bin\ld.exe: cannot find -lfl
collect2: ld returned 1 exit status

I tried googling this error but no luck so far. Since I'm new in Lex programming, I'm not understanding how to fix this. Any help will be greatly appreciated. Thank so much in advance.

Pharmacopsychosis answered 16/4, 2012 at 14:13 Comment(3)
Not sure but think you have your parameters listed wrong have you tried: gcc lex.yy.c -lfl -o TestPudendas
If -lfl is not found, are you sure you need it? Try the build without, and see what happens. If you get main() undefined, then you'll need to write one yourself. Otherwise, if you get something else undefined, then you need to find the Flex library.Antimalarial
Hi Kaz, not working for me.Dangerfield
J
10

If you are using lex + yacc you can remove -lfl if you define yywrap function or, even better, if you use noyywrap option:

%option noyywrap
%%
 ...
%%
Jerome answered 3/2, 2015 at 14:12 Comment(0)
A
7

To compile the lex code, firstly you should have installed flex in your machine. If so , there will a file libfl.a. In my machine I've installed flex in 'C:\GnuWin32\lib'

gcc lex.yy.c -L"C:\GnuWin32\lib" -lfl

Ascendant answered 5/4, 2013 at 21:42 Comment(0)
A
6

I encountered the same problem and so i checked it out in the Internet and found a solution by workingcaptchabypass posted June 3, 2011 6:44 PM here

he said:

You could add this function instead and compile normally

yywrap()
{
}

And so i supplied the code in the .lex file before the main function. After doing that, it worked out the way it should :)

Abbess answered 7/9, 2012 at 11:42 Comment(1)
@Abbess and @caitriiona, yywrap should return a int value! If you want it to "do nothing" it must return a true value, like int yywrap(){ return 1;}Jerome
M
2

I have run into this issue when porting TXR to Windows using MinGW.

MinGW has a flex library for itself, but does not export it to the environment.

See here: http://lists.nongnu.org/archive/html/txr-users/2011-10/msg00001.html

The workaround is to use -L/usr/lib before -lfl. But think about this: it is a hack. Why? Because the path /usr/lib/ belongs to MinGW, the compilation environment's run-time.

/usr/lib is not where the toolchain is supposed to find libs for the Windows program being built (which is it's not in the library search path!)

That is to say, we are effectively stealing the build machine's native library in a cross-compile job.

This is like if you were cross-compiling, say, a Fedora program on Ubuntu, and helping yourself to Ubuntu's static library in /usr/lib that happens to be missing in the Fedora cross toolchain (taking advantage of the fact that the architecture and object file format happens to be the same).

It's definitely a bug in the way Flex is "packaged" in MingW.

Marnie answered 16/4, 2012 at 23:16 Comment(1)
The real reason for this may be that GNU Flex does not support cross-compiling very well. In my previous job, I patched Flex to support cross-compiling properly through the usual --host/--build options to the configure script. I had flex being built for the build machine's toolchain, but libfl.a being properly cross-compiled to the target machine (which was MIPS). Without this, Flex just builds both pieces for the build machine, which is what seems to have happend in MinGW: you have a build-machine flex and a build-machine libfl.a.Marnie
S
2

After having a hard time trying to fix the same problem as yours, I installed flex-old:

sudo apt install flex-old

Schaab answered 6/4, 2019 at 11:54 Comment(0)
S
1

Try using -ll instead of -lfl in gcc

Sweitzer answered 15/11, 2019 at 21:55 Comment(0)
V
1

If you are on macOSX then replace -lfl with -ll

Virile answered 23/2, 2021 at 6:56 Comment(0)
T
0

For error: cannot find -lflx

In your Makefile change: LEXLIB = -lfl to LEXLIB =.

Otherwise remove the -lfl argument wherever present.

Thenceforward answered 14/4, 2014 at 22:42 Comment(0)
D
0

just do this:

yum install flex-devel

Dub answered 15/8, 2023 at 8:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.