public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* is there any way to change the order of name resolution in linking (aside from putting the libraries in the right order?)
@ 2012-09-27  5:09 Dan Hitt
  2012-09-27  5:24 ` Ian Lance Taylor
  0 siblings, 1 reply; 12+ messages in thread
From: Dan Hitt @ 2012-09-27  5:09 UTC (permalink / raw)
  To: gcc-help

I am building some code and i need a certain library to be linked in
last, so it can resolve symbols left unresolved earlier.

Normally the way to do this would be to just list that library last
on the command line.

However, i'm using a build system which constructs all the
commands for me, and although it makes a provision for
adding extra arguments to the linker, they end up going first.

So although i can add arguments to the link, i cannot order
them the way i want, and i get unresolved symbols as a consequence.

So i'm wondering if there's any argument i can slip in that will change
the order that gcc runs through the libraries in.

(The gcc info suggest not, but perhaps there's an undocumented
option for doing this?)

Thanks in advance for any clues on this.

dan

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: is there any way to change the order of name resolution in linking (aside from putting the libraries in the right order?)
  2012-09-27  5:09 is there any way to change the order of name resolution in linking (aside from putting the libraries in the right order?) Dan Hitt
@ 2012-09-27  5:24 ` Ian Lance Taylor
  2012-09-27 14:05   ` Feuerbacher, Alan
  0 siblings, 1 reply; 12+ messages in thread
From: Ian Lance Taylor @ 2012-09-27  5:24 UTC (permalink / raw)
  To: Dan Hitt; +Cc: gcc-help

On Wed, Sep 26, 2012 at 10:09 PM, Dan Hitt <dan.hitt@gmail.com> wrote:
> I am building some code and i need a certain library to be linked in
> last, so it can resolve symbols left unresolved earlier.
>
> Normally the way to do this would be to just list that library last
> on the command line.
>
> However, i'm using a build system which constructs all the
> commands for me, and although it makes a provision for
> adding extra arguments to the linker, they end up going first.
>
> So although i can add arguments to the link, i cannot order
> them the way i want, and i get unresolved symbols as a consequence.
>
> So i'm wondering if there's any argument i can slip in that will change
> the order that gcc runs through the libraries in.

I'm not aware of any such option.

One thing that may work, if you are using the GNU linker or something
reasonably compatible, is
    -Wl,--whole-archive,-lMYLIB,-Wl,--no-whole-archive

That will link in the entire contents of the library, so all the
symbols defined in the library will be available.

Ian

^ permalink raw reply	[flat|nested] 12+ messages in thread

* RE: is there any way to change the order of name resolution in linking (aside from putting the libraries in the right order?)
  2012-09-27  5:24 ` Ian Lance Taylor
@ 2012-09-27 14:05   ` Feuerbacher, Alan
  2012-09-27 16:06     ` Jonathan Wakely
  2012-09-27 17:56     ` Ian Lance Taylor
  0 siblings, 2 replies; 12+ messages in thread
From: Feuerbacher, Alan @ 2012-09-27 14:05 UTC (permalink / raw)
  To: Ian Lance Taylor, Dan Hitt; +Cc: gcc-help

Ian Lance Taylor wrote:


> One thing that may work, if you are using the GNU linker or something
> reasonably compatible, is
>     -Wl,--whole-archive,-lMYLIB,-Wl,--no-whole-archive
> 
> That will link in the entire contents of the library, so all the
> symbols defined in the library will be available.

I'm trying something like that for a related purpose, but for it to work you have to replace the "," after "MYLIB" with a space. Otherwise gcc complains about an invalid option "-Wl".

Alan

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: is there any way to change the order of name resolution in linking (aside from putting the libraries in the right order?)
  2012-09-27 14:05   ` Feuerbacher, Alan
@ 2012-09-27 16:06     ` Jonathan Wakely
  2012-09-27 17:56     ` Ian Lance Taylor
  1 sibling, 0 replies; 12+ messages in thread
