Troubles linking with ld

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Troubles linking with ld

securehell
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello group...

I am dealing with creating a complicated shared object library and
then linking against it. This is on a 64-bit linux platform with
Fedora Core 3.

I compile several .c files with the following compile flags:

gcc -m64 -c -Wall -fPIC -g -o file1.o file1.c
gcc -m64 -c -Wall -fPIC -g -o file2.o file2.c
gcc -m64 -c -Wall -fPIC -g -o file3.o file3.c
gcc -m64 -c -Wall -fPIC -g -o file4.o file4.c

I then create archives:

ar rs libfiles12.a file1.o file2.o
ar rs libfiles34.a file3.o file4.o

I then compile file5.c then link to create a shared lib:

gcc -m64 -c -Wall -fPIC -O3 -o file5.o file5.c

ld -shared --whole-archive -o libnew.so file5.o libfiles12.a
libfiles34.a

Now if I write file main.cpp and want to link against libnew.so, I
have problems:

gcc -m64 -c -Wall -fPIC -g -o main.o main.cpp
ld -lc -o main main.o libnew.so
/usr/bin/ld: warning: cannot find entry symbol _start; defaulting
to 00000000004010a0
main.o(.gnu.linkonce.d.DW.ref.__gxx_personality_v0+0x0): undefined
reference to `__gxx_personality_v0'
libnew.so: undefined reference to `fstat'

Can anyone give me any pointers on this?

Thanks,

SH
-----BEGIN PGP SIGNATURE-----
Note: This signature can be verified at https://www.hushtools.com/verify
Version: Hush 2.4

wkYEARECAAYFAkPpHQYACgkQRBFe1uc9INpZygCfWsZqmDhaXaNt081x7POFfZVMMoAA
oL6NpZnEVY24du24sgy29R4qLu5o
=0MY4
-----END PGP SIGNATURE-----


Reply | Threaded
Open this post in threaded view
|

Re: Troubles linking with ld

Daniel Jacobowitz-2
On Tue, Feb 07, 2006 at 05:19:51PM -0500, [hidden email] wrote:
> ld -shared --whole-archive -o libnew.so file5.o libfiles12.a
> libfiles34.a

Don't.  Use gcc -shared instead; it knows how to invoke ld correctly
to link shared libraries.

> gcc -m64 -c -Wall -fPIC -g -o main.o main.cpp
> ld -lc -o main main.o libnew.so
> /usr/bin/ld: warning: cannot find entry symbol _start; defaulting
> to 00000000004010a0
> main.o(.gnu.linkonce.d.DW.ref.__gxx_personality_v0+0x0): undefined
> reference to `__gxx_personality_v0'
> libnew.so: undefined reference to `fstat'

Especially true for executables.  And in this case, you need to link
with g++, not gcc.

--
Daniel Jacobowitz
CodeSourcery
Reply | Threaded
Open this post in threaded view
|

Re: Troubles linking with ld

securehell
In reply to this post by securehell
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

OK, I'll resist the temptation to ask numerous questions about why
I can't use ld here and just go with your suggestion...

Now when I try to use gcc to compile and link my shared library, I
get the following:


gcc -m64 -c -Wall -fPIC -g -o file5.o file5.c

gcc -shared -Wl,--whole-archive -fPIC -o libnew.so file5.o
libfiles12.a libfiles34.a

/usr/lib/gcc/x86_64-redhat-
linux/3.4.4/libgcc.a(_muldi3.oS)(.text+0x0): In function `__multi3':
: multiple definition of `__multi3'
/usr/lib/gcc/x86_64-redhat-
linux/3.4.4/libgcc.a(_muldi3.oS)(.text+0x0): first defined here

::

/usr/lib/gcc/x86_64-redhat-
linux/3.4.4/libgcc.a(__gcc_bcmp.oS)(.text+0x0): In function
`__gcc_bcmp':
: multiple definition of `__gcc_bcmp'
/usr/lib/gcc/x86_64-redhat-
linux/3.4.4/libgcc.a(__gcc_bcmp.oS)(.text+0x0): first defined here

