To my knowledge the term "static library" does not exist on the win32 platform. The object code of a .dll is never linked to the code of the excutable (only the .lib stub is linked). Please enlighten my on how can I produce a .lib file containing my object code without a .dll - Thanks in advance.
To your problem now. Your approach is correct and I have also used it several times to do just what you are describing.
Including the above code fragment in a library will make the InitClass constructor to be called right after the .dll is mapped into your process' address space (after it gets loaded).
If the executable does not reference any of the symbols contained in the .dll, the linker may NOT link the .dll to your executable (depends on the compiler, build options etc.). You need to reference at least one symbol (variable, class, function) exported from your library, or load the .dll explicitly by using the LoadLibrary() call. The latter will also force the initialization of any statically declared variables or class instances.
P.S. The abovementioned problem can only be experienced in the win32 platform, since most unices I've been working on link the shared object unconditionally.
Thanks for the info. However I do not use the IDE. Is it possible for you to send me the command line used to create a static library?
Based on my experience with the Win32 platform the term static refers to the C runtime that will be linked to the created library, not the type of the library being built.
Do you say for example that after you create a static library you don't have a .dll file created? I mean that if the .dll file does get created, then it is NOT a static library!
To your problem now. Are you a 100% sure that the .lib is linked to your executable? Use the depends.exe tools to verify. If not this is your problem: Try loading the .dll file explicitly using the LoadLibrary() call and the static classes in the .dll will get initialized. Otherwise I need to see some code. I would also suggest you try something really simple - a library will a single class such as the one contained in my previous post.
To be honest this close to what I've been doing sometime: renaming the XX.obj object file to XX.lib. The .lib and .obj extensions for the VC compiler seem to be the same.
In your problem now. From the 2 bullets/comments regarding the behavior of your exe, it seems that if you use this way or another the class from the library its static member gets initialized otherwise not. This simply means that the linker avoids linking your executable with the lib since no references to it are found.
Compare the size of your exe with and without linking the .lib - given that you don't instanciate explicitly the "offending" class. I believe that the size will be in both cases the same.
In order to maintain unreferced symbols during the link phase, pass the option /OPT:NOREF to the linker. Command line should look like:
Well to my knowledge if you declare an object outside of any function body or class declaration like...
Its declared at global scope, and is called global object. You dont need to decalre it with static keyword. I guess in C, if you want to limit the scope of a var, you declare it with static (even though a class is meaning-less to C) may be using static limits the visibility of the obj somehow. But then the compiler would have complained.
But i dont think C++ standard different set of rules for lib or main programs.
Hi again. Unfortunately but what you are trying to do seems not to be possible when your library is statically linked to your executable.
Find attached an example, along with a README file, on how to force the linker to include a non-referenced symbol in the output executable. Not a very convenient way though, since the name of the symbol needs to be known a-priori (using the dumpbin.exe /symbols xxx.lib) in order to include it during the link.
Note also that the static does not apply in your case since external linkage is required for symbols that have to be mapped to your executable initialized data segment.
If I find a more elegant solution I'll let you know ...