gpt4 book ai didi

c++ - 生成文件 : multiple definition of _start 中的错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:04:14 26 4
gpt4 key购买 nike

我试着写了一个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

但是当我执行这段代码时,出现以下错误:

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

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

最佳答案

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

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

关于c++ - 生成文件 : multiple definition of _start 中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20237490/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com