From: Jonathan Wakely @ 2012-09-27 16:06 UTC (permalink / raw)
  To: Feuerbacher, Alan; +Cc: Ian Lance Taylor, Dan Hitt, gcc-help

On 27 September 2012 15:05, Feuerbacher, Alan wrote:
> Ian Lance Taylor wrote:
>
>
>> One thing that may work, if you are using the GNU linker or something
>> reasonably compatible, is
>>     -Wl,--whole-archive,-lMYLIB,-Wl,--no-whole-archive
>>
>> That will link in the entire contents of the library, so all the
>> symbols defined in the library will be available.
>
> I'm trying something like that for a related purpose, but for it to work you have to replace the "," after "MYLIB" with a space. Otherwise gcc complains about an invalid option "-Wl".

I expect the linker complains about that option, not GCC.

This should also work:

-Wl,--whole-archive,-lMYLIB,--no-whole-archive

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: is there any way to change the order of name resolution in linking (aside from putting the libraries in the right order?)
  2012-09-27 14:05   ` Feuerbacher, Alan
  2012-09-27 16:06     ` Jonathan Wakely
@ 2012-09-27 17:56     ` Ian Lance Taylor
  2012-09-27 20:05       ` Dan Hitt
  1 sibling, 1 reply; 12+ messages in thread
From: Ian Lance Taylor @ 2012-09-27 17:56 UTC (permalink / raw)
  To: Feuerbacher, Alan; +Cc: Dan Hitt, gcc-help

On Thu, Sep 27, 2012 at 7:05 AM, Feuerbacher, Alan
<AFeuerbacher@allegromicro.com> wrote:
> Ian Lance Taylor wrote:
>
>
>> One thing that may work, if you are using the GNU linker or something
>> reasonably compatible, is
>>     -Wl,--whole-archive,-lMYLIB,-Wl,--no-whole-archive
>>
>> That will link in the entire contents of the library, so all the
>> symbols defined in the library will be available.
>
> I'm trying something like that for a related purpose, but for it to work you have to replace the "," after "MYLIB" with a space. Otherwise gcc complains about an invalid option "-Wl".

Sorry, I should not have duplicated the -Wl.  (See the docs for -Wl,
which is a GCC option, for how it works.)

Ian

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: is there any way to change the order of name resolution in linking (aside from putting the libraries in the right order?)
  2012-09-27 17:56     ` Ian Lance Taylor
@ 2012-09-27 20:05       ` Dan Hitt
  2012-09-27 20:08         ` Jonathan Wakely
  0 siblings, 1 reply; 12+ messages in thread
From: Dan Hitt @ 2012-09-27 20:05 UTC (permalink / raw)
  To: Ian Lance Taylor; +Cc: Feuerbacher, Alan, gcc-help

Thanks Ian, Alan, and Jonathan.

The --whole-archive option worked for me.

My extra argument (put in at the very beginning of the link command,
which was gcc-4.6 on ubuntu 12.04, for reference) was:

-Wl,--whole-archive,library1,library2,...,--no-whole-archive

(because the first library added some more unresolved symbols, so i needed
a second library, etc ---- luckily 4 were enough).

I was not able to use the -l abbreviation, so i had to put in the full
path for each library /usr/lib/....../library.a.  (That's not a complaint, just
an observation, btw.)

Of course this does make for a bigger executable but that's ok for
what i'm doing.

I appreciate everybody's help!

dan

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: is there any way to change the order of name resolution in linking (aside from putting the libraries in the right order?)
  2012-09-27 20:05       ` Dan Hitt
@ 2012-09-27 20:08         ` Jonathan Wakely
  2012-09-27 20:16           ` Dan Hitt
  0 siblings, 1 reply; 12+ messages in thread
From: Jonathan Wakely @ 2012-09-27 20:08 UTC (permalink / raw)
  To: Dan Hitt; +Cc: Ian Lance Taylor, Feuerbacher, Alan, gcc-help