/usr/lib64/libc_nonshared.a(elf-init.oS)(.text+0xd): In function
`__libc_csu_init':
: undefined reference to `__init_array_start'
/usr/bin/ld: /usr/lib64/libc_nonshared.a(elf-init.oS): relocation
R_X86_64_PC32 against `__init_array_start' can not be used when
making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Bad value
collect2: ld returned 1 exit status
make[1]: *** [libnew.so] Error 1

Sorry, this is kind of messy but I did chop it down for you.

Am I missing gcc flags?



On Tue, 07 Feb 2006 17:25:04 -0500 Daniel Jacobowitz
<[hidden email]> wrote:

>On Tue, Feb 07, 2006 at 05:19:51PM -0500, [hidden email]
>wrote:
>> ld -shared --whole-archive -o libnew.so file5.o libfiles12.a
>> libfiles34.a
>
>Don't.  Use gcc -shared instead; it knows how to invoke ld
>correctly
>to link shared libraries.
>
>> gcc -m64 -c -Wall -fPIC -g -o main.o main.cpp
>> ld -lc -o main main.o libnew.so
>> /usr/bin/ld: warning: cannot find entry symbol _start;
>defaulting
>> to 00000000004010a0
>> main.o(.gnu.linkonce.d.DW.ref.__gxx_personality_v0+0x0):
>undefined
>> reference to `__gxx_personality_v0'
>> libnew.so: undefined reference to `fstat'
>
>Especially true for executables.  And in this case, you need to
>link
>with g++, not gcc.
>
>--
>Daniel Jacobowitz
>CodeSourcery
-----BEGIN PGP SIGNATURE-----
Note: This signature can be verified at https://www.hushtools.com/verify
Version: Hush 2.4

wkYEARECAAYFAkPpJqAACgkQRBFe1uc9INqp/gCgmTP/p0V9W61xROUqjWXrmbiPgKIA
nAjFzKC4jo5OvgLwx/8WcaggEbze
=rpnQ
-----END PGP SIGNATURE-----


Reply | Threaded
Open this post in threaded view
|

Re: Troubles linking with ld

Daniel Jacobowitz-2
On Tue, Feb 07, 2006 at 06:00:49PM -0500, [hidden email] wrote:
> OK, I'll resist the temptation to ask numerous questions about why
> I can't use ld here and just go with your suggestion...

Run gcc -v, see what it's doing.  If you don't understand everything
it does, I recommend letting it do it :-)

> gcc -shared -Wl,--whole-archive -fPIC -o libnew.so file5.o
> libfiles12.a libfiles34.a
>
> /usr/lib/gcc/x86_64-redhat-
> linux/3.4.4/libgcc.a(_muldi3.oS)(.text+0x0): In function `__multi3':
> : multiple definition of `__multi3'
> /usr/lib/gcc/x86_64-redhat-
> linux/3.4.4/libgcc.a(_muldi3.oS)(.text+0x0): first defined here

Use -Wl,--no-whole-archive after your own libraries.


--
Daniel Jacobowitz
CodeSourcery
Reply | Threaded
Open this post in threaded view
|

Re: Troubles linking with ld

securehell
In reply to this post by securehell
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Thanks. That worked. I will have to edit our entire make system due
to global variables but that's ok.

I'm sure to have more problems later but that's enough for today.

Cheers!

On Tue, 07 Feb 2006 18:02:33 -0500 Daniel Jacobowitz
<[hidden email]> wrote:

>On Tue, Feb 07, 2006 at 06:00:49PM -0500, [hidden email]
>wrote:
>> OK, I'll resist the temptation to ask numerous questions about
>why
>> I can't use ld here and just go with your suggestion...
>
>Run gcc -v, see what it's doing.  If you don't understand
>everything
>it does, I recommend letting it do it :-)
>
>> gcc -shared -Wl,--whole-archive -fPIC -o libnew.so file5.o
>> libfiles12.a libfiles34.a
>>
>> /usr/lib/gcc/x86_64-redhat-
>> linux/3.4.4/libgcc.a(_muldi3.oS)(.text+0x0): In function
>`__multi3':
>> : multiple definition of `__multi3'
>> /usr/lib/gcc/x86_64-redhat-
>> linux/3.4.4/libgcc.a(_muldi3.oS)(.text+0x0): first defined here
>
>Use -Wl,--no-whole-archive after your own libraries.
>
>
>--
>Daniel Jacobowitz
>CodeSourcery
-----BEGIN PGP SIGNATURE-----
Note: This signature can be verified at https://www.hushtools.com/verify
Version: Hush 2.4

wkYEARECAAYFAkPpKOoACgkQRBFe1uc9INpsDACgvE3Q/RdNyrYXQILN/NYsoyze5zwA
nRCg47WGdDQISakFQfy5fQMtYD63
=eypG
-----END PGP SIGNATURE-----


Reply | Threaded
Open this post in threaded view
|

Re: Troubles linking with ld

securehell
In reply to this post by securehell
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

OK so my first shared object library is linking and I can compile
an executable against it. I'm happier.

My next challenge: I actually build 4 different .so libraries. The
first so far has compiled/linked.

The next library is built the same as my previous library with one
exception: When I link the objects I am getting the following error:

gcc -shared -Wl,--whole-archive -fPIC -o libnew2.so file6.o
libfiles12.a libfiles56.a libnew.so -Wl,--no-whole-archive

libfiles12.a: member libfiles12.a(libfiles78.a) in archive is not
an object

So one of the archives is created which includes another archive(s)
but the linking step doesn't like it. By the way, I have verified
that the libfiles78.a is correct with "ar x libfiles12.a
libfiles78.a" and everything looked fine.

Any suggestions?

SH

On Tue, 07 Feb 2006 18:02:33 -0500 Daniel Jacobowitz
<[hidden email]> wrote:

>On Tue, Feb 07, 2006 at 06:00:49PM -0500, [hidden email]
>wrote:
>> OK, I'll resist the temptation to ask numerous questions about
>why
>> I can't use ld here and just go with your suggestion...
>
>Run gcc -v, see what it's doing.  If you don't understand
>everything
>it does, I recommend letting it do it :-)
>
>> gcc -shared -Wl,--whole-archive -fPIC -o libnew.so file5.o
>> libfiles12.a libfiles34.a
>>
>> /usr/lib/gcc/x86_64-redhat-
>> linux/3.4.4/libgcc.a(_muldi3.oS)(.text+0x0): In function
>`__multi3':
>> : multiple definition of `__multi3'
>> /usr/lib/gcc/x86_64-redhat-
>> linux/3.4.4/libgcc.a(_muldi3.oS)(.text+0x0): first defined here
>
>Use -Wl,--no-whole-archive after your own libraries.
>
>
>--
>Daniel Jacobowitz
>CodeSourcery
-----BEGIN PGP SIGNATURE-----
Note: This signature can be verified at https://www.hushtools.com/verify
Version: Hush 2.4

wkYEARECAAYFAkPpTgsACgkQRBFe1uc9INrLSgCgqoPHghECFFLncjlMc8qjBDAR9V8A
oIHV2aMCzKpWpx9BRRMUHgV+6PaV
=IOd3
-----END PGP SIGNATURE-----


Reply | Threaded
Open this post in threaded view
|

Re: Troubles linking with ld

Ian Lance Taylor
<[hidden email]> writes:

> My next challenge: I actually build 4 different .so libraries. The
> first so far has compiled/linked.
>
> The next library is built the same as my previous library with one
> exception: When I link the objects I am getting the following error:
>
> gcc -shared -Wl,--whole-archive -fPIC -o libnew2.so file6.o
> libfiles12.a libfiles56.a libnew.so -Wl,--no-whole-archive
>
> libfiles12.a: member libfiles12.a(libfiles78.a) in archive is not
> an object
>
> So one of the archives is created which includes another archive(s)
> but the linking step doesn't like it. By the way, I have verified
> that the libfiles78.a is correct with "ar x libfiles12.a
> libfiles78.a" and everything looked fine.

Yes, it looks like you put an archive file inside another archive
file.  The linker doesn't understand that.  Archive files that you
pass to the linker should only contain object files.

You can put the entire contents of one archive into another by doing
something like:

    mkdir tmp
    cd tmp
    ar x ../INNERARCHIVE
    ar rcv ../OUTERARCHIVE *.o

Ian
Reply | Threaded
Open this post in threaded view
|

Re: Troubles linking with ld

securehell
In reply to this post by securehell
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Thanks. I have rewritten the makefile to include the separate .a
files which worked. I am curious, though, if including a .a within
another .a is possible, what purpose does it serve if the linker
cannot handle it when creating a .so?

Thanks,

SH

On Wed, 08 Feb 2006 02:27:44 -0500 Ian Lance Taylor <[hidden email]>
wrote:

><[hidden email]> writes:
>
>> My next challenge: I actually build 4 different .so libraries.
>The
>> first so far has compiled/linked.
>>
>> The next library is built the same as my previous library with
>one
>> exception: When I link the objects I am getting the following
>error:
>>
>> gcc -shared -Wl,--whole-archive -fPIC -o libnew2.so file6.o
>> libfiles12.a libfiles56.a libnew.so -Wl,--no-whole-archive
>>
>> libfiles12.a: member libfiles12.a(libfiles78.a) in archive is
>not
>> an object
>>
>> So one of the archives is created which includes another
>archive(s)
>> but the linking step doesn't like it. By the way, I have
>verified
>> that the libfiles78.a is correct with "ar x libfiles12.a
>> libfiles78.a" and everything looked fine.
>
>Yes, it looks like you put an archive file inside another archive
>file.  The linker doesn't understand that.  Archive files that you
>pass to the linker should only contain object files.
>
>You can put the entire contents of one archive into another by
>doing
>something like:
>
>    mkdir tmp
>    cd tmp
>    ar x ../INNERARCHIVE
>    ar rcv ../OUTERARCHIVE *.o
>
>Ian
-----BEGIN PGP SIGNATURE-----
Note: This signature can be verified at https://www.hushtools.com/verify
Version: Hush 2.4

wkYEARECAAYFAkPqMRwACgkQRBFe1uc9INofrwCcDD10RelEmhnkTJ8ptFrWyjDJ/pQA
oIU/NLm6rY6mQZlBBp0v3X9mfi6t
=8HEq
-----END PGP SIGNATURE-----


Reply | Threaded
Open this post in threaded view
|

Re: Troubles linking with ld

Andreas Schwab
<[hidden email]> writes:

> I am curious, though, if including a .a within another .a is possible,
> what purpose does it serve if the linker cannot handle it when creating
> a .so?

You can put any file whatsoever in an archive.  Traditionally the use of
archives was not restricted to linker archives containing object files.

Andreas.

--
Andreas Schwab, SuSE Labs, [hidden email]
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
PGP key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."