Compiling a static version of GMP for `-Wl,--whole-archive`

Marc Glisse marc.glisse at inria.fr
Thu Jul 2 09:41:07 UTC 2020


On Wed, 1 Jul 2020, Cade Brown wrote:

> Hello all,
>
> I am trying to compile a static version of GMP (i.e. libgmp.a) on Linux
> (Ubuntu) which I can embed fully in a .so I am compiling. I should be able
> to do this as long as GMP contains PIC.
>
> If I configure with `$ ./configure --enable-static --disable-shared
> --with-pic --prefix=deps/prefix`
>
> (and then make && make install)
>
> I later compile my library with:
>
> ```
> $ gcc -shared -O3 -std=c99 $(MY_OBJ) \
>    -Wl,--whole-archive deps/prefix/lib/libgmp.a -Wl,--no-whole-archive \
>    -o libmylibrary.so
> ```
>
> Obviously, some steps are simplified for brevity, but I get the error:
>
> ```
> usr/bin/ld: deps/prefix/lib/libgmp.a(dive_1.o): relocation R_X86_64_PC32
> against symbol `__gmp_binvert_limb_table' can not be used when making a
> shared object; recompile with -fPIC
> /usr/bin/ld: final link failed: bad value
> ```
>
> Any help, or experiences others have had embedding GMP into a shared
> library?

What you described seems to work for me, using the current sources from 
master. Is m4 correctly called with -DPIC on dive_1.asm in your build?

-- 
Marc Glisse


More information about the gmp-discuss mailing list