Re: Strange behaviour of linker (vc++)

From: Igor Tandetnik (itandetnik_at_mvps.org)
Date: 09/27/04


Date: Mon, 27 Sep 2004 15:11:43 -0400


"Philip Lawatsch" <philip@waug.at> wrote in message
news:41585a58$1@e-post.inode.at
>> You can force an inclusion of the symbol with /INCLUDE linker command
>> line option, but that kinda defeats the purpose.
>
> Well, I disagree. At least partially. I suppose one has reasons as to
> why one would initialise a static variable via a function.
>
> Anyway, I have tons of such things and they work fine with g++. So, is
> there anything I can do to force all "dead" code into my binary? Its
> not reasonable to specify every symbol per hand, this is hell a lot
> of work.

That's precisely what I meant by "defeats the purpose". The idea of the
idiom is to build the catalog implicitly, just by linking with
corresponding libraries, but /INCLUDE trick makes you reference each
class explicitly.

Another alternative is /OPT:NOREF linker option, which instructs the
linker to keep everything in, whether used or not. This may result in a
large executable.

Yet another is to have some kind of Init() function in your library, and
have the client call it. You are back to assembling your catalog
explicitly, but at least you don't need to figure out the exact mangled
name of a particular variable.

>> Needed how? As far as linker is concerned, no outside module ever
>> references this symbol, so it does not need to be included.
>
> Yea, but there is a function wich initialises a static in there. This
> function references outside stuff. So imo the linker should not
> discard it.

Well, the linker decides whether to include a function not based on what
the function references, but based on whether the function _itself_ is
referenced by something. Otherwise two functions calling each other
would never be discarded, even though neither is being called. Once the
linker decides that the function is needed, it further pulls in the
stuff referenced by this function.

-- 
With best wishes,
    Igor Tandetnik
"On two occasions, I have been asked [by members of Parliament], 'Pray,
Mr. Babbage, if you put into the machine wrong figures, will the right
answers come out?' I am not able to rightly apprehend the kind of
confusion of ideas that could provoke such a question." -- Charles
Babbage


Relevant Pages

  • Re: Home brew Z80 CP/M computer
    ... nice pile of assemblers and linkers. ... But it doesn't say what the SLR tools do with it. ... The thing that I absolutely _LOVED_ about the SLR tools was how well they dealt with external references. ... Reading between the lines, it was easy enough to figure out that Steve had defined a stack machine to do the address calculations, and embedded that into the linker. ...
    (comp.os.cpm)
  • Re: Phanerozoic Geology, or, Wheres the Sequel?
    ... >> to pick up some geology, but without spending months on it. ... It is now clear to me that I should clarify my purpose, ... I had intended to do a more comprehensive "history of nature" ... I have written a detailed set of questions about these references, ...
    (sci.geo.geology)
  • Re: Im a stupid blond :( Please help me!!!
    ... > in a program to be left out of the external references table. ... Well for one the linker that comes with Microsoft Visual Studio 6 (not ... It's the loader's job to adjust ... > get the entire object file that contains that function linked in. ...
    (comp.lang.cpp)
  • Re: strange linking problem
    ... I usually use C, and since strings ... > files and looks for unsatisfied references to functions and variables. ... then I don't understand how the linker does not either ... that there should be a data area of a certain size and a certain name. ...
    (comp.lang.fortran)
  • Re: Strange linker warning?
    ... The warning means that the linker optimized away all references to functions ... The optimization kicks in only after all symbols are resolved. ... your link is pulling in something that references CEDDK. ...
    (microsoft.public.windowsce.embedded.vc)