构建错误:未定义引用`yyFlexLexer :: yyFlexLexer(std :: istream *,std :: ostream *)

问题描述 投票:1回答:1

我在Windows机器上的应用程序中使用了Flex,编译器是mingw32-make。我的My C ++代码出现了构建错误。

我已经完成了Flex安装,并且已经在PATH上完成了路由包含和lib目录。 代码行是:

const char * filename;

std::fstream file;

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

yyFlexLexer * scanner;

scanner = new yyFlexLexer(&file);

错误是:

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

请帮我解决这个问题。

提前致谢!

c++ flex-lexer
1个回答
0
投票
File.cpp:63: undefined reference to `yyFlexLexer::yyFlexLexer(std::istream*, std::ostream*)

这意味着您的c ++词法分析器未定义或以不同的名称定义。

如果不编译和链接flex文件,则无法编写以下内容:

#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);
}

如果没有编写flex文件,运行flex(默认生成lex.yy.cc),然后编译和链接生成的代码,上述操作将无法工作。你可以在Generating C++ Scanners上阅读所有相关内容。

如果您编译并链接了生成的代码,则在创建命名扫描程序时仍可能会出现此错误。基本上,如果您的程序有多个扫描仪,为不同的扫描仪提供不同的名称是正确的解决方案。在这种情况下,您需要访问正确的名称。

这一切都在弹性手册中,在Generating C++ Scanners部分。我在这里引用:

如果要创建多个(不同的)词法分析器类,可以使用`-P'标志(或`prefix ='选项)将每个yyFlexLexer重命名为其他xxFlexLexer。然后,每个词法分析器类可以在其他源中包含“<FlexLexer.h>”,首先重命名yyFlexLexer,如下所示:

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

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

例如,如果您为其中一个扫描仪使用了'%option prefix =“xx”'而对另一个扫描仪使用了'%option prefix =“zz”'。

这是丑陋和棘手的,但它是flex与C ++一起工作的方式。自90年代或之前以来,这已被证明是实验性的。我认为它不会改变。

© www.soinside.com 2019 - 2024. All rights reserved.