C语言 flex:仅在单独编译/链接时未定义对“yywrap”的引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14042903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:53:03  来源:igfitidea点击:

flex: undefined reference to `yywrap' only when compiling/linking separately

cflex-lexer

提问by Flukiluke

I've been learning to use Flex (the lexical analyser) and I've been compiling with the following command:

我一直在学习使用 Flex(词法分析器),并且一直在使用以下命令进行编译:

gcc -lfl -o test lex.yy.c

gcc -lfl -o test lex.yy.c

and all is well. However, I want to link it with other files, so I compile and link it separately with

一切都很好。但是,我想将它与其他文件链接,所以我单独编译并链接它

gcc -c lex.yy.c

gcc -c lex.yy.c

followed by

其次是

gcc -lfl -o test lex.yy.o

gcc -lfl -o test lex.yy.o

but gcc tells me that there is an undefined reference to yywrap(). So, what's going on here?

但是 gcc 告诉我有一个未定义的yywrap(). 那么,这里发生了什么?

I'm using Flex 2.5.35, gcc 4.7.2 and ld 2.22

我正在使用 Flex 2.5.35、gcc 4.7.2 和 ld 2.22

回答by Raj

add -lflat the end instead of beginning.

添加-lfl在末尾而不是开头。

回答by kartik7153

Use gcc lex.yy.c -ll. Otherwise it will yield an undefined reference to yywrap.

使用gcc lex.yy.c -ll. 否则,它将产生对 的未定义引用yywrap

回答by user207421

Alternatively, if you don't want to use the library, just #define yywrap()1 in your .l file, or provide a yywrap()method that returns 1.

或者,如果您不想使用该库,只需#define yywrap()在 .l 文件中使用1,或者提供yywrap()返回 1的方法。

It's documented.

它被记录在案。

回答by Hugo

My solution was:

我的解决方案是:

sudo apt-get install flex

sudo apt-get install flex

回答by Th. Thielemann

I created a file addon.c

我创建了一个文件 addon.c

int yywrap (void )
{
    return 1;
}

and added it to the target.

并将其添加到目标中。