build error : undefined reference to `yyFlexLexer::yyFlexLex
build error : undefined reference to `yyFlexLexer::yyFlexLexer(std::istream*, std::ostream*)
我在Windows机器上的应用程序中使用了Flex,编译器mingw32-make
。我在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*)'"
请帮助我解决此问题。
提前感谢!
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
(,然后编译和链接生成的代码,上述内容将不起作用。您可以在生成C++扫描仪中阅读所有相关信息。
如果确实编译并链接了生成的代码,则在创建了命名扫描程序时仍可能收到此错误。基本上,如果您的程序有多个扫描仪,则为不同的扫描仪指定不同的名称是正确的解决方案。在这种情况下,您需要访问正确的名称。
这一切都在 flex 手册的生成C++扫描仪部分。我在这里引用:
如果要创建多个(不同的(词法分析器类,请使用 "-P"标志(或"前缀="选项(将每个 yyFlexLexer 重命名为 其他一些 xxFlexLexer。然后,>
#undef yyFlexLexer
#define yyFlexLexer xxFlexLexer
#include <FlexLexer.h>
#undef yyFlexLexer
#define yyFlexLexer zzFlexLexer
#include <FlexLexer.h>
例如,如果您将"%选项前缀="xx"用于您的之一 扫描仪和"%选项前缀="zz"表示另一个。
这既丑陋又棘手,但这是flex与C++一起工作的方式。自 90 年代或更早以来,这已被记录为实验性。我认为它不会改变。
相关文章:
- "error: no matching function for call to"构造函数错误
- 调用专用模板时出错"no matching function for call to [...]"
- C++ Singleton - Prevent ::instance() to variable
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- Visual Studio Code "undefined reference to `WinMain@16'"
- 使用 GCC 卸载的 OpenMP 卸载失败,并出现"Ptx assembly aborted due to errors"
- 如何解决"invalid conversion from 'char' to 'const char*'"
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- Python str to C++ to Python str
- 为什么创建友元类的实例会导致"undefined reference to"错误?
- System.InvalidCastException - SQL to C++ - safe_cast<float>
- 使用 cmake 的 Linux 终端上的"Conversion to non-scalar type is requested"错误
- "no matching function for call to 'Vector::Vector'"错误
- vector<vector<double>> to mxArray using memcpy
- Tensorflow c++ api undefined reference to 'tflite::D efaultErrorReporter()'
- 当覆盖存在时调用基本虚拟"binded to object"函数
- OpenGL VBO Indexing ( How to compute Index Array)
- 为什么我会" void value not ignored as it ought to be"?
- build error : undefined reference to `yyFlexLexer::yyFlexLex