简体   繁体   中英

dyld: lazy symbol binding failed: Symbol not found: _yylex

I am new to flex and bison, on my Mac, I install flex and bison on my Mac using these:

brew install flex && brew link flex --force
brew install bison && brew link bison --force

This is my test1ll.l file

%{
  #include <iostream>
  using namespace std;
%}
%%
[0-9]+        { cout << "Number "; }
[a-zA-Z]+     { cout << "Word ";   }
[ \t]         ;
%%

then I run the following commands:

flex -otest1ll.c test1ll.l
g++ test1ll.c -otest1 -lfl
./test1

I got these errors:

dyld: lazy symbol binding failed: Symbol not found: _yylex
  Referenced from: /usr/local/opt/flex/lib/libfl.2.dylib
  Expected in: flat namespace

dyld: Symbol not found: _yylex
  Referenced from: /usr/local/opt/flex/lib/libfl.2.dylib
  Expected in: flat namespace

Abort trap: 6

Can someone explain and help me fix it?

If you are going to use C++, you will find it easier to make your flex scanner self-contained, rather than relying on libfl , which presumes C linkage.

Add %option noyywrap before the first %% (but see below) to avoid the need for yywrap and add a simple main at the end, after the second %% :

int main() {
  while (yylex()) {}
  return 0;
}

Personally I prefer:

%option noinput nounput noyywrap nodefault

The first two options make it possible to compile with -Wall if you don't use input() or unput() and the last one will cause flex to complain if your scanner does not recognise some input. In this case, it would have flagged the fact that your scanner fails to act on non-alphanumeric characters, just echoing them to standard out. (But perhaps that was intentional.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM