我试过写一个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
编译代码时,代码可以正常工作
谢谢!
答案 0 :(得分:13)
g++ main.o -o main main.o -lgsl -lgslcblas -lpthread
^^^^^^ ^^^^^^
您将main.o
链接两次,因此其中的所有内容都是重复的。
答案 1 :(得分:7)
之前的响应者向您显示了一个错误。当您更正该错误时,某些错误消息会消失。你说你有“同样的错误”。没有!仔细检查,你会少一些。
您的其他主要错误是,在编译-c
时您没有使用main.o
选项。实际上,main.o
是一个程序(如main
但有一个奇怪的名字)。该程序具有定义的所有系统功能,隐式链接系统库。然后,您尝试再次链接该程序,再次隐式链接系统库。当然,所有系统功能现在都是双重定义的。