简体   繁体   中英

g++ linking a static library into a dynamic library (without -fPIC)

I am trying to link with g++ a static library (staticLib.a) into a dynamic library (dynamicLib.so) using:

g++  *.o -Wl,--whole-archive staticLib.a -Wl,--no-whole-archive -shared -o dynamicLib.so

And I got the same error as here :

/usr/bin/ld: staticLib.a(object.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC staticLib.a(object.o): error adding symbols: Bad value collect2: error: ld returned 1 exit status

I read several topics, but I could not find the answer I am looking for. staticLib.a was not compiled as position-independent code (PIC). According to the link above , it seems to be mandatory. However, staticLib.a is a library from another project over which I do not have control.

My first thought was to extract the objects *.o using ar -x (as explained in this second link ). But the problem remains the same as the object was not compiled with -fPIC .

My second thought was to create my own Makefile to recompile staticLib.a with -fPIC in my project (I do not want to mess up the existing project). But I am not sure it is a good way to do...

So my question is the following: Is there any possible way to link a static library (compiled without -fPIC ) into a dynamic one ?

Related topics:

So my question is the following: Is there any possible way to link a static library (compiled without -fPIC) into a dynamic one ?

As providing position independent code requires compilation no practically it is not possible to change already compiled code. Theoretically you may reverse engineer source code from binary and recompile, but that would be completely ineffective solution. So you have to (re)compile orginal project with -fPIC passed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM