Saturday, April 17, 2010

iPhone dev; $non_lazy_ptr linker error

There are 3 causes that I know of for a $non_lazy_ptr linker error;

1) You forgot to instantiate your template functions and they are begin used from other files
2) You forgot to add the body of a virtual function (or you did and messed up its name space)
3) You forgot to instantiate the class variable.

If you forget to instantiate your templates so that another file can
use them. Then the iPhone version of gcc's linker is going to spit a
$non_lazy_ptr error for it.

For example if you code is a generic kind of setter function:

template <typename type>
void GenericClass::set(type _val)
{
  ((DataClass<type>*)holders[column])->set(_val);
  dirty = true;
}

Then the fix(using explicit instantiation) is to add the missing template instantiations to the file end of the file that defines the
template for each type of template you will end up using.

template void GenericRecord::setAssigns(int _val);
template void GenericRecord::setAssigns(bool _val);

Check here for more details on it;
http://gcc.gnu.org/onlinedocs/gcc/Template-Instantiation.html

For Missing virtual function bodies
class  GenericClass{ 
 virtual void set(int _val):
};

void set(int _val)
{
  ((DataClass<type>*)holders[column])->set(_val);
  dirty = true;
}

Then your fix is to add the missing namespace
void GenericClass::set(int _val)
{
  val = _val;
  dirty = true;
}

If you problem is a missing class variable instantiation, Then it might look something like this;

class  GenericClass{ 
 static int _val;
};

then your fix is this;
class  GenericClass{ 
 static int _val;
};

int GenericClass::_val;

Also note that this same error spat out when you miss instantiation of
a virtual functions body (but it refers to the vtable) and static
class variables.

No comments:

Post a Comment