I have a simple "language" that I'm using Flex(Lexical Analyzer), it's like this:
/* Just like UNIX wc */
%{
int chars = 0;
int words = 0;
int lines = 0;
%}
%%
[a-zA-Z]+ { words++; chars += strlen(yytext); }
\n { chars++; lines++; }
. { chars++; }
%%
int main()
{
yylex();
printf("%8d%8d%8d\n", lines, words, chars);
}
The I run a flex count.l
, all goes ok without errors or warnings, then when I try to do a cc lex.yy.c
I got this errors:
ubuntu@eeepc:~/Desktop$ cc lex.yy.c
/tmp/ccwwkhvq.o: In functionyylex': lex.yy.c:(.text+0x402): undefined reference to
yywrap'
/tmp/ccwwkhvq.o: In functioninput': lex.yy.c:(.text+0xe25): undefined reference to
yywrap'
collect2: ld returned 1 exit status
What is wrong?
The scanner calls this function on end of file, so you can point it to another file and continue scanning its contents. If you don't need this, use
Although disabling
yywrap
is certainly the best option, it may also be possible to link with-lfl
to use the defaultyywrap()
function in the libraryfl
(i.e.libfl.a
) provided by flex. Posix requires that library to be available with the linker flag-ll
and the default OS X install only provides that name.flex doesn't always install with its development libraries (which is odd, as it is a development tool). Install the libraries, and life is better.
On Redhat base systems:
On Debian based systems
As a note for followers, flex 2.6.3 has a bug where libfl.a "typically would" define yywrap but then doesn't in certain instances, so check if that's your version of flex, might be related to your problem:
https://github.com/westes/flex/issues/154
I prefer to define my own yywrap(). I'm compiling with C++, but the point should be obvious. If someone calls the compiler with multiple source files, I store them in a list or array, and then yywrap() is called at the end of each file to give you a chance to continue with a new file.