linker dead-stripping my variables! 
Author Message
 linker dead-stripping my variables!

Hey all, I tried finding this in archives/google groups but haven't found
anything suitable as a response.

Part of a static library i'm writing has WinSock functionality.
Accordingly, I'm responsible for invoking "WSAStartup()" and "WSACleanup()"
at the right times.  Ideally, i'd like to do something like this:

My internal .cpp file:
---------------------

namespace
{
    class WSAInitializer
    {
    public:
      WSAInitializer() { ::WSAStartup(...); }
      ~WSAInitializer() { ::WSACleanup(); }
    };

    WSAInitializer wsaInitializer;

Quote:
}

But the linker is dead-stripping this because my .cpp file is never
referenced from outside the library.

I know that in Metrowerks there's a #pragma "force_active" that forces the
linker not to dead-strip the contents of the file, but i can't find anything
similar for VS.NET.

I also know that I can get around this by #including my way to an .obj file
that the linker can't dead-strip, but i'd prefer to not do it that way...

Does anyone have any suggestions?  This also has implications with
auto-registering factory components, so i can't just have Startup() &
Shutdown() methods instead.

Thanks in advance for any advice,
Charles Nicholson



Wed, 10 Aug 2005 05:58:45 GMT  
 linker dead-stripping my variables!
You can use /INCLUDE linker swich, or you can make sure something in
this OBJ file is actually referenced by other source files that end up
using WinSock after all. Something like

// in initializing .cpp

int dummy;

// in every .cpp that needs WinSock

int* pDummy = &dummy;

Another variation on this theme, sometimes referred to as Schwartz
counters:

// in initializing .cpp
class WSAInitializer {/*...*/};
void InitializeWSA()
{
    // gets constructed the first time the function is called
    static WSAInitializer init;

Quote:
}

// in .cpp files using WinSock:

namespace
{
    struct Counter
    {
        Counter() {InitializeWSA();}
    } counter;

Quote:
};

--
With best wishes,
    Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken


Quote:
> Hey all, I tried finding this in archives/google groups but haven't
found
> anything suitable as a response.

> Part of a static library i'm writing has WinSock functionality.
> Accordingly, I'm responsible for invoking "WSAStartup()" and
"WSACleanup()"
> at the right times.  Ideally, i'd like to do something like this:

> My internal .cpp file:
> ---------------------

> namespace
> {
>     class WSAInitializer
>     {
>     public:
>       WSAInitializer() { ::WSAStartup(...); }
>       ~WSAInitializer() { ::WSACleanup(); }
>     };

>     WSAInitializer wsaInitializer;
> }

> But the linker is dead-stripping this because my .cpp file is never
> referenced from outside the library.

> I know that in Metrowerks there's a #pragma "force_active" that forces
the
> linker not to dead-strip the contents of the file, but i can't find
anything
> similar for VS.NET.

> I also know that I can get around this by #including my way to an .obj
file
> that the linker can't dead-strip, but i'd prefer to not do it that
way...

> Does anyone have any suggestions?  This also has implications with
> auto-registering factory components, so i can't just have Startup() &
> Shutdown() methods instead.

> Thanks in advance for any advice,
> Charles Nicholson



Wed, 10 Aug 2005 06:26:47 GMT  
 linker dead-stripping my variables!
Igor, thank you for the quick reply.


Quote:
> You can use /INCLUDE linker swich, or you can make sure something in
> this OBJ file is actually referenced by other source files that end up
> using WinSock after all. Something like

> // in initializing .cpp
> int dummy;

> // in every .cpp that needs WinSock
> int* pDummy = &dummy;

This is ok, but the problem is that the only .cpp that "needs" winsock is
the .cpp file in question!
My win32-specific .cpp file is an implementation of a pure virtual interface
that will later also be provided by other platform-specific child classes.
I don't know whether or not future child classes will have similar
subsystem-initialization requirements (i.e. WSAStartup()).

