LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Unable to resolve compilation error in c file, provided with the lex/yacc book by John Levine. (https://www.linuxquestions.org/questions/programming-9/unable-to-resolve-compilation-error-in-c-file-provided-with-the-lex-yacc-book-by-john-levine-4175732968/)

ajiten 01-19-2024 10:16 PM

Unable to resolve compilation error in c file, provided with the lex/yacc book by John Levine.
 
2 Attachment(s)
Am facing a never before seen error on compiling the first file in the code given for the sole book available for learning lex/yacc, i.e. the book by John Levine, Tony Mason, & Doug Brown; titled: Lex & Yacc, second edition.

The error is shown before, as given, at the top, in the page #7 (of the book).

Code:

    $ cc lex.yy.c -o first -ll
    ch1-02.l:38:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
    38 | main()
      | ^~~~
    /usr/lib/gcc/x86_64-pc-cygwin/11/../../../../x86_64-pc-cygwin/bin/ld: cannot find -ll: No such file or
    directory
    collect2: error: ld returned 1 exit status

The screenshot of the cygwin terminal is attached herewith too.

P.S. If you are feeling uncomfortable by a minor typo in page#7, though even I didn't; then the typo screen is attached herewith, and the errata page is here.

NevemTeve 01-20-2024 12:18 AM

1. Use any text-editor to insert word `int` before `main`.
2. Do you a have a library called libl.so? If not, drop the `-ll` part.

You might want to check this topic which has a complete example linked in:
https://www.linuxquestions.org/quest...de-4175732020/

ajiten 01-20-2024 02:56 AM

3 Attachment(s)
Quote:

Originally Posted by NevemTeve (Post 6478036)
1. Use any text-editor to insert word `int` before `main`.
2. Do you a have a library called libl.so? If not, drop the `-ll` part.

Regarding your second point, please tell how to check for the presence of the same. Tried to check on cygwin, but it offers flex, and not lex; which as shown here should have instead libfl.so. The webpage, is shown in an attachment too.
The cygwin shows nothing like libfl, as shown in the attachment.

Kindly note that your first point failed to make any difference, except removing warning for the same effect; that gets introduced, when remove the return type of main (i.e., 'int'). This fact is shown in the last attachment.

Also, on removing the '-ll' part, it introduced new set of errors; stated below, as well as in the attachment.


Code:

$ cc lex.yy.c -o first
ch1-02.l:38:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
  38 | int main()
      | ^~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/../../../../x86_64-pc-cygwin/bin/ld: /tmp/ccJzPJqO.o:lex.yy.c:(.text+0x4ea): undefined reference to `yywrap'
/usr/lib/gcc/x86_64-pc-cygwin/11/../../../../x86_64-pc-cygwin/bin/ld: /tmp/ccJzPJqO.o:lex.yy.c:(.text+0x110a): undefined reference to `yywrap'
collect2: error: ld returned 1 exit status


pan64 01-20-2024 03:05 AM

it is not mentioned in that pdf, you need to use -lfl instead of -ll on cygwin. Probably.

ajiten 01-20-2024 03:11 AM

1 Attachment(s)
Quote:

Originally Posted by pan64 (Post 6478059)
it is not mentioned in that pdf, you need to use -lfl instead of -ll on cygwin. Probably.

It works (as shown in the attachment), but not sure why it did.
Please help by detailing the reason. There is additional reason for asking as got the same as answer, when earlier failed, on some other site (that seems is inactive now, as coudl directly ask from one author there). The lack of reason, has created the same problem again (due to forgetting in a long span of time), which shouldn't have occurred, if had known the reason too for the choice of '-lfl'.

pan64 01-20-2024 03:32 AM

cygwin is an interesting thing, looks like a linux, but actually it is based on windows. It really just looks like linux, trying to be as close to it as possible, but there are a lot of "other" things going on behind the scenes, it's definitely not linux. That's why you will always find differences, for example the name of that library is not the same. But actually I don't know why it has been altered.

ajiten 01-20-2024 03:39 AM

Quote:

Originally Posted by pan64 (Post 6478065)
cygwin is an interesting thing, looks like a linux, but actually it is based on windows. It really just looks like linux, trying to be as close to it as possible, but there are a lot of "other" things going on behind the scenes, it's definitely not linux. That's why you will always find differences, for example the name of that library is not the same. But actually I don't know why it has been altered.

So, you mean that the option used for compiling, i.e. '-lfl' is actually a library file.
If yes, then am again sorry, as need to know how to locate the same.
In the attachment, there is curiously in the 'bin' folder, of cygwin installation; 'lex' as a system file, but nothing like 'lfl'.
However, there is a 'flex' application file, and 'flex++' system file, in the same folder. The last file with prefix, as 'flex' is 'flexlink' in the same folder.

P.S. Unable to attach the stated attachment, as inspite of trying to attach it multiple times, it does not show up.

pan64 01-20-2024 03:53 AM

see the documentation cc -l<something> tells the linker to use a library named lib<something>.so. in general.

ajiten 01-20-2024 04:17 AM

Quote:

Originally Posted by pan64 (Post 6478072)
see the documentation cc -l<something> tells the linker to use a library named lib<something>.so. in general.

Please tell what to fill in the '<something>' part.

pan64 01-20-2024 04:30 AM

that is the name of the library. In your case it is either l or fl.

NevemTeve 01-20-2024 06:23 AM

@OP Please insert `int` before °main` that cannot be that hard.
When you have done that, I'll tell you the solution for the yywrap problem:
Code:

int yywrap(void) { return 1; }

ajiten 01-20-2024 07:54 AM

Quote:

Originally Posted by NevemTeve (Post 6478099)
@OP Please insert `int` before °main` that cannot be that hard.

Dear sir,
I did it, though otherwise it brings a warning.
I mean, not an error; unless you count warnings as errors.

P.S. Have sent a message to you on 16th, for which not sure why no response is coming.

ajiten 01-20-2024 08:05 AM

1 Attachment(s)
Quote:

Originally Posted by pan64 (Post 6478072)
see the documentation cc -l<something> tells the linker to use a library named lib<something>.so. in general.

Sorry, but have got out of touch with cygwin.

I tried :
1. help cc,
2. info cc,
3. man -k cc,

though none worked. Please tell the correct command. Also, the command should be able to detail about the -lfl option too.

NevemTeve 01-20-2024 08:13 AM

Off; no private messages, please
On: kindly read my previous posts: `-ll` is to be dropped; add this line into your program;
Code:

int yywrap(void) { return 1; }
PS: basic usage of the C-compiler should have been learnt already, but nonetheless here is a quickstart document: https://www3.ntu.edu.sg/home/ehchua/.../gcc_make.html

ntubski 01-20-2024 10:04 AM

Quote:

Originally Posted by pan64 (Post 6478072)
see the documentation cc -l<something> tells the linker to use a library named lib<something>.so. in general.

In this case it is lib<something>.a, see https://cygwin.com/cygwin/packages/x...flex-2.6.4-0.1:

Quote:

2017-04-16 08:00 5470 usr/lib/libfl.a
Also https://gcc.gnu.org/onlinedocs/gcc-1...ndex-Libraries:
Quote:

-llibrary
-l library

Search the library named library when linking. (The second alternative with the library as a separate argument is only for POSIX compliance and is not recommended.)

The -l option is passed directly to the linker by GCC. Refer to your linker documentation for exact details. The general description below applies to the GNU linker.

The linker searches a standard list of directories for the library. The directories searched include several standard system directories plus any that you specify with -L.

Static libraries are archives of object files, and have file names like liblibrary.a. Some targets also support shared libraries, which typically have names like liblibrary.so. If both static and shared libraries are found, the linker gives preference to linking with the shared library unless the -static option is used.

It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, 'foo.o -lz bar.o' searches library 'z' after file foo.o but before bar.o. If bar.o refers to functions in 'z', those functions may not be loaded.



All times are GMT -5. The time now is 05:58 PM.