2

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?

timoleonn
  • 303
  • 1
  • 5
  • 12

3 Answers3

4
#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.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
2

This might be late but i must have to keep this here. I had the same 'Multiple definition of a function issue' even though i used the correct include statements. I dont quite know why this happens but turns i was getting the multiple definition errors only for the global functions(that are not inside any class) in my header files. I had a bunch of utility methods and it didnt quite mean much to be a member function of a class.

So i was getting 'Multiple Definitions' error for these functions:

My Problem

So my solution was :

Solution

Basically wrapping them up with a class solved the issue and it works fine now.

because_im_batman
  • 975
  • 10
  • 26
  • 1
    Non-inline function definitions don't belong in header files. They should be *declared* in the header file and then defined in the corresponding .cpp file. Moving them into the class works because methods defined directly inside a class are automatically declared `inline`. You could have achieved the same effect by simply adding the `inline` keyword to all the global function definitions. But, unless you actually need those functions to be inline, the proper solution is still to move the definitions into the .cpp file. – sepp2k Jul 11 '19 at 12:58
0

I tried both of the solutions. They worked fine. But still I was having more 'Multiple Definition' errors in my other header files where i used class. Those files had Method Declarations inside the class but Method Definitions outside the class using Scope resolution(::) .

So I tried this

remove all the method declarations and write all the method definitions inside the class

And it worked!