Forcing global constructors to execute

Started by
21 comments, last by doynax 19 years, 5 months ago
Quote:Original post by ajas95
Hmm. It's only guaranteed to be constructed at some point before first-use. So even though you call the constructor, unless you actually /use/ the instance, I don't think you can assume it's constructed.

This is not true. Object construction order is based on the order they appear in the translation unit relative to scope. Whether you use the object or not has no bearing. In the case of multiple translation units, as in this case, you have no guarantee that if one global/static member object uses another one in a separate translation that the second one is initialized first. It's russian roulette whether or not the result is what you'd want. When you first use an object has no bearing on when it is constructed. Again, as I said in my previous post, this is why people often use statics inside functions, since they are constructed the first time the function is called, making construction order (though not destruction order) clearly defined.

Edit: Actually, not in this case, since after reading his source code he's not relying on another object's construction.

[Edited by - Polymorphic OOP on November 8, 2004 6:52:10 PM]
Advertisement
Quote:Original post by eq
No the constructor is NOT called. I can put an __asm int 3; and there's no break. My test cases are obvisouly flaved, but here's a project that breaks: http://hem.bredband.net/b295424/test.zip (BTW how do you post links?)

It's basicly one lib with 2 classes, and a main console project.

This is strange. Using VC++ 7.1, I notice that if I switch the lib to just be an executable and run that, the constructor gets called as it should, otherwise, if I leave it as a lib an just link with it in the other executable, the constructor isn't called. I dunno what to tell you. Unless I missed something, I'm stumped.

Edit: I'm talking about TestA8::m_format's constructor in case I wasn't clear.
The "solutions" I've seen so far are basicly the modification I've done in my last post.
Basicly one .cpp file is created which references all objects in all libs that needs to be linked.
Here's another sample workspace using this method:

http://hem.bredband.net/b295424/test2.zip

This is semi-ok, but you must do more work than simple linking with the library to be able to use it correctly, namely adding the file "ImageFileFormat_lists.cpp" to you main executable project. I'd still like some "magic" code that forces something to be linked and executed.

i can't help you with this specific problem on MSVC, but i have seen this on other compilers. in particular, Metrowerks C++ for PS2.

to get around this problem, we used the Metrowerks specific pragma "#pragma force_active on"

don't know what (if anything) MSVC might have to offer for that.
Thats exactly what I'm looking for, I do belive that GCC has a similar thing: __attribute__((used)) to force the linker to generate code for an object. I can't seem to find the eqvivalent for MSVC / Intel C++ though... any hints welcome.
I use vc71 2003 and the constructor is called.
Quote:Original post by Leadorn
I use vc71 2003 and the constructor is called.

Odd... I tested it on VC++ 7.1 and it wasn't. Did you change any settings?
Quote:Original post by eq
Thats exactly what I'm looking for, I do belive that GCC has a similar thing: __attribute__((used)) to force the linker to generate code for an object. I can't seem to find the eqvivalent for MSVC / Intel C++ though... any hints welcome.


Here you can find out about the #pragma optimize directive.

It's basically like
#pragma optimize( "", off )void foo(){    int a = 5;}#pragma optimize( "", on ) 
If I recall, making it static makes it initialize on first use. Why not just make it global and non-static?
Quote:Original post by Polymorphic OOP
Quote:Original post by Leadorn
I use vc71 2003 and the constructor is called.

Odd... I tested it on VC++ 7.1 and it wasn't. Did you change any settings?


Ive retested it with a totaly new empty console project and it calls the constructor on the static Test test object.

This topic is closed to new replies.

Advertisement