Jump to content
  • Advertisement
Sign in to follow this  
ph33r

I tricked the linker :(

This topic is 4612 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I wrote a piece of code that during construction will register itself to a factory. Unfortunately, the instance of the object I put in its own object file. This causes a seperate .obj file that will never be referenced anywhere else in the code base. Since the .obj file doesn't get referenced it doesn't get linked into the main executable. This causes the static instance never to be constructed and the factory never gets registered. Is there anyway to tell the main executable, using cl.exe, to give a hint that it needs the .obj? The code I've come up with is really clean and elegant and I'd like to avoid hacking it up just to get it to link. Any suggetions?

Share this post


Link to post
Share on other sites
Advertisement
What good is the object if you never access it? I guess it depends on what it does, exactly - so what's it do?

Share this post


Link to post
Share on other sites
How does the registration process for your factory work? If you're using a static instance of some sort I would have expected the linker to include the .obj file.

Share this post


Link to post
Share on other sites
Quote:
Original post by joanusdmentia
How does the registration process for your factory work? If you're using a static instance of some sort I would have expected the linker to include the .obj file.



// .h
class DerivedFactory : BaseFactory
public:

virtual Memory* AllocationScheme() { return new int; }

// .cpp file
static FactoryInstaller<DerivedFactory> factory;


What happens is in FactoryInstaller's ctor it registers itself, and the dtor removes itself. The allocation scheme is part of BaseFactory so DerivedFactory never needs to be explicitly referenced.

In short: Factory installer adds Derived Factory and all the functionality you need is through base class functionality. This causes DerivedFactory to be compiled but never linked because no one references any of its functionality.






Share this post


Link to post
Share on other sites
Hmm..... seems strange to me. I've never had any trouble with this scenario before. Are you doing anything else nonstandard with the linker?

Share this post


Link to post
Share on other sites
Could you put the FactoryInstaller<DerivedFactory> object in the source file for DerivedFactory? Or anywhere else that has referenced code? This IS typical behavior, but I'm not sure if there's a way to override it.

Share this post


Link to post
Share on other sites
Quote:
Original post by RDragon1
Could you put the FactoryInstaller<DerivedFactory> object in the source file for DerivedFactory? Or anywhere else that has referenced code? This IS typical behavior, but I'm not sure if there's a way to override it.


That is the solution I have found to work, unfortunately it's not something I find acceptable. What ends up happening is you have to manage a .cpp/.h file that has a list of all factories. In my opinion having a seperate module that can be moved/linked in is much cleaner.

I'm hoping there is a way to tell the linker that this obj file needs to be linked in. I suppose I can compile the .obj file as a .lib and statically link it in.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
Hmm..... seems strange to me. I've never had any trouble with this scenario before. Are you doing anything else nonstandard with the linker?


Really? There is no possible way that the linker can know that the object file is actually needed without hints from the programmer.

Though, no I'm not doing anything non-standard.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I was thinking "volatile", but that won't affect the linker, only the compiler, right? What compiler are you using, by the way?

Share this post


Link to post
Share on other sites
I've had similar problems before. The simple solution is to turn off the linker option that rips out unreferenced code ;)

Another solution that may work for you is to put the static variable declaraion in the header file. This isn't ideal, but it helps the linker to realize specifically that you want a template instantiation of that type, and not to rip it out (as there's technically a global variable floating around now... put it in a namespace or something to make it prettier).

I experimented with making the static variable instance a member of the class itself, but the linker rips that one out too...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!