Search code examples
clinker

Fail to compile the Y86 simulatur (CSAPP)


I have problem compiling the Y86 simulator for doing practices in CSapp book . It seems variable lineno is defined multiple times

here is the error

 make 
    (cd misc; make all)
    make[1]: Entering directory '/home/platoali/CSapp/sim/sim/misc'
    gcc -Wall -O1 -g -c yis.c
    gcc -Wall -O1 -g -c isa.c
    gcc -Wall -O1 -g yis.o isa.o -o yis
    gcc -Wall -O1 -g -c yas.c
    flex yas-grammar.lex
    mv lex.yy.c yas-grammar.c
    gcc -O1 -c yas-grammar.c
    gcc -Wall -O1 -g yas-grammar.o yas.o isa.o -lfl -o yas
    /usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: yas.o:/home/platoali/CSapp/sim/sim/misc/yas.h:13: multiple definition of `lineno'; yas-grammar.o:(.bss+0x0): first defined here
    collect2: error: ld returned 1 exit status
    make[1]: *** [Makefile:32: yas] Error 1

Here are the declarations that I found

 $ grep -R "int lineno" * 
misc/isa.c:    int lineno = 0;
misc/yas.c:int lineno = 1; /* Line number of input file */
misc/yas.c:int lineno;  /* What line number am I processing? */
misc/yas.h:int lineno;
misc/hcl.lex:extern int lineno;
misc/hcl.y:int lineno = 1;
misc/hcl.tab.c:int lineno = 1;
misc/lex.yy.c:extern int lineno;

the code is accessible from: CS:APP3e Student Site

Can anybody help me how to solve this? It seems lineno is declared multiple times, but I can not find it where?

EDIT

    $ grep -R "lineno" * 
misc/isa.c:    int lineno = 0;
misc/isa.c:     lineno++;
misc/isa.c:             fprintf(stderr, "Line %d:%s\n", lineno, buf);
misc/isa.c:                 fprintf(stderr, "Line %d:%s\n", lineno, buf);
misc/yas.c:int lineno = 1; /* Line number of input file */
misc/yas.c:int lineno;  /* What line number am I processing? */
misc/yas.c:    fprintf(out, "Line %d, Byte %d: ", lineno, bytepos);
misc/yas.c:     fprintf(stderr, "Error on line %d: %s\n", lineno, message);
misc/yas.c:             lineno, bytepos, input_line);
misc/yas.c:    lineno = 1;
misc/yas.h:int lineno;
misc/hcl.lex:extern int lineno;
misc/hcl.lex:[\n]                  lineno++;
misc/hcl.lex:"#".*\n               lineno++ ;
misc/hcl.y:int lineno = 1;
misc/hcl.y:  fprintf(stderr, "Error, near line %d: %s\n", lineno, str);
misc/yas-grammar.lex:#{Char}*{Return}*{Newline}      {finish_line(); lineno++;}
misc/yas-grammar.lex:"//"{Char}*{Return}*{Newline}     {finish_line(); lineno++;}
misc/yas-grammar.lex:"/*"{Char}*{Return}*{Newline}   {finish_line(); lineno++;}
misc/yas-grammar.lex:{Blank}*{Return}*{Newline}      {finish_line(); lineno++;}
misc/yas-grammar.lex:<ERR>{Char}*{Newline} {fail("Invalid line"); lineno++; BEGIN 0;}

Solution

  • I had the same problem, it is about gcc. gcc-10 changed default from "-fcommon" to "-fno-common". You need to add "-fcommon" flag to Makefiles. For example,

    Old misc/Makefile:

    CFLAGS=-Wall -O1 -g
    LCFLAGS=-O1
    

    New misc/Makefile:

    CFLAGS=-Wall -O1 -g -fcommon
    LCFLAGS=-O1 -fcommon
    

    You can check here for more detail.