0

I wrote a simple shared lib which contains the usage of stdout in stdio.h.

#include <stdio.h>

...
  fflush(stdout);
...

There was no compilation issue before I added the fflush(stdout) with the command below

$gcc -shared -o a.so a.c 

But after adding the fflush(stdout), compiler complains:

/usr/bin/ld: /tmp/ccK4npwc.o: relocation R_X86_64_PC32 against symbol `stdout@@GLIBC_2.2.5' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: bad value
collect2: error: ld returned 1 exit status

Can someone explain the rationale behind this with as much detail as possible? why does it have to be -fPIC here?

EDIT: Some comments suggest I read on PIC, but that is missing my question. My question is why do I need PIC for this program. In my program, I also uses puts which is from libc. I can compile it fine without -fPIC. But why is -fPIC required with the stdout variable?

drdot
  • 3,215
  • 9
  • 46
  • 81
  • Does this answer your question? [GCC -fPIC option](https://stackoverflow.com/questions/5311515/gcc-fpic-option) – mgc Jan 17 '21 at 20:26
  • also: https://stackoverflow.com/questions/966960/ – M.M Jan 17 '21 at 20:27
  • thanks for the comments. But why does this share library usage has to be PIC? I can compile a program with puts API without PIC. So these links did not answer my Q. – drdot Jan 17 '21 at 23:31

0 Answers0