On 27 September 2012 21:04, Dan Hitt wrote:
>
> I was not able to use the -l abbreviation

Why not?

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: is there any way to change the order of name resolution in linking (aside from putting the libraries in the right order?)
  2012-09-27 20:08         ` Jonathan Wakely
@ 2012-09-27 20:16           ` Dan Hitt
  2012-09-28 13:13             ` Vardhan, Sundara (GE Transportation)
  0 siblings, 1 reply; 12+ messages in thread
From: Dan Hitt @ 2012-09-27 20:16 UTC (permalink / raw)
  To: Jonathan Wakely; +Cc: gcc-help

On Thu, Sep 27, 2012 at 1:08 PM, Jonathan Wakely <jwakely.gcc@gmail.com> wrote:
> On 27 September 2012 21:04, Dan Hitt wrote:
>>
>> I was not able to use the -l abbreviation
>
> Why not?

It didn't resolve the reference.

To be really specific here (except that i have to suppress some of the dozens
of characters in the command), the command generated was:

gcc-4.6  -Wl,--whole-archive,-lX11,--no-whole-archive  [[[other stuff deleted]]]

And that command gives the same result as just putting -lX11 first (as
opposed to putting it last, which resolves everything).

(It may be that the -l option somehow signifies *.so these days?  Because
it certainly gives a much smaller executable when it works, and i had to use
the *.a forms with --whole-archive in order to make it work?)

dan

^ permalink raw reply	[flat|nested] 12+ messages in thread

* RE: is there any way to change the order of name resolution in linking (aside from putting the libraries in the right order?)
  2012-09-27 20:16           ` Dan Hitt
@ 2012-09-28 13:13             ` Vardhan, Sundara (GE Transportation)
  2012-09-30  1:06               ` Dan Hitt
  0 siblings, 1 reply; 12+ messages in thread
From: Vardhan, Sundara (GE Transportation) @ 2012-09-28 13:13 UTC (permalink / raw)
  To: Dan Hitt, Jonathan Wakely; +Cc: gcc-help

Hi Dan

I think -l defaults to .so and does not include .a if the extension is
not explicitly mentioned. However, I am not sure if anything in this
area changed in the latest gcc.

Regards

Vardhan

-----Original Message-----
From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org] On
Behalf Of Dan Hitt
Sent: Thursday, September 27, 2012 4:17 PM
To: Jonathan Wakely
Cc: gcc-help@gcc.gnu.org
Subject: Re: is there any way to change the order of name resolution in
linking (aside from putting the libraries in the right order?)

On Thu, Sep 27, 2012 at 1:08 PM, Jonathan Wakely <jwakely.gcc@gmail.com>
wrote:
> On 27 September 2012 21:04, Dan Hitt wrote:
>>
>> I was not able to use the -l abbreviation
>
> Why not?

It didn't resolve the reference.

To be really specific here (except that i have to suppress some of the
dozens of characters in the command), the command generated was:

gcc-4.6  -Wl,--whole-archive,-lX11,--no-whole-archive  [[[other stuff
deleted]]]

And that command gives the same result as just putting -lX11 first (as
opposed to putting it last, which resolves everything).

(It may be that the -l option somehow signifies *.so these days?
Because it certainly gives a much smaller executable when it works, and
i had to use the *.a forms with --whole-archive in order to make it
work?)

