Sign in to follow this  

Unity MSVC static lib linking culls some code?

This topic is 2590 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

Hi,

I use macros in quite a few places for classes that need to be created based on their name or ID. It's useful for scripting, serializing and when networking our game. It's basically a dummy struct with a constructor that is initialized before main.

Quote:

#define IMPLEMENT_OBJECT(name) struct name##Creator { name##Creator () { ObjectManager::addObject (#name, new name); } }; name##Creator name##CreatorInstance;


(Had to modify the source above to a quote or it would screw up the formatting of the page)

Ugly perhaps, but these work great in .cpp files in all cases so far, but today I decided to branch them out into their own lib because I use them in so many projects.

So it seems that if I put one in a .cpp file that contains a class that is never referenced anywhere else, it won't get called at all even though it's actually referenced in the macro itself. This happens only when in the library, and not in the projects themselves so it's not very consistent.

Putting the macro "call" in a .h file that is included didn't help. I have to actually reference the class in those files for the dummy initializer to run.

I know about the compiler option, but is there a pragma or reference trick that I can use to make the library .cpp file compiled and used in all cases like it does in my project?

[EDIT]
It's the same issue mentioned here:
Link

And there are a few mentioned "solutions" though the last one doesn't work and it's still unreferenced. The pragma also didn't work, it finds the symbol but it's not referencing it (since the code still isn't executed).

This is how I tested it:
Quote:

struct Test
{
Test ()
{
MessageBoxA (NULL, "Test", "It works!", MB_OK);
}
};
Test TestInstance;

#pragma comment(linker,"/include:TestInstance")

[/EDIT]

Global Compiler/Linker options have no effect.

Thank you for any suggestions!

[Edited by - SymLinked on November 12, 2010 2:09:18 AM]

Share this post


Link to post
Share on other sites
Ah, this wonderful bug. I don't think there is any real solution, but there is a workaround:

1) Add a:
void doAbsolutelyNothing() { }
function in the same source file as the instances of the objects you are creating. If you have multiple source files with instances, you'll need multiple functions with different names.

2) Call this:
doAbsolutelyNothing();
function from some other part of your source, such as in initialization code, or code which is only called once.

The VC linker doesn't include object files in the final .exe with no external references as it assumes they're never used (it's apparently not smart enough to notice that object variables are simply instanced in that compilation unit). By adding an external reference, you force the inclusion of that object in the linker.

You could optionally make a variable reference to your TestInstance from some other part of your source which would work just as well, but since you're generating these names with macros, that seems like it would be much more difficult for you.

Share this post


Link to post
Share on other sites
It's not a bug. This is related to dynamic initializers.

Gory details for the GNU linker, but the idea seems to be the same for the Visual C++ linker in my experience.

Have you tried passing /OPT:NOREF to the linker? (Though beware of dragging in lots of other bloat).

Share this post


Link to post
Share on other sites
Quote:
Original post by SymLinked]Ugly perhaps, but these work great in .cpp files in all cases so fars ...

That is not far from my description of a hack and sometimes when you play with fire you do get burnt.

You are relying on the static initialisation of an object which may not be used, to access global (probably) state in ObjectManager::addObject.

Quote:
Original post by SymLinkedThank you for any suggestions!

You may not like it but fix the source of the problem not the current problem.

Share this post


Link to post
Share on other sites

