How to fix a missing ld library for -lfl while com

2020-08-17 06:57发布

问题:

I am trying to translate my .spl file into a C file (because there is no compiler). I have an example "Hello World" .spl file, and I have downloaded the Shakespeare Programming Language .tar and extracted it, but I have no idea what to do next. I can't seem to find instructions in any documentation. Can anyone help?

Edit:

When I type make -f "Makefile", I get the following output:

bison --verbose -d grammar.y
gcc -O2 -Wall -c grammar.tab.c
gcc -O2 -Wall -c makescanner.c
gcc makescanner.o -O2 -Wall -o makescanner
./makescanner include > scanner.l
flex -Cem -t scanner.l > scanner.c
scanner.l:600: warning, rule cannot be matched
gcc -O2 -Wall -c scanner.c
<stdout>:5823: warning: ‘yyunput’ defined but not used
gcc -O2 -Wall -c strutils.c
gcc grammar.tab.o scanner.o strutils.o -O2 -Wall -lfl -o spl2c
ld: library not found for -lfl
collect2: ld returned 1 exit status
make: *** [spl2c] Error 1

回答1:

The error:

ld: library not found for -lfl

means that you've missing the library which is part of flex library.

To check if you've already flex library installed, you can look after it using locate:

locate libfl.a

OSX

If you don't have it, you can install install flex (if you're using brew):

brew install flex

If you still having the problems, you may try to pass the following variables manually during make, e.g.:

LDFLAGS="-L/usr/local/Cellar/flex/*/lib" CPPFLAGS="-I/usr/local/Cellar/flex/*/include" make -f "Makefile"

or:

MAKEFLAGS="-j8 -L/usr/local/Cellar/flex/*/lib -I/usr/local/Cellar/flex/*/include" make -f "Makefile"

eventually by modifying DYLD_FALLBACK_LIBRARY_PATH environment.

You can see which environment were passed by executing brew --env.

Note: Make sure that you'll change the paths according to your environment.

Related:

  • Dylibs and OS X

Linux

On most popular Linux distributions, you can install it by:

apt-get install flex

Related:

  • libfl.a file missing on RHEL

OS X (Homebrew)

Alternatively use Homebrew to install Shakespeare:

brew install shakespeare -v

Which should detect all your dependencies.



回答2:

You want to run the file called "Makefile" with the command make -f "Makefile", and if you have the commands ar gcc flex ranlib tar bison in your terminal, you should be fine.
You can test if you have a command by typing its name and then --help into your terminal.

Then you can use spl2c (which has now appeared in your SPL folder) with your code to make a .c file, which you can then put into gcc with spl.h to make a .o file, which you can finally put into gcc again with libspl.a to make a final, compiled program.

Just in case, put the files into gcc like this:
First pass: gcc programname.c spl.h -o programname.o
Second pass: gcc programname.o libspl.a -o compiledprogramname

You can comment if you want more info, hopefully this helps (or even works).



回答3:

If you're using a Mac, apparently OSX doesn't include the necessary libraries for flex, and that is what is causing the error: library not found for -lfl

However, there is supposed to be an equivalent replacement you can use, you'll just need to change a line in the Makefile. You should change this line:

$(CC) $^ $(CCFLAGS) -ll -o $@

to this:

$(CC) $^ $(CCFLAGS) -lfl -o $@

After doing that, trying running make again and see if you get any better results.



回答4:

The problem is that the Authors decided to link their program against the mostly useless libfl library, which is almost never needed, so not included in some flex distributions (in particlar, the one on MacOS).

It turns out that libfl only has two functions defined in it -- main and yywrap. The main in libfl is pretty much never used, and the yywrap might as well not be, because all it does is return 1.

It turns out you can fix the code easily enough:

  1. edit the file include/user_code_top.metaflex and add the line %option noyywrap to the end.

  2. edit the Makefile and remove the -lfl from the link line (just search for the string -lfl and remove those 4 characters wherever they appear)

Now you should be able to build it (though you may need to delete the file scanner.l if you previously ran make and got the failure, as the Makefile fails to detect that it need to be rebuilt after editing the metaflex file).



回答5:

Actually, I had the same problem on OS X Yosemite.

make -f "Makefile"
bison --verbose -d grammar.y
gcc -O2 -Wall -c grammar.tab.c
grammar.y:915:42: warning: illegal character encoding in string literal
      [-Winvalid-source-encoding]
  ...converter by Jon <C5>slund and Karl Hasselstr<F6>m.           *\n");
                      ^~~~                        ~~~~
1 warning generated.
gcc -O2 -Wall -c makescanner.c
gcc makescanner.o -O2 -Wall -o makescanner
./makescanner include > scanner.l
flex -Cem -t scanner.l > scanner.c
scanner.l:600: warning, rule cannot be matched
gcc -O2 -Wall -c scanner.c
<stdout>:5822:17: warning: unused function 'yyunput' [-Wunused-function]
    static void yyunput (int c, register char * yy_bp )
                ^
<stdout>:5867:16: warning: function 'input' is not needed and will not be
      emitted [-Wunneeded-internal-declaration]
    static int input  (void)
               ^
2 warnings generated.
gcc -O2 -Wall -c strutils.c
gcc grammar.tab.o scanner.o strutils.o -O2 -Wall -lfl -o spl2c
ld: library not found for -lfl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [spl2c] Error 1

Solution

I was able to make the "Makefile" after changing:

spl2c: grammar.tab.o scanner.o strutils.o
  $(CC) $^ $(CCFLAGS) -lfl -o $@

to

spl2c: grammar.tab.o scanner.o strutils.o
  $(CC) $^ $(CCFLAGS) -ll -o $@

In the meantime I installed the flex library, but I'm not sure whether it helped or not. In case my solution doesn't work without the flex library, you can simply install it using:

brew install flex

(Here is how to get the brew: http://brew.sh)