Article View: comp.unix.programmer
Article #3623Re: Force linking of .a?
From: Dale Hagglund
Date: Tue, 11 May 1999 00:00
Date: Tue, 11 May 1999 00:00
70 lines
3408 bytes
3408 bytes
psmith@baynetworks.com (Paul D. Smith) writes:
> I've seen that I can add -uSYMBOL to the link line and that works for a
> given .o, but it's gross since there will undoubtedly be many more cases
> of this in the future. Similarly, I'd like to avoid dummy function
> calls, etc.
>
> What we'd like to do is find some kind of linker flag (or other method)
> that forces the entire contents of particular .a's to be linked, even if
> nothing appears to reference any of the symbols in some of the .o's in
> the archive.
>
> I tried the docs for ld(1) but didn't see anything; surely this isn't
> such an unusual requirement? Anyone have any ideas?
I'd approach this problem something like this: as the last step of the
build process, automatically create a small .c file that contains the
references you want, by examining the public symbols contained in each
archive. Then, compile that, and include it as one of the .o's on the
final link. This avoids any dependency on special linker flags, plus,
it's fairly easily extensibleif you need finer grained control of what
is linked in and what isn't.
In your makefile, list the libraries that you want to force to be
linked in a specific variable, for example
FORCE_LIBS = archive1.a archive2.a
and so on.
Add a make target that looks like:
librefs.c: $(FORCE_LIBS)
In the commands for this target, use "nm" to extract the public
symbols from the .a's in $(FORCE_LIBS), and from this, create a
librefs.c file that looks something like
extern int symbol1;
extern int symbol2;
...
static void *references[] = {
&symbol1,
&symbol2,
...
};
(A file like this is strictly non-conforming C, since the declarations
of the symbols won't match the actual types, but it won't matter to
the linker.)
On my particular system, "nm -t -n" seems the right way to use nm
command for this purpose. Each line of output looks like
object|type|symbol
From output like this, a fairly straight-forward awk or perl script
should be able to generate the necessary C program.
Then, add librefs.o to the dependencies of the final build target, and
list it before any other objects on the link line. The default make
rules will ensure librefs.c is built and up to date, and then compile
it into librefs.o, which will then provide references for each symbol
in your archives.
program: librefs.o $(OBJECTS)
$(LD) -o $@ pullin.o $(OBJECTS) ...
Dale.
Technical Details
Message-ID:
<86emknfo17.fsf@ponoka.battleriver.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<p5ogjs8dgz.fsf@baynetworks.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 11318c,9f41c0c55a58cbbd X-Google-Attributes: gid11318c,public X-Google-Thread: fc89c,9f41c0c55a58cbbd X-Google-Attributes: gidfc89c,public X-Google-Thread: 11035d,9f41c0c55a58cbbd X-Google-Attributes: gid11035d,public From: Dale Hagglund <rdh@best.com> Subject: Re: Force linking of .a? Date: 1999/05/11 Message-ID: <86emknfo17.fsf@ponoka.battleriver.com> X-Deja-AN: 476690670 Sender: rdh@ponoka.battleriver.com References: <p5ogjs8dgz.fsf@baynetworks.com> Content-Type: text/plain; charset=us-ascii X-Trace: nntp1.ba.best.com 926448030 217 207.21.140.73 Organization: None to speak of. User-Agent: Gnus/5.070083 (Pterodactyl Gnus v0.83) Emacs/20.3 MIME-Version: 1.0 Newsgroups: comp.unix.programmer,comp.unix.solaris,gnu.gcc.help