C++ makefile 中的错误:_start 的多个定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20237490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:59:17  来源:igfitidea点击:

Error in makefile: multiple definition of _start

c++makefile

提问by arc_lupus

I have tried to write a makefile:

我试图写一个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!

那么,我做错了什么,我该如何解决?如果需要,我也可以添加我的代码。(但代码在我用 编译时有效g++ -Wall -O3 -std=c++0x $1 -lgsl -lgslcblas
谢谢!

回答by David Schwartz

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

You link main.otwice, so everything in it is duplicated.

您链接了main.o两次,因此其中的所有内容都是重复的。

回答by Mark Galeck

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 -coption when compiling main.o. In effect, main.ois a program (like mainbut 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.

您的另一个主要错误是您-c在编译main.o. 实际上,main.o是一个程序(就像main一个奇怪的名字)。该程序定义了所有系统函数,从隐式链接系统库。然后您尝试再次链接该程序,再次隐式链接系统库。自然地,所有的系统函数现在都是双重定义的。