Christian Olson Christian Olson - 2 months ago 7
C++ Question

multiple definition of '_start'

I've been getting errors on my makefile code and I haven't a clue how to fix it, I searched the site for an answer and ended up making an account to ask for help
Here's the error code

date.o: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o:(.text+0x0): first defined here
date.o: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crti.o (.fini+0x0): first defined here
date.o:(.rodata+0x0): multiple definition of `_IO_stdin_used'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o (.rodata.cst4+0x0): first defined here
date.o: In function `data_start':
(.data+0x0): multiple definition of `__data_start'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o:(.data+0x0): first defined here
date.o: In function `data_start':
(.data+0x8): multiple definition of `__dso_handle'
/usr/lib/gcc/x86_64-linux-gnu/5/crtbegin.o:(.data+0x0): first defined here
date.o: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crti.o (.init+0x0): first defined here
/usr/lib/gcc/x86_64-linux-gnu/5/crtend.o:(.tm_clone_table+0x0): multiple definition of `__TMC_END__'
date.o:(.data+0x10): first defined here
/usr/bin/ld: error in date.o(.eh_frame); no .eh_frame_hdr table will be created.
collect2: error: ld returned 1 exit status
Makefile:3: recipe for target 'testdate' failed
make: *** [testdate] Error 1


and my makefile

testdate: date.o testdate.o
g++ -Wall -o testdate.o date.o

date.o: date.h date.cpp
g++ -Wall -c date.cpp
testdate.o: date.h testdate.cpp
g++ -Wall -c testdate.cpp

Answer

The rule

testdate: date.o testdate.o
        g++ -Wall -o testdate.o date.o

should be

testdate: date.o testdate.o
        g++ -Wall -o testdate testdate.o date.o
#                    ^^^^^^^^

or to avoid repeating yourself:

testdate: date.o testdate.o
        g++ -Wall $^ -o $@

(this should produce g++ -Wall date.o testdate.o -o testdate)

Indeed, you might want to consider:

testdate: date.o testdate.o
        g++ -Wall $^ -o $@
date.o: date.cpp date.h
    g++ -Wall -c $< -o $@
testdate.o: testdate.cpp date.h
    g++ -Wall -c $< -o $@

$^ is all of the dependencies, $< is just the first and $@ is the current target.

For more on Makefile rules: https://www.chemie.fu-berlin.de/chemnet/use/info/make/make_4.html

Comments