This topic is 2590 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Forum Statistics

    • Total Topics
      628734
    • Total Posts
      2984444
  • Similar Content

    • By INTwindwolf
      THE PROJECT

      INT is a 3D Sci-fi RPG with a strong emphasis on story, role playing, and innovative RPG features such as randomized companions. The focus is on the journey through a war-torn world with fast-paced combat against hordes of enemies. The player must accomplish quests like a traditional RPG, complete objectives, and meet lively crew members who will aid in the player's survival. Throughout the game you can side and complete missions through criminal cartels, and the two major combatants, the UCE and ACP, of the Interstellar Civil War.
      Please note that all of our current positions are remote work. You will not be required to travel.
      Talent Needed
       
      Unity Engine Programmer
      Website Administrator
      3D Animator
      We have made great strides in the year 2017! INT has received a comprehensive face-lift compared to the start of the year. We look forward to a productive, fruitful year 2018!
      Revenue-Share
      This is the perfect opportunity to get into the game development industry. Being an Indie team we do not have the creative restrictions often imposed by publishers or other third parties. We are extremely conscientious of our work and continuously uphold a high level of quality throughout our project.
      We are unable to offer wages or per-item payments at this time. However revenue-sharing from crowd-funding is offered to team members who contribute 15-20 hours per week to company projects, as well as maintain constant communication and adhere to deadlines. Currently the crowd-funding campaign is scheduled for the year 2018. Your understanding is dearly appreciated.
       
      Thank you for your time! We look forward to hearing from you!
       
      John Shen
      HR Lead
      Starboard Games LLC
    • By Apollo Cabrera
      Energy particles being harnessed by collection multi-hedron energy matrix. Whuuuttt?
      Love it :)
    • By AndySv
        Total Music Collection (http://u3d.as/Pxo)   THE COLLECTION CONTAINS:   Mega Game Music Collection   Universal Music Collection   Huge library of high quality music for any project! All at an incredibly low price!   - 2,5GB of high quality audio - 100+ different music tracks - Loop and short versions   Action, fantasy, casual, horror, puzzle, epic, dramatic, romantic, positive, inspiring, motivational and more!
    • By Dafu
      FES Retro Game Framework is now available on the Unity Asset Store for your kind consideration!
      FES was born when I set out to start a retro pixel game project. I was looking around for an engine to try next. I tried a number of things, from GameMaker, to Fantasy Consoles, to MonoGame and Godot and then ended up back at Unity. Unity is just unbeatable in it's cross-platform support, and ease of deployment, but it sure as heck gets in the way of proper retro pixel games!
      So I poured over the Unity pipeline and found the lowest levels I could tie into and bring up a new retro game engine inside of Unity, but with a completely different source-code-only, classic game-loop retro blitting and bleeping API. Months of polishing and tweaking later I ended up with FES.
      Some FES features:
      Pixel perfect rendering RGB and Indexed color mode, with palette swapping support Primitive shape rendering, lines, rectangles, ellipses, pixels Multi-layered tilemaps with TMX file support Offscreen rendering Text rendering, with text alignment, overflow settings, and custom pixel font support Clipping Sound and Music APIs Simplified Input handling Wide pixel support (think Atari 2600) Post processing and transition effects, such as scanlines, screen wipes, screen shake, fade, pixelate and more Deploy to all Unity supported platforms I've put in lots of hours into a very detail documentation, you can flip through it here to get an better glimpse at the features and general overview: http://www.pixeltrollgames.com/fes/docs/index.html
      FES is carefully designed and well optimized (see live stress test demo below). Internally it uses batching, it chunks tilemaps, is careful about memory allocations, and tries to be smart about any heavy operations.
      Please have a quick look at the screenshots and live demos below and let me know what you think! I'd love to hear some opinions, feedback and questions!
      I hope I've tickled your retro feels!



      More images at: https://imgur.com/a/LFMAc
      Live demo feature reel: https://simmer.io/@Dafu/fes
      Live blitting stress test: https://simmer.io/@Dafu/fes-drawstress
      Unity Asset Store: https://www.assetstore.unity3d.com/#!/content/102064

      View full story
    • By Dafu
      FES Retro Game Framework is now available on the Unity Asset Store for your kind consideration!
      FES was born when I set out to start a retro pixel game project. I was looking around for an engine to try next. I tried a number of things, from GameMaker, to Fantasy Consoles, to MonoGame and Godot and then ended up back at Unity. Unity is just unbeatable in it's cross-platform support, and ease of deployment, but it sure as heck gets in the way of proper retro pixel games!
      So I poured over the Unity pipeline and found the lowest levels I could tie into and bring up a new retro game engine inside of Unity, but with a completely different source-code-only, classic game-loop retro blitting and bleeping API. Months of polishing and tweaking later I ended up with FES.
      Some FES features:
      Pixel perfect rendering RGB and Indexed color mode, with palette swapping support Primitive shape rendering, lines, rectangles, ellipses, pixels Multi-layered tilemaps with TMX file support Offscreen rendering Text rendering, with text alignment, overflow settings, and custom pixel font support Clipping Sound and Music APIs Simplified Input handling Wide pixel support (think Atari 2600) Post processing and transition effects, such as scanlines, screen wipes, screen shake, fade, pixelate and more Deploy to all Unity supported platforms I've put in lots of hours into a very detail documentation, you can flip through it here to get an better glimpse at the features and general overview: http://www.pixeltrollgames.com/fes/docs/index.html
      FES is carefully designed and well optimized (see live stress test demo below). Internally it uses batching, it chunks tilemaps, is careful about memory allocations, and tries to be smart about any heavy operations.
      Please have a quick look at the screenshots and live demos below and let me know what you think! I'd love to hear some opinions, feedback and questions!
      I hope I've tickled your retro feels!



      More images at: https://imgur.com/a/LFMAc
      Live demo feature reel: https://simmer.io/@Dafu/fes
      Live blitting stress test: https://simmer.io/@Dafu/fes-drawstress
      Unity Asset Store: https://www.assetstore.unity3d.com/#!/content/102064
  • Popular Now