compiler-constructionbisonflex-lexerc-minus-minus

Flex/Bison: Multiple definition of 'said function'


Since my code is a bit too long, I thought it would be easier to post a github link if anyone is willing to help me and needs the code: https://github.com/Pigums/Cminus-Compiler

In cygwin, I run these commands:

bison -d step3.y
flex step3.fl
gcc step3.tab.c lex.yy.c -lfl -o step3

Then the following errors pop up:

/tmp/ccdKHQL3.o:step3.tab.c:(.text+0x0): multiple definition of `CreateTemp'
/tmp/ccfXBuoP.o:lex.yy.c:(.text+0x0): first defined here
/tmp/ccdKHQL3.o:step3.tab.c:(.text+0x4a): multiple definition of `Insert'
/tmp/ccfXBuoP.o:lex.yy.c:(.text+0x4a): first defined here
/tmp/ccdKHQL3.o:step3.tab.c:(.text+0x140): multiple definition of `PrintSym'
/tmp/ccfXBuoP.o:lex.yy.c:(.text+0x140): first defined here
/tmp/ccdKHQL3.o:step3.tab.c:(.text+0x19f): multiple definition of `Display'
/tmp/ccfXBuoP.o:lex.yy.c:(.text+0x19f): first defined here
/tmp/ccdKHQL3.o:step3.tab.c:(.text+0x1e6): multiple definition of `Search'
/tmp/ccfXBuoP.o:lex.yy.c:(.text+0x1e6): first defined here
/tmp/ccdKHQL3.o:step3.tab.c:(.text+0x266): multiple definition of `Delete'
/tmp/ccfXBuoP.o:lex.yy.c:(.text+0x266): first defined here
/tmp/ccdKHQL3.o:step3.tab.c:(.text+0x2fd): multiple definition of `ASTCreateNode'
/tmp/ccfXBuoP.o:lex.yy.c:(.text+0x2fd): first defined here
/tmp/ccdKHQL3.o:step3.tab.c:(.text+0x3c0): multiple definition of `ASTattachleft'
/tmp/ccfXBuoP.o:lex.yy.c:(.text+0x3c0): first defined here
/tmp/ccdKHQL3.o:step3.tab.c:(.text+0x3f6): multiple definition of `PT'
/tmp/ccfXBuoP.o:lex.yy.c:(.text+0x3f6): first defined here
/tmp/ccdKHQL3.o:step3.tab.c:(.text+0x427): multiple definition of `ASTprint'
/tmp/ccfXBuoP.o:lex.yy.c:(.text+0x427): first defined here
/tmp/ccdKHQL3.o:step3.tab.c:(.text+0xa83): multiple definition of `compareFormals'
/tmp/ccfXBuoP.o:lex.yy.c:(.text+0xa83): first defined here
/tmp/ccdKHQL3.o:step3.tab.c:(.bss+0x0): multiple definition of `mem'
/tmp/ccfXBuoP.o:lex.yy.c:(.bss+0x14): first defined here
collect2: error: ld returned 1 exit status

Not sure what I'm doing wrong, tried looking up the error but I don't think the answers I got are the ones I'm looking for. What's the problem here?


Solution

  • #include "symtable.c"
    #include "ast.c"
    

    That's your problem right there. By including those two C files in the requires section of step3.y, their contents end up in both lex.yy.c and step3.tab.c, so everything is defined twice.

    Instead you should include the header files, not the C files, and then compile and link ast.c and symbtable.c by passing them to gcc:

    gcc step3.tab.c lex.yy.c ast.c symtable.c -o step3
    

    (You could also use a Makefile to compile each file separately and then link them together, so you only need to recompile the files that have changed, but that's an entirely different matter)

    Note that this isn't specific to flex or bison. You shouldn't ever #include C-files unless you know exactly what this implies and you have a very good reason.