templatetypedef
templatetypedef

Reputation: 372674

Linker errors with flex and g++?

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?

Upvotes: 3

Views: 3803

Answers (1)

Ise Wisteria
Ise Wisteria

Reputation: 11669

As you mentioned, I think -ll is unnecessary.
Probably libl contains the code like the following:

int main() { return yylex(); }

However, the scanner code generated by flex++ doesn't contain free-standing function yylex. So if the scanner is linked with the above main, it cannot find yylex.
When the scanner is generated by flex++, that scanner needs a dedicated main instead of linking with -ll, and the simplest main will be like the following:

int main() {
  for ( yyFlexLexer l; l.yylex(); ) {}
}

As for the usage of C++ scanner, this part of flex document will help.

That being said, as the document says:

the present form of the scanning class is experimental and may change considerably between major releases

I cannot recommend using C++ scanner positively.

Upvotes: 4

Related Questions