链接gcc -o0优化级别的错误

时间:2015-04-27 01:31:27

标签: c gcc optimization linker

我有一个项目,我正在尝试进行一些库和系统调用基准测试。我想确保编译器不会优化我的调用。

这是我的makefile:

CC=gcc
CFLAGS= -I ./ 
LIBFLAGS= -lm
RM=/bin/rm -f

all:  osbench

osbench: osbench.c
    $(CC) $(CFLAGS) -o $@ osbench.c $(LIBFLAGS)

clean:
    $(RM) osbench *~

当我将标志设置为-o时,所有内容都编译并运行完美,但我不确定编译器是否省略了对math.h库的调用。 根据gcc手册(https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html)-o0表示不进行优化 - 这就是我想要的。当我将标志设置为-o0时,我会收到各种链接错误。

$(CC) $(CFLAGS) -o0 $@ osbench.c $(LIBFLAGS)

我还缺少另一面旗帜吗?

gcc -I ./  -o0 osbench osbench.c -lm
osbench: 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
osbench: In function `__data_start':
(.data+0x0): multiple definition of `__data_start'
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): reloca                                tion 0 has invalid symbol index 10
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): reloca                                tion 1 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): reloca                                tion 2 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): reloca                                tion 3 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): reloca                                tion 4 has invalid symbol index 10
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): reloca                                tion 5 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): reloca                                tion 6 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): reloca                                tion 7 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): reloca                                tion 8 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): reloca                                tion 9 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): reloca                                tion 10 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): reloca                                tion 11 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): reloca                                tion 12 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): reloca                                tion 13 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): reloca                                tion 14 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): reloca                                tion 15 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): reloca                                tion 16 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): reloca                                tion 17 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): reloca                                tion 18 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): reloca                                tion 19 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): reloca                                tion 20 has invalid symbol index 19
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o:(.data+0x0):                                 first defined here
osbench: 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
osbench:(.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
osbench: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_line): reloca                                tion 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
osbench: 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
/tmp/ccOMIYX2.o:(.rodata+0x0): multiple definition of `numOfCallsPerTrial'
osbench:(.rodata+0x8): first defined here
/tmp/ccOMIYX2.o:(.rodata+0x8): multiple definition of `numOfTrials'
osbench:(.rodata+0x10): first defined here
/tmp/ccOMIYX2.o: In function `runLibCallTrial':
osbench.c:(.text+0x0): multiple definition of `runLibCallTrial'
osbench:(.text+0xe4): first defined here
/tmp/ccOMIYX2.o: In function `calcLibCallTime':
osbench.c:(.text+0xab): multiple definition of `calcLibCallTime'
osbench:(.text+0x18f): first defined here
/tmp/ccOMIYX2.o: In function `main':
osbench.c:(.text+0x143): multiple definition of `main'
osbench:(.text+0x38c): first defined here
/usr/lib/gcc/x86_64-linux-gnu/4.6/crtend.o:(.dtors+0x0): multiple definition of                                 `__DTOR_END__'
osbench:(.dtors+0x8): first defined here
/usr/bin/ld: error in osbench(.eh_frame); no .eh_frame_hdr table will be created                                .
collect2: ld returned 1 exit status

我可以在必要时发布我的代码,但是基准代码部分用于作业,所以我不确定我是否被允许。它编译得很好,所以我不认为它是.c中的问题 谢谢。

1 个答案:

答案 0 :(得分:4)

-O不是-o。也就是说,大写不是小写。无论如何,你可以完全放弃它。默认值为-O0

如果不清楚,-o有不同的含义。它确定编译输出名称。所以你应该在命令行中保留它。添加-O以更改优化级别(或将其保留为默认值)。事实上,您的原始版本已经禁用了优化。