0

I am trying to create .so dynamic library from *.o files and facing below issue.

LOG:

[nptemp-static]$ gcc -shared *.o -o libexample.so

/usr/bin/ld: bindings_hubbub_parser.o: relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object; recompile with -fPIC
bindings_hubbub_parser.o: could not read symbols: Bad value
collect2: ld returned 1 exit status

Any idea? Do I need to recompile my whole source code with the option specified?

Actually, I am not aware of the source code which I compiled because all the source code is open source which I downloaded and compiled by following instructions in README.

melpomene
  • 84,125
  • 8
  • 85
  • 148
  • 2
    `ld` gives you the exact solution in the error message: `recompile with -fPIC` – Stephen Newell Apr 30 '18 at 06:07
  • You mean recompiling the source code again? means while running make do I need to specify the option -fPIC? – Harish Tadikamalla Apr 30 '18 at 06:14
  • 1
    @jerry not while running make, your makefile should pass it to the compiler when creating the object files. You don't need it for a static library. In [my own build system](https://github.com/Zirias/zimk), I solve the problem by having different names for the object files, the ones intended for a shared library are suffixed `_s.o`. Of course, that's just one possible way to handle it. –  Apr 30 '18 at 07:39
  • Yeah, Felix... I did the changes in make file. I am able to get dynamic library now. thanks. – Harish Tadikamalla Apr 30 '18 at 08:48

1 Answers1

2

I am trying to create .so dynamic library from *.o files and facing below issue.

This is not that simple. In practice, you should compile specifically when making a shared library, at least on Linux.

(Perhaps you might need to edit your Makefile or configure somehow your build automation if it was not designed for building a shared library; if building some free software library, you might ask help from its authors or community)

Shared libraries want to have position independent code. So you need to compile their source code with the -fPIC flag passed to g++ or gcc (see this). You could also want to explicit the rpath.

Read Drepper's paper: How To Write Shared Libraries.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Thanks...I have added the -fPIC under CFALGS variable. Able to build .so library. Now I just want to know the version of that library.. How can we get he version of that library. – Harish Tadikamalla Apr 30 '18 at 08:47
  • That should go into your question (and probably another one), not here. And again, you need to give a lot more details (in particular, what exact library are you building). The notion of version is library specific. – Basile Starynkevitch Apr 30 '18 at 08:48