Undefined Reference To yywrap
我有一个使用Flex(Lexical Analyzer)的简单"语言",就像这样:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | /* Just like UNIX wc */ %{ int chars = 0; int words = 0; int lines = 0; %} %% [a-zA-Z]+ { words++; chars += strlen(yytext); } \ { chars++; lines++; } . { chars++; } %% int main() { yylex(); printf("%8d%8d%8d\ ", lines, words, chars); } |
我运行
ubuntu@eeepc:~/Desktop$ cc lex.yy.c
/tmp/ccwwkhvq.o: In functionyylex': yywrap'
lex.yy.c:(.text+0x402): undefined reference to
/tmp/ccwwkhvq.o: In functioninput': yywrap'
lex.yy.c:(.text+0xe25): undefined reference to
collect2: ld returned 1 exit status
怎么了?
扫描程序在文件末尾调用此功能,因此您可以将其指向另一个文件并继续扫描其内容。如果不需要,请使用
1 | %option noyywrap |
在扫描仪规格中。
尽管禁用
我更喜欢定义自己的yywrap()。我正在使用C ++进行编译,但重点很明显。如果有人用多个源文件调用编译器,则将它们存储在列表或数组中,然后在每个文件的末尾调用yywrap(),使您有机会继续使用新文件。
1 2 3 4 5 6 7 8 | int yywrap() { // open next reference or source file and start scanning if((yyin = compiler->getNextFile()) != NULL) { line = 0; // reset line counter for next source file return 0; } return 1; } |
flex并不总是随其开发库一起安装(这很奇怪,因为它是开发工具)。安装库,生活会更好。
在Redhat基本系统上:
1 2 | yum -y install flex-devel ./configure && make |
在基于Debian的系统上
1 | sudo apt-get install libfl-dev |
作为对追随者的说明,flex 2.6.3有一个错误,其中libfl.a"通常会"定义yywrap,但在某些情况下却没有,因此请检查这是否是您的flex版本,是否与您的问题有关:
https://github.com/westes/flex/issues/154
1 | int yywrap(){return(1);} |
在程序末尾使用此代码。.简单