c++ static object not created

Kövesdi György kgy@deverto.hu
Thu Nov 20 10:28:00 GMT 2008


> Can you give us an example of this not working?  It really should work,
> and it works for me.
Your example is really works.
I checked the difference in my case, and found that the problematic object is 
put in an archive file:

---------- x.cpp: -----------
#include <iostream>
using namespace std;
class SomeClass
{
public:
  SomeClass()
  {
    cout << "Hello!\n";
  }
};
static SomeClass my_instance;
------------- main.cpp: ------------
int main()
{
}
-----------------------------------------
What i did:
g++ main.cpp -c -o main.o
g++ x.cpp -c -o x.o
ar r x.a x.o
g++ -o good main.o x.o
g++ -o bad main.o x.a

Try to start 'bad' and 'good' executables: the 'bad' does not execute the 
constructor. Is there any difference in linking or I created the archive 
wrong way?

Regards
K. Gy.



More information about the Gcc-help mailing list