0

I am working on a simple C++ reflection system to use in my own library. I went for a manual registration by using static member per each class that has side effects in the constructor - it actually registers the class in the system.

Everything seemed to be fine until I found out that some of the types are not properly registered. After research I came to conclusion that this is because linker is refusing to link members from a translation unit that is not referenced (I am building a static library) anywhere in build executable.

My question is the following, are there any tricks/hacks, even platform specific ones, that would force linker to link to selected translation units.

Or even better, is there any solid alternative for described method of self registering classes.

Adrian Lis
  • 647
  • 4
  • 20
  • Are you sure that it's not just the class you're registering to that's initialized after some types are already initialized/registered? – Joachim Isaksson Jul 28 '13 at 14:45
  • Yes I am sure that this is not the case. I am not registering to an object of some class, It is plain array that stores pointers to such static instances. When I add reference to this particular class in executables source code then it properly initializes the field. What I am after is a way to force linkage to this translation unit even if code from the executable project is not referencing it. – Adrian Lis Jul 28 '13 at 15:46
  • If I understand correctly, you're relying on global constructor initialization to register all your objects in a central registry? That is, the objects push themselves in, rather than external explicit references pulling the objects in. And I guess tacit in all this is that you don't want to write a "registry file" that collects all these global objects in one compilation unit, so they all get linked as a unit. Is that correct? Because if you can do that, I think that'll solve it. – Joe Z Jul 28 '13 at 16:28
  • Yes this is what I do, the problem is when the library is built as a static one, and the user of the library (some executable linking to it) is not referencing to some of the classes, so some of the cpp files are not linked against, and the constructors are not triggered. – Adrian Lis Jul 28 '13 at 16:58

0 Answers0