0
votes

I have a project where I am trying to do some library and system call benchmarking. I want to make sure that the compiler does not optimize away my calls.

Here is my 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 *~

When I set the flag to -o everything compiles and runs perfectly but I am not sure if the compiler has omitted my calls to the math.h library. According to the gcc manual (https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html) -o0 means that no optimization takes place - which is what I want. When I set the flag to -o0 I get all sorts of linking errors.

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

Is there a another flag I am missing?

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

I can post my code if necessary, however the benchmarking code part is for a homework assignment so I'm not sure I am allowed to. It compiles fine otherwise so I don't think it's a problem in the .c Thanks.

1

1 Answers

4
votes

-O not -o. That is, upper case not lower case. Anyway, you can just leave it out altogether. Default is -O0.

In case it's not clear, -o has a different meaning. It determines the compilation output name. So you should keep that in your command line. Add -O to change the optimisation level (or leave it out for default). So in fact, your original build already has optimisations disabled.