2

I have used Flex in my application on windows machine and the compiler is mingw32-make. I got an an build error on My C++ code.

I have done Flex installation and I have done routed include and lib directory on PATH.
The code line is:

const char * filename;

std::fstream file;

file.open(filename, std::ios_base::in);

yyFlexLexer * scanner;

scanner = new yyFlexLexer(&file);

The error is:

"File.cpp:63: undefined reference to `yyFlexLexer::yyFlexLexer(std::istream*, std::ostream*)'"

Please help me to resolve this issue.

Thanks in advance!

CKE
  • 1,533
  • 19
  • 18
  • 29
  • You have not provided enough information to identify what your problem is. Please edit your question to include a [mcve]. That should include everything necessary to compile your project: all source files (if there is more than one) and the build procedure. In order to decrease the size of the post, please remove irrelevant details: in this case, you could probably remove all the lexical patterns and actions, leaving only the default. – rici Aug 31 '18 at 16:41

1 Answers1

3
File.cpp:63: undefined reference to `yyFlexLexer::yyFlexLexer(std::istream*, std::ostream*)

This means that your c++ lexer is either undefined, or defined under a different name.

You can't write the following, without compiling and linking the flex file:

#include <fstream>
#include <FlexLexer.h>
int main()
{
  const char * filename= "file.txt";;
  std::fstream file;
  file.open(filename, std::ios_base::in);

  // better use auto scanner = std::make_unique<yyFlexLexer>(&file)
  yyFlexLexer * scanner; 
  // leaks memory
  scanner = new yyFlexLexer(&file);
}

The above won't work without writing a flex file, running flex (which by default generates lex.yy.cc), and then compiling and linking the generated code. You can read all about it in Generating C++ Scanners.

If you did compile and link the generated code, you still may get this error if a named scanner was created. Basically, if your program has more than one scanner, giving different names to different scanners is the correct solution. In that case you need to access the correct name.

This is all in the flex manual, at the Generating C++ Scanners section. Which I quote here:

If you want to create multiple (different) lexer classes, you use the `-P' flag (or the `prefix=' option) to rename each yyFlexLexer to some other xxFlexLexer. You then can include `<FlexLexer.h>' in your other sources once per lexer class, first renaming yyFlexLexer as follows:

#undef yyFlexLexer 
#define yyFlexLexer xxFlexLexer 
#include <FlexLexer.h> 

#undef yyFlexLexer   
#define yyFlexLexer zzFlexLexer   
#include <FlexLexer.h> 

if, for example, you used `%option prefix="xx"' for one of your scanners and `%option prefix="zz"' for the other.

This is ugly and tricky, but it is the way flex works with C++. This has been documented as experimental since the 90's, or before. I assume that it won't change.

rici
  • 234,347
  • 28
  • 237
  • 341
Michael Veksler
  • 8,217
  • 1
  • 20
  • 33
  • You need to watch out for angle brackets and Markdown characters when you're cutting and pasting random text into an answer. Alternatively, you can use Ctl-K to turn the quote into a code block, which is a bit uglier but less work. I also changed the manual link to the official manual – rici Sep 02 '18 at 03:20
  • Thanks @MichaelVeksler. I have generated lex.asc.cc by using command 'Flex++ Lexer.l' in command line. I have added Flex **includepath & LIBS** on the _.pro_ file and then I tried to build my application in Qt Creator. I have added some more line to use flex on my application with reference [link](https://www.qtcentre.org/threads/3557-Flex-Bison-and-qmake). But I'm getting error 'lex.asc.cc:242:23: fatal error: FlexLexer.h: No such file or directory compilation terminated.' So Please help me to solve this issue. – Venkatesan SoundiraRajan Sep 05 '18 at 13:58
  • @VenkatesanSoundiraRajan you must find where `FlexLexer.h` is located on your system. The installation of `flex` should have installed this file. If it is not installed, then reinstall flex. If it is installed, then make sure your include path includes the folder that contains it. – Michael Veksler Sep 05 '18 at 14:01
  • Thanks @MichaelVeksler I have installed Flexlexer.h with **Flex 2.5.4** and rooted in 'PATH'. The location Flex is _D:\Public\Myinstall\GnuWin32\include_ – Venkatesan SoundiraRajan Sep 06 '18 at 05:34
  • @VenkatesanSoundiraRajan in that case, you didn't set up your include path correctly. This warrants a completely different question about setting up include paths in your compiler. Please Google for a setp-by-step explanation of how to set up include path in your compiler. Hint: it is different between Visual Studio and gcc. It is different between IDE and command line, between cmd.exe and bash, and so on. – Michael Veksler Sep 06 '18 at 06:11
  • Thanks @MichaelVeksler you may right but I have [cross verified](https://stackoverflow.com/questions/5456011/how-to-compile-lex-yacc-files-on-windows) with their example. I got an warning ` D:\LocalData\Downloads\hellex-master\hellex-master>gcc lex.yy.c y.tab.c -o hello.exe hello.l: In function 'main': hello.l:19:6: warning: implicit declaration of function 'yyparse' [-Wimplicit-function-declaration] yyparse();` and hello.exe is not execute in my command window. – Venkatesan SoundiraRajan Sep 07 '18 at 05:57
  • @VenkatesanSoundiraRajan yyparse() and y.tab.c are completely different beasts. They are in the output of your bison or yacc. Also, you did not set up your include path on that command line. You must provide it with `-I "D:\Public\Myinstall\GnuWin32\include"` if you are running on cmd.exe, or possibly `-I 'D:\Public\Myinstall\GnuWin32\include'` if in bash. Other than that, you should check why you are getting that warning – Michael Veksler Sep 07 '18 at 06:01
  • @VenkatesanSoundiraRajan anyway, searching for "*gcc include path*" will give you helpful documentation at: https://gcc.gnu.org/onlinedocs/cpp/Search-Path.html – Michael Veksler Sep 07 '18 at 06:16
  • Solved by proper environment variable PATH setting & fresh reinstalling all softwares pakcage, which is mentioned above. Thanks @MichaelVeksler – Venkatesan SoundiraRajan Dec 06 '18 at 06:25