removal of unreferenced static variables

Simon MORLAT simon_morlat@hp.com
Tue Apr 1 15:30:00 GMT 2003


Hello all,

Is there an option to prevent gcc to skip out of a program unreferenced
static objects ?
I've looked at the manual but found nothing that worked.

Here is my practical example (stupid here, but simplified from a real
case):
in object1.h
class Object1
{
	public:
		Object1(){
			std::cout<<"Object 1 is living"<<endl;
		}
}

in object2.h:
#include "object1.h"
class Object2
{
	public:
		void fantasticFunction(){
			cout<<"does nothing interesting.";
		}
	private:
		static Object1 _myobject1;
}
in object2.cc
#include "object2.h"
Object1 Object2::_myobject1;

Now let's say that object2.o is part of library called libobjects[.a or
.so]

*When I link my main program with libojects.so (shared lib), then I can
see "Object1 is living" before entering main. It shows that _myobject1
has been initialized.

*When I link my main program with libobject.a (static lib), then the
behaviour is a bit different:
	- if the program references an Object2 class, then _myobject1 is
correctly initialized.
	- if the program does not reference any symbol related to Object2, then
_myobject1 is not initialized, I can't seen "Object1 is living."

This is very problematic for me. Some other C++ compilers offer ways to
workaround this, but I did not find anything for g++.
I tried to change static to "const static", tried -keep-const-static
without effect.

Thank you very much in advance for your help.



-- 
Simon MORLAT <simon_morlat@hp.com>



More information about the Gcc-help mailing list