1

Wrote the following code in flex and c file has generated but compiler shows "undefined reference to 'yywrap' " error.

%{
int nchar;
int nline;
%}
%%
[\n] {nline++;}
. {nchar++;}
%%
void main(void){
yylex();
printf("%d%d,nchar,nline");
}  

Questions like this have been asked and all the answers were based on ignoring yywrap function like adding #define yywrap() 1 Which in this case the program runs but doesn't work but I want it to work.

Nazanin
  • 38
  • 6
  • 1
    There is a reference to `yywrap` in your code that will not link. Did you define it? Are you using a version that doesn't automatically define a stub definition? If you don't want to suppress `yywrap` you might have to define it or update Flex. –  Nov 21 '17 at 20:26
  • I use flex-2.5.4a-1-bin version. how should I update it? I also tried other versions but the error was not fixed.i did not define yywrap! – Nazanin Nov 21 '17 at 20:39
  • The easiest thing is to define a stub definition. That is, if you don't actually need a `yywrap()`, create a no-op. You said you did some research, but many of the answers in https://stackoverflow.com/questions/1811125/undefined-reference-to-yywrap apply here. Are you using the `-lfl` linker option, for example? –  Nov 21 '17 at 20:48
  • I don't know what is -lf1 I need yywrap() because I want to work correctly the exe file that Is produced after compiling – Nazanin Nov 21 '17 at 20:57
  • Did you read those answers? They explain what you have to do. `-lfl` will ask the linker to link in a default reference. If a stubbed yywrap is causing a problem that is a different problem that you need to solve. You _need_ to provide a ref to `yywrap`, either implicitly or explicitly (or set the `noyywrap` option if you don't want it at all.) –  Nov 21 '17 at 21:02
  • It sounds like your real question is related to a problem that you get after stubbing out or configuring out `yywrap` from your code, which is a different problem than reported here. The linking issue you report is addressed by the Linked QA. I advise resubmitting your question so the real problem can be discussed. –  Nov 21 '17 at 21:12
  • Possible duplicate of [Undefined Reference To yywrap](https://stackoverflow.com/questions/1811125/undefined-reference-to-yywrap) –  Nov 21 '17 at 21:12

1 Answers1

1

You can define it yourself like this:

int yywrap() {  
// open next reference or source file and start scanning  
if((yyin = compiler->getNextFile()) != NULL) {  
  line = 0; // reset line counter for next source file
  return 0;  
   }
return 1;
}  

Or decide not to use it:

    %option noyywrap  

Or install it; which is like this for a redhat-based os:

    yum -y install flex-devel
    ./configure && make
Gudarzi
  • 486
  • 3
  • 7
  • 22