Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in makefile: multiple definition of _start

Tags:

c++

makefile

I have tried to write a makefile:

    CC = g++
LD = ld
CFLAGS = -Wall -std=c++0x -O3
LDFLAGS = -lgsl -lgslcblas -lpthread

SOURCES = main.cpp
OBJECTS = $(SOURCES:.cpp=.o)
EXECUTABLES = main

all: $(SOURCES) $(EXECUTABLES)
$(EXECUTABLES): $(OBJECTS)
    $(CC) $(OBJECTS) -o $@ $^ $(LDFLAGS) 

.cpp.o: 
    $(CC) $(CFLAGS) $< -o $@


.PHONY: clean
clean: 
    rm -f *~ *.o main

But when I am executing this code, I get the following errors:

g++ -Wall -std=c++0x -O3 main.cpp -o main.o
main.cpp: In function ‘int main()’:
main.cpp:63:12: warning: unused variable ‘order’ [-Wunused-variable]
main.cpp:64:12: warning: variable ‘x’ set but not used [-Wunused-but-set-variable]
g++ main.o -o main main.o -lgsl -lgslcblas -lpthread 
main.o: In function `_start':
(.text+0x108): multiple definition of `_start'
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 10
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 10
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 20 has invalid symbol index 19
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_line): relocation 0 has invalid symbol index 2
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o:/build/buildd/eglibc-2.15/csu/../sysdeps/x86_64/elf/start.S:109: first defined here
main.o: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crti.o:(.fini+0x0): first defined here
main.o:(.rodata+0x0): multiple definition of `_IO_stdin_used'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o:(.rodata.cst4+0x0): first defined here
main.o: In function `__data_start':
(.data+0x0): multiple definition of `__data_start'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o:(.data+0x0): first defined here
main.o: In function `__data_start':
(.data+0x8): multiple definition of `__dso_handle'
/usr/lib/gcc/x86_64-linux-gnu/4.6/crtbegin.o:(.data+0x0): first defined here
main.o: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crti.o:(.init+0x0): first defined here
main.o: In function `__libc_csu_fini':
(.text+0x7f0): multiple definition of `__libc_csu_fini'
main.o:(.text+0x7f0): first defined here
main.o: In function `_start':
(.text+0x108): multiple definition of `_start'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o:/build/buildd/eglibc-2.15/csu/../sysdeps/x86_64/elf/start.S:109: first defined here
main.o: In function `Lagrange_coefficient(int, double, int, int)':
(.text+0x1f0): multiple definition of `Lagrange_coefficient(int, double, int, int)'
main.o:(.text+0x1f0): first defined here
main.o: In function `__data_start':
(.data+0x80): multiple definition of `datax'
main.o:(.data+0x80): first defined here
main.o: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crti.o:(.fini+0x0): first defined here
main.o:(.rodata+0x0): multiple definition of `_IO_stdin_used'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o:(.rodata.cst4+0x0): first defined here
main.o: In function `__data_start':
(.data+0x0): multiple definition of `__data_start'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o:(.data+0x0): first defined here
main.o: In function `Lagrange(double, double)':
(.text+0x3b0): multiple definition of `Lagrange(double, double)'
main.o:(.text+0x3b0): first defined here
main.o:(.bss+0x0): multiple definition of `std::cout@@GLIBCXX_3.4'
main.o:(.bss+0x0): first defined here
main.o: In function `__data_start':
(.data+0x8): multiple definition of `__dso_handle'
/usr/lib/gcc/x86_64-linux-gnu/4.6/crtbegin.o:(.data+0x0): first defined here
main.o:(.dtors+0x8): multiple definition of `__DTOR_END__'
main.o:(.dtors+0x8): first defined here
main.o: In function `__libc_csu_init':
(.text+0x760): multiple definition of `__libc_csu_init'
main.o:(.text+0x760): first defined here
main.o: In function `__data_start':
(.data+0x20): multiple definition of `datay'
main.o:(.data+0x20): first defined here
main.o: In function `main':
(.text+0x0): multiple definition of `main'
main.o:(.text+0x0): first defined here
main.o: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crti.o:(.init+0x0): first defined here
/usr/lib/gcc/x86_64-linux-gnu/4.6/crtend.o:(.dtors+0x0): multiple definition of `__DTOR_END__'
main.o:(.dtors+0x8): first defined here
/usr/bin/ld: error in main.o(.eh_frame); no .eh_frame_hdr table will be created.
/usr/bin/ld: error in main.o(.eh_frame); no .eh_frame_hdr table will be created.
collect2: ld returned 1 exit status
make: *** [main] Error 1

So, what am I doing wrong, and how can I fix it? If requested I can add my code, too. (But the code works when I am compiling it with g++ -Wall -O3 -std=c++0x $1 -lgsl -lgslcblas.
Thank you!

like image 325
arc_lupus Avatar asked Nov 27 '13 08:11

arc_lupus


2 Answers

  g++ main.o -o main main.o -lgsl -lgslcblas -lpthread 
      ^^^^^^         ^^^^^^

You link main.o twice, so everything in it is duplicated.

like image 95
David Schwartz Avatar answered Oct 22 '22 04:10

David Schwartz


The previous responder showed you one error that you are making. When you correct that error, some of the error messages do go away. You are saying you have "the same errors". No! Check carefully, you will have fewer.

Your other and main error, is that you are not using the -c option when compiling main.o. In effect, main.o is a program (like main but with a strange name). That program has all the system functions defined, from implicitly linking system libraries. Then you are trying to link that program again, again implicitly linking system libraries. Naturally, all the system functions are now double-defined.

like image 25
Mark Galeck Avatar answered Oct 22 '22 05:10

Mark Galeck