dan

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: is there any way to change the order of name resolution in linking (aside from putting the libraries in the right order?)
  2012-09-28 13:13             ` Vardhan, Sundara (GE Transportation)
@ 2012-09-30  1:06               ` Dan Hitt
  2012-09-30 10:00                 ` Jonathan Wakely
  0 siblings, 1 reply; 12+ messages in thread
From: Dan Hitt @ 2012-09-30  1:06 UTC (permalink / raw)
  To: Vardhan, Sundara (GE Transportation); +Cc: Jonathan Wakely, gcc-help

Thanks Sundara.

Actually, i tried several variations to try to specify the extension.

These are the variations i tried, with the results:
    -lX11 (found, but does not resolve symbols)
    -lX11a (cannot find)
    -lX11.a (cannot find)
    lX11 (cannot find)
    libX11 (cannot find)
    libX11a (cannot find)
    libX11.a (cannot find)
    llibX11.a (cannot find)
    -llibX11.a (cannot find)

Just for reference, these go in the arguments as
-Wl,--whole-archive,<<library goes here>>,--no-whole-archive

and so far, the only things that don't bail are -lX11 and
/usr/lib/...../libX11.a,
and the latter is the only thing that actually resolves any symbols.

dan

On Fri, Sep 28, 2012 at 6:13 AM, Vardhan, Sundara (GE Transportation)
<sundara.vardhan@ge.com> wrote:
> Hi Dan
>
> I think -l defaults to .so and does not include .a if the extension is
> not explicitly mentioned. However, I am not sure if anything in this
> area changed in the latest gcc.
>
> Regards
>
> Vardhan
>
> -----Original Message-----
> From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org] On
> Behalf Of Dan Hitt
> Sent: Thursday, September 27, 2012 4:17 PM
> To: Jonathan Wakely
> Cc: gcc-help@gcc.gnu.org
> Subject: Re: is there any way to change the order of name resolution in
> linking (aside from putting the libraries in the right order?)
>
> On Thu, Sep 27, 2012 at 1:08 PM, Jonathan Wakely <jwakely.gcc@gmail.com>
> wrote:
>> On 27 September 2012 21:04, Dan Hitt wrote:
>>>
>>> I was not able to use the -l abbreviation
>>
>> Why not?
>
> It didn't resolve the reference.
>
> To be really specific here (except that i have to suppress some of the
> dozens of characters in the command), the command generated was:
>
> gcc-4.6  -Wl,--whole-archive,-lX11,--no-whole-archive  [[[other stuff
> deleted]]]
>
> And that command gives the same result as just putting -lX11 first (as
> opposed to putting it last, which resolves everything).
>
> (It may be that the -l option somehow signifies *.so these days?
> Because it certainly gives a much smaller executable when it works, and
> i had to use the *.a forms with --whole-archive in order to make it
> work?)
>
> dan

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: is there any way to change the order of name resolution in linking (aside from putting the libraries in the right order?)
  2012-09-30  1:06               ` Dan Hitt
@ 2012-09-30 10:00                 ` Jonathan Wakely
  2012-10-01  4:14                   ` Dan Hitt
  0 siblings, 1 reply; 12+ messages in thread
From: Jonathan Wakely @ 2012-09-30 10:00 UTC (permalink / raw)
  To: Dan Hitt; +Cc: Vardhan, Sundara (GE Transportation), gcc-help

On 30 September 2012 02:05, Dan Hitt wrote:
> Thanks Sundara.
>
> Actually, i tried several variations to try to specify the extension.
>
> These are the variations i tried, with the results:
>     -lX11 (found, but does not resolve symbols)

At the start of the link line there are no symbols to resolve. If
-lX11 finds both libX11.so and libX11.a then it will prefer the shared
library, and --whole-archive doesn't affect shared libraries.

>     -lX11a (cannot find)

This looks for a library called libX11a.so or libX11a.a so obviously won't work.

>     -lX11.a (cannot find)

This looks for libX11.a.so or libX11.a.a, so won't work.

>     lX11 (cannot find)

This looks for a file called lX11 in the current dir.

>     libX11 (cannot find)
>     libX11a (cannot find)
>     libX11.a (cannot find)
>     llibX11.a (cannot find)

Similarly, these look for files with that exact name in the current dir.

>     -llibX11.a (cannot find)

This looks for liblibX11.a.so or liblibX11.a.a, so won't work.

> Just for reference, these go in the arguments as
> -Wl,--whole-archive,<<library goes here>>,--no-whole-archive
>
> and so far, the only things that don't bail are -lX11 and
> /usr/lib/...../libX11.a,
> and the latter is the only thing that actually resolves any symbols.

