使用flex生成的文件时出现问题

时间:2022-06-01 12:40:21

I am trying to setup a project that uses flex (fast lex, not the adobe one). I am running on Ubuntu and I installed flex via the apt-get method.

我正在尝试设置一个使用flex的项目(快速lex,而不是adobe one)。我在Ubuntu上运行,我通过apt-get方法安装了flex。

I have googled the compile error and I have either found people who just create their own patches around it or a lot of forums where people ask and nobody answers.

我已经搜索了编译错误,我找到的人只是围绕它创建了自己的补丁,或者是很多人们提出要求而没有人回答的论坛。

This is my .ll file

这是我的.ll文件

%option c++

%%

%%

It generates a lex.yy.cc file which I include in my main file.

它生成一个lex.yy.cc文件,我将其包含在我的主文件中。

#include "lex.yy.cc"

int main ()
{
    return 0;
}

The error I get are a lot of "multiple definition" errors like this.

我得到的错误是很多像这样的“多重定义”错误。

lex.yy.cc | 511 | multiple definition of `yyFlexLexer::yylex() ' |

lex.yy.cc | 511 | `yyFlexLexer :: yylex()'|的多重定义

I am pretty stuck, the flex version is 2.5.35 and I am using the gcc compiler through the code::blocks editor.

我很困难,flex版本是2.5.35,我通过code :: blocks编辑器使用gcc编译器。

If I compile the main file straight through the terminal. I get this.

如果我直接通过终端编译主文件。我明白了

undefined reference to `yyFlexLexer::yywrap()'

未定义的引用`yyFlexLexer :: yywrap()'

1 个解决方案

#1


2  

Found the problem! You should not include the generated file but compile it and include it as an object file.

发现问题了!您不应该包含生成的文件,而是将其编译并将其作为目标文件包含在内。

flex file.ll 
g++ -c lex.yy.cc 
g++ -o main main.cpp lex.yy.o

#1


2  

Found the problem! You should not include the generated file but compile it and include it as an object file.

发现问题了!您不应该包含生成的文件,而是将其编译并将其作为目标文件包含在内。

flex file.ll 
g++ -c lex.yy.cc 
g++ -o main main.cpp lex.yy.o