It seems {*filter*} to have my base class (or some other 'known' and externally
referenced file) sullied with a 'hook' from my win32 child class to force it
to init...  If there's no other way to do it, i'll do it, but it doesn't
seem clean.

I'm also not too crazy about using the "/INCLUDE" linker switch, I'd prefer
to not do that unless absolutely necessary (it's a big project, and has a
bunch of linker switches already, i'd like to keep it from exploding with
every new static-init symbol, like _every_ factory product in our system).

I found the following in MSDN that force the linker to include symbols, but
i don't know what their deal is....

HRESULT __stdcall get_ForceSymbolReferences(
   /* [out, retval] */ BSTR* retVal
);
HRESULT __stdcall put_ForceSymbolReferences(
   /* [in] */ BSTR NewValue
);

Does anyone have information as to what these are?  Can i use them?  Where
do they live?

Thanks again for your replies.

Regards,
Charles Nicholson

Quote:
> --
> With best wishes,
>     Igor Tandetnik

> "For every complex problem, there is a solution that is simple, neat,
> and wrong." H.L. Mencken



> > Hey all, I tried finding this in archives/google groups but haven't
> found
> > anything suitable as a response.

> > Part of a static library i'm writing has WinSock functionality.
> > Accordingly, I'm responsible for invoking "WSAStartup()" and
> "WSACleanup()"
> > at the right times.  Ideally, i'd like to do something like this:

> > My internal .cpp file:
> > ---------------------

> > namespace
> > {
> >     class WSAInitializer
> >     {
> >     public:
> >       WSAInitializer() { ::WSAStartup(...); }
> >       ~WSAInitializer() { ::WSACleanup(); }
> >     };

> >     WSAInitializer wsaInitializer;
> > }

> > But the linker is dead-stripping this because my .cpp file is never
> > referenced from outside the library.

> > I know that in Metrowerks there's a #pragma "force_active" that forces
> the
> > linker not to dead-strip the contents of the file, but i can't find
> anything
> > similar for VS.NET.

> > I also know that I can get around this by #including my way to an .obj
> file
> > that the linker can't dead-strip, but i'd prefer to not do it that
> way...

> > Does anyone have any suggestions?  This also has implications with
> > auto-registering factory components, so i can't just have Startup() &
> > Shutdown() methods instead.

> > Thanks in advance for any advice,
> > Charles Nicholson



Wed, 10 Aug 2005 06:49:56 GMT  
 linker dead-stripping my variables!

Quote:
> Igor, thank you for the quick reply.



> > You can use /INCLUDE linker swich, or you can make sure something in
> > this OBJ file is actually referenced by other source files that end
up
> > using WinSock after all. Something like

> > // in initializing .cpp
> > int dummy;

> > // in every .cpp that needs WinSock
> > int* pDummy = &dummy;

> This is ok, but the problem is that the only .cpp that "needs" winsock
is
> the .cpp file in question!

Now I'm confused. If this is the only file that uses WinSock, and the
main program does not use anything from this file, then you don't need
to initialize WinSock after all since nobody's gonna use it anyway,
right? As soon as the main program references something from that file,
you'll get your WinSock initialization back. Am I missing something
obvious?

I guess a rough sketch of your architecture would help. Who's calling
whom and how is it all organized into libraries?
--
With best wishes,
    Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken



Wed, 10 Aug 2005 07:07:40 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. dead-stripping static initialization code

2. Dead Stripped Code?

3. Can linker report dead code?

4. Global Variable Linker Question...

5. Help - I need to set variable A = value held in variable named in variable B

6. Why strip comment (was: Want a way to strip comments ...)

7. <<<New to Group: Is ATL Dead? Is COM dead?>>>

8. URL parameters stripped before getting to aspx

9. Strip non-printables from string

10. Horrible user control bug stripped all user controls from my project

11. Function to strip \n and \r from strings

12. Stripping spaces from a string

 

 
Powered by phpBB® Forum Software