Flex and Yacc - Cannot find - lfl?

2019-01-20 09:14发布

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.

标签: yacc lex
5条回答
Melony?
2楼-- · 2019-01-20 09:41

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 :)

查看更多
ゆ 、 Hurt°
3楼-- · 2019-01-20 09:56

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.

查看更多
聊天终结者
4楼-- · 2019-01-20 09:59

For error: cannot find -lflx

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

Otherwise remove the -lfl argument wherever present.

查看更多
可以哭但决不认输i
5楼-- · 2019-01-20 10:01

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

查看更多
\"骚年 ilove
6楼-- · 2019-01-20 10:06

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
%%
 ...
%%
查看更多
登录 后发表回答