Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have a bunch of variables that should be initialized then my game launches, but must of them are never initialized.

Here is an example of the code:

MyClass.h

class MyClass : public BaseObject {
    DECLARE_CLASS_RTTI(MyClass, BaseObject);
    ...
};

MyClass.cpp

REGISTER_CLASS(MyClass)

Where REGISTER_CLASS is a macro defined as follow

#define REGISTER_CLASS(className)\
class __registryItem##className : public __registryItemBase {\
    virtual className* Alloc(){ return NEW className(); }\
    virtual BaseObject::RTTI& GetRTTI(){ return className::RTTI; }\
}\
\
const __registryItem##className __registeredItem##className(#className);

and __registryItemBase looks like this:

class __registryItemBase {
    __registryItemBase(const _string name):mName(name){ ClassRegistry::Register(this); }
    const _string mName;
    virtual BaseObject* Alloc() = 0;
    virtual BaseObject::RTTI& GetRTTI() = 0;
}

Now the code is similar to what I currently have and what I have works flawlessly, all the registered classes are registered to a ClassManager before main(...) is called. I'm able to instantiate and configure components from scripts and auto-register them to the right system etc...

The problem arrises when I create a static library (currently for the iPhone, but I fear it will happen with android as well). In that case the code in the .cpp files is never registered.

Why is the resulting code not executed when it is in the library while the same code in the program's binary is always executed?


Bonus questions:

For this to work in the static library, what should I do?

  • Is there something I am missing?
  • Do I need to pass a flag when building the lib?
  • Should I create another structure and init all the __registeredItem##className using that structure?
share|improve this question

closed as off-topic by Josh Petrie Dec 20 '13 at 1:49

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Programming questions that aren't specific to game development are off-topic here, but can be asked on Stack Overflow. A good rule of thumb is to ask yourself "would a professional game developer give me a better/different/more specific answer to this question than other programmers?"" – Josh Petrie
If this question can be reworded to fit the rules in the help center, please edit the question.

2  
I remember having the same issue.. where I initialized a factory through a bunch of global variables. Didn't work as a static lib. Ended up writing an "init" function that had to be called by the application. –  Jari Komppa Sep 27 '12 at 18:43
    
@JariKomppa yes, this confirms snake5's answer. I was hoping for a more dynamic option, I will do the same (init function) then if time allows I will explore a few other options. Thanks. –  Coyote Sep 28 '12 at 6:12
    
Note that names containing two consecutive underscores anywhere, starts with an underscore and a capital letter, or is in the global namespace and starts with an underscore are all reserved for the implementation. –  Lars Viklund Dec 20 '13 at 12:59
    
@JoshPetrie Why not move the question to stack overflow directly instead of closing it? It's been sitting in Game Dev for over a year... Why close it now? –  Coyote Dec 20 '13 at 15:22
    
@Coyote The rules of the site have changed a bit since this question was asked. Closing old questions to new rules is one way to make sure people searching the site know what kind of questions are off and on topic (if they see someone else doing it, it makes it seem OK). Further, questions can't be migrated this long after they've been asked. It's not something that you did wrong. It's OK for the question to be closed, it will still remain on the site and people can still benefit from it. –  Byte56 Dec 20 '13 at 15:42
show 1 more comment

2 Answers 2

My guess is that none of those static library variables are used directly by the executable. The compiler likes to optimize these situations by removing the initialization code of unused variables.

For this thing to work, you have to at least touch each variable (do a read/write that cannot be optimized out*) that you want initialized from all static libraries. This basically means creating a function in the library that touches all variables and is called from the executable, somewhere before the point the variables are actually used for the first time.

*- Since this task heavily depends on compiler type, settings and many other things, I'll leave it up to you. For starters, you could try printf'ing some of the contents of those variables.

share|improve this answer
    
I feared the solution could not be automated. The variables serve no real purpose apart from forcing the initialization. I wouldn't like having to touch them manually as this introduces more room for human error. Some of the code of the library is never exposed to the application so maybe I'll keep it all organized myself in the meantime. –  Coyote Sep 28 '12 at 3:44
    
"none of those static library variables are used directly by the executable" Marked as the correct answer. But I'm sure some references and a mode detailed peek into gcc and/or clang would be instructive. –  Coyote Aug 11 '13 at 7:39
add comment
up vote 1 down vote accepted

This answer actually fixes the issue:

Use the -all_load linker option to load all members of static libraries. Or for a specific library, use -force_load path_to_archive.

In Xcode, you'll want to add these options under "Other Linker Flags" for your executable (not your static library).

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.