Alexey Pavlov <alexpux(a)gmail.com> writes:
> 2013/11/29 Niels Möller <nisse(a)lysator.liu.se>:
>> What failed? Can I repost your message to the list?
>
> I do it already. I receive subscribing message now.
Ok. I'm now cc:ing the list. Context: Link error when building a dynamic
libnettle.dll and libhogweed.dll, and statically linking to libgmp.a.
>>> i686-w64-mingw32-gcc -march=i686 -mtune=generic -O2 -pipe
>>> -I/mingw32/include -ggdb3 -Wno-pointer-sign -Wall -W
>>> -Wmissing-prototypes -Wmissing-declarations -Wstrict-prototypes
>>> -Wpointer-arith -Wbad-function-cast -Wnested-externs -L.. -pipe
>>> -L/mingw32/lib ecc-mod-test.o testutils.o ../nettle-internal.o
>>> -lhogweed -lnettle -lgmp -o ecc-mod-test.exe
>>>
>>> C:/msys64/mingw32/lib/libgmp.a(sub_n.o):(.text+0x0): multiple
>>> definition of `__gmpn_sub_n'
>>>
>>> ../libhogweed.dll.a(d000131.o):(.text+0x0): first defined here
>>>
>>> collect2.exe: error: ld returned 1 exit status
>>
>> So you're building shared libraries for nettle, but you have a static
>> gmp library? The way I understand the error, the used gmp functions gets
>> copied into libhogweed.dll. Not sure if that is the best behavior.
>>
> Yes I have static GMP because we build toolchains with static
> prerequisites and don't want to has dynamic dependencies from them
> (gmp, mpc, mpfr, clog, isl). I prefer to use static versions of them.
>> But I'm not sure I understand the link error. There's some function
>> depending on __gmpn_sub_n ("mpn_sub_n" to user code), but if that
>> function is included in libhogweed.dll, why isn't that function just
>> skipped when the linker processes libgmp.a?
>
> A program in C can have only one definition (declaration which assigns
> a value) for each object (storage space for a variable).
But when doing linking with static libraries, multiple definitions are
generally ok. The linker copies only the object files which are needed,
and when it needs a symbol, it includes only the first object file in
any of the libraries, which define the symbol.
One may still get clashes if each object file includes multiple symbols,
since in the set of object files selected by the linker in the end,
multiple definitions of the same symbol are not allowed.
> As I understand, somewhere header file contains definitions for
> several functions __gmpn_sub_n which when included in several
> different translation units results in the linker throwing this error.
I don't think so. My understanding is that
1. The symbol __gmp_sub_n is defined in some object file included in
libgmp.a, and nowhere in the nettle source or object code (and
mini-gmp.c doesn't count, it's not involved in the link, and it uses
different linker symbol names than the real gmp).
2. This object file gets copied from libgmp.a into libhogweed.dll by the
linker. Not sure if this is right; -lgmp was added to the libhogweed
link command primarily to get dependencies between *shared* libraries
right.
3. You then link the executable ecc-mod-test.exe with -lhogweed and
-lgmp. The linked sees two definitions of the symbol __gmpn_sub_n,
one in libgmp.a, and one in libhogweed.dll, and it considers this an
error.
What I don't understand, is why, in (3) above, the linker doesn't just
ignore the object in libgmp.a which defines the symbol.
Regards,
/Niels
--
Niels Möller. PGP-encrypted email is preferred. Keyid C0B98E26.
Internet email is subject to wholesale government surveillance.