You could make the former work (e.g. with --no-as-needed or -static)
but giving the full path to the static archive works too.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: is there any way to change the order of name resolution in linking (aside from putting the libraries in the right order?)
  2012-09-30 10:00                 ` Jonathan Wakely
@ 2012-10-01  4:14                   ` Dan Hitt
  0 siblings, 0 replies; 12+ messages in thread
From: Dan Hitt @ 2012-10-01  4:14 UTC (permalink / raw)
  To: Jonathan Wakely; +Cc: Vardhan, Sundara (GE Transportation), gcc-help

(Jonathan, sorry for sending this twice, but meant to cc group
for reference)

On Sun, Sep 30, 2012 at 3:00 AM, Jonathan Wakely <jwakely.gcc@gmail.com> wrote:
> On 30 September 2012 02:05, Dan Hitt wrote:
....
>> These are the variations i tried, with the results:
>>     -lX11 (found, but does not resolve symbols)
>
> At the start of the link line there are no symbols to resolve. If
> -lX11 finds both libX11.so and libX11.a then it will prefer the shared
> library, and --whole-archive doesn't affect shared libraries.
>
>>     -lX11a (cannot find)
>
> This looks for a library called libX11a.so or libX11a.a so obviously won't work.
>
>>     -lX11.a (cannot find)
>
> This looks for libX11.a.so or libX11.a.a, so won't work.
>
>>     lX11 (cannot find)
>
> This looks for a file called lX11 in the current dir.
>
>>     libX11 (cannot find)
>>     libX11a (cannot find)
>>     libX11.a (cannot find)
>>     llibX11.a (cannot find)
>
> Similarly, these look for files with that exact name in the current dir.
>
>>     -llibX11.a (cannot find)
>
> This looks for liblibX11.a.so or liblibX11.a.a, so won't work.
>
>> Just for reference, these go in the arguments as
>> -Wl,--whole-archive,<<library goes here>>,--no-whole-archive
>>
>> and so far, the only things that don't bail are -lX11 and
>> /usr/lib/...../libX11.a,
>> and the latter is the only thing that actually resolves any symbols.
>
> You could make the former work (e.g. with --no-as-needed or -static)
> but giving the full path to the static archive works too.

Thanks Jonathan for this analysis.

-static does not work for me (it seems to have global scope, and the linking
fails because ld cannot find something ("-lgcc_s")), but --no-as-needed works
very well.

In fact, with --no-as-needed, the compiled product is only 28K (as opposed to
32K done the right way, and 1.4M done by listing all the needed .a files out
with full paths --- with --no-as-needed only -lX11 is necessary).
(And, "the right way"
means that i did a very poor job of reading the build documentation for
the system i'm using: as it turns out, you certainly can position the libraries,
or, at least position them better than i was; i was using the wrong variables
to get the libraries in.  So if i had read the docs more carefully the
first time i could have
avoided the whole adventure.)

So: thanks for pointing out this very effective, but mysterious
--no-as-needed option.

(Of course, i wonder if my test program were more vigorous if i
wouldn't run into
run-time problems at some point?  But of course that's pure speculation, and i
appreciate your help!!)


dan

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2012-10-01  4:14 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-09-27  5:09 is there any way to change the order of name resolution in linking (aside from putting the libraries in the right order?) Dan Hitt
2012-09-27  5:24 ` Ian Lance Taylor
2012-09-27 14:05   ` Feuerbacher, Alan
2012-09-27 16:06     ` Jonathan Wakely
2012-09-27 17:56     ` Ian Lance Taylor
2012-09-27 20:05       ` Dan Hitt
2012-09-27 20:08         ` Jonathan Wakely
2012-09-27 20:16           ` Dan Hitt
2012-09-28 13:13             ` Vardhan, Sundara (GE Transportation)
2012-09-30  1:06               ` Dan Hitt
2012-09-30 10:00                 ` Jonathan Wakely
2012-10-01  4:14                   ` Dan Hitt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).