Assume the following simple C
code:
file1.c
#include <stdio.h>
char* gets(char* i){
return i;
}
which is redefining the libC native function gets
.
This compiles fine with gcc file1.c
.
My question is howcome the linker isn't complaining about duplicate symbols
as this function is also defined in libC itself?
Because you can override functions from standard libraries in C, check this
add option -whole-archive in the link phase, as below:
gcc -c -ofile1.o file1.c
gcc -ofile1 -Wl,--whole-archive -lc file1.o -Wl,--no-whole-archive
result:
file1.o: In function `gets':
file1.c:(.text+0x0): multiple definition of `gets'
file1.o: In function `main':
file1.c:(.text+0x18): undefined reference to `gets@@GLIBC_2.2.5'
collect2: error: ld returned 1 exit status
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With