I’m running into some linker errors with flex when I try compiling the resulting scanner with g++. If I give flex the following (very simple) script:
%%
. ECHO;
Then run flex as
flex++ scanner.l
And then try compiling the resulting code as
g++ lex.yy.cc -ll
I get the following errors:
/tmp/ccD5WdY3.o:(.rodata._ZTV11yyFlexLexer[vtable for yyFlexLexer]+0x28): undefined reference to `yyFlexLexer::yywrap()'
/usr/lib/libl.a(libmain.o): In function `main':
/build/buildd/flex-2.5.35/libmain.c:30: undefined reference to `yylex'
collect2: ld returned 1 exit status
I’m not sure why this is. Am I linking in the wrong library with -ll? If so, what should I do to fix this?
As you mentioned, I think
-llis unnecessary.Probably
liblcontains the code like the following:However, the scanner code generated by
flex++doesn’t contain free-standingfunction
yylex.So if the scanner is linked with the above
main, it cannot findyylex.When the scanner is generated by
flex++, that scanner needs a dedicatedmaininstead of linking with
-ll, and the simplestmainwill be like the following:As for the usage of C++ scanner,
this part
of flex document will help.
That being said, as the document says:
I cannot recommend using C++ scanner positively.