LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Why fl library is missing when flex is installed? (https://www.linuxquestions.org/questions/linux-software-2/why-fl-library-is-missing-when-flex-is-installed-4175559782/)

dedec0 11-24-2015 12:41 PM

Why fl library is missing when flex is installed?
 
I'm making this from a Debian GNU/Linux 8.2 (jessie). I have this Flex source code:

Code:

/* igual o wc do Unix */
%{
int chars = 0;
int words = 0;
int lines = 0;
%}

%%

[a-zA-Z]+  { words++; chars += strlen(yytext); }
\n          { chars++; lines++; }
.          { chars++; }

%%

main(int argc, char **argv)
{

  yylex();
  printf("%8d %8d %8d\n", lines, words, chars);
}


I save and compile the above source code with flex source-code.l. No error is shown (which means no output except the c file. After this, the expected step to work is gcc -lfl lex.yy.c, but this gives:

Code:

/tmp/ccxXhigZ.o: In function `main':
lex.yy.c:(.text+0x1bcf): multiple definition of `main'
/usr/lib/x86_64-linux-gnu/libfl_pic.a(libmain.o):(.text.startup+0x0): first defined here
/tmp/ccxXhigZ.o: In function `yylex':
lex.yy.c:(.text+0x42d): undefined reference to `yywrap'
/tmp/ccxXhigZ.o: In function `input':
lex.yy.c:(.text+0xf6a): undefined reference to `yywrap'
collect2: error: ld returned 1 exit status

Shouldn't the fl library exist, since flex is installed? This error is not expected because I'm following these steps from a book about Flex and Bison... :-/

dedec0 11-24-2015 01:05 PM

Quote:

Originally Posted by dedec0 (Post 5454657)
I'm making this from a Debian GNU/Linux 8.2 (jessie). I have this Flex source code:

Code:

/* igual o wc do Unix */
%{
int chars = 0;
int words = 0;
int lines = 0;
%}

%%

[a-zA-Z]+  { words++; chars += strlen(yytext); }
\n          { chars++; lines++; }
.          { chars++; }

%%

main(int argc, char **argv)
{

  yylex();
  printf("%8d %8d %8d\n", lines, words, chars);
}


I save and compile the above source code with flex source-code.l. No error is shown (which means no output except the c file. After this, the expected step to work is gcc -lfl lex.yy.c, but this gives:

Code:

/tmp/ccxXhigZ.o: In function `main':
lex.yy.c:(.text+0x1bcf): multiple definition of `main'
/usr/lib/x86_64-linux-gnu/libfl_pic.a(libmain.o):(.text.startup+0x0): first defined here
/tmp/ccxXhigZ.o: In function `yylex':
lex.yy.c:(.text+0x42d): undefined reference to `yywrap'
/tmp/ccxXhigZ.o: In function `input':
lex.yy.c:(.text+0xf6a): undefined reference to `yywrap'
collect2: error: ld returned 1 exit status

Shouldn't the fl library exist, since flex is installed? This error is not expected because I'm following these steps from a book about Flex and Bison... :-/

In the meantime, playing with aptitude on that machine, I have put together a few more details. Installed packages:

- libfl-dev 2.5.39-8+b1
- flex 2.5.39-8+b1
- bison 3.0.2dfsg-2

... :-/

astrogeek 11-24-2015 01:27 PM

The Flex libs exist or you would not see the lex.yy.c produced.

Do this, it will build and work as expected...

Code:

gcc lex.yy.c -lfl
It is not a Flex problem, the gcc args are order dependent.

From man gcc:

Quote:

You can mix options and other arguments. For the most part, the order you use
doesn't matter. Order does matter when you use several options of the same kind;
for example, if you specify -L more than once, the directories are searched in
the order specified. Also, the placement of the -l option is significant.

dedec0 11-24-2015 01:45 PM

Thank you! Now it worked.

I did not know that. Can you explain me why this order works and the other don't? I would guess that the correct order should not work because the source contains "extern int yywrap (void );" and it does not exist (yet).

Or maybe I should think reversed for gcc arguments?

astrogeek 11-24-2015 01:58 PM

I do not really know the details of how gcc operates at this level, but when -l is called before the lex.yy.c file has been compiled, it of course invokes any defines, and undefs, applicable at that moment.

The immediate clue for me was the error messages...

Code:

/tmp/ccxXhigZ.o: In function `main':
lex.yy.c:(.text+0x1bcf): multiple definition of `main'
/usr/lib/x86_64-linux-gnu/libfl_pic.a(libmain.o):(.text.startup+0x0): first defined here
/tmp/ccxXhigZ.o: In function `yylex':
lex.yy.c:(.text+0x42d): undefined reference to `yywrap'
/tmp/ccxXhigZ.o: In function `input':
lex.yy.c:(.text+0xf6a): undefined reference to `yywrap'
collect2: error: ld returned 1 exit status

The main function from flex is only defined if you have not defined one, and yywrap is only generated in the lex.yy.c code.

Because the flex libs are loaded before your code is compiled, it defines the main function and sees no yywrap. The fix would be to load and compile those libraries after the lex.yy.c, and your own options, have been compiled into object format.

Glad that worked for you!

dedec0 11-24-2015 05:50 PM

Complex. I never heard that a library could change (or similar action) its content at compilation time. I imagined that all -lXYZ would search for some .o or .so, but not for source code with precompilation details like #defines that affect what happens and is present in the end.

The multiple main definition error was something that puzzled me, but I assumed it was any secondary effect for the library not being correctly present.


All times are GMT -5. The time now is 06:39 PM.