I have many cpp files, some of which have functions to self-subscribe to events. Unfortunately, most linkers will eliminate all symbols from compilation units if there is no apparent function call into them. Is there a way to force linking of these subscription functions? I do not want to disable dead code stripping entirely, because I might miss a lot of opportunities from the other translation units.
Subscriber.cpp:
Event &someEvent();
void doSomething()
{
printf("doing something\n");
}
class Initializer
{
public: Initializer()
{
// I need this function to be kept
someEvent().subscribe(&doSomething);
}
} initializer;
Main.cpp:
Event &someEvent();
int main()
{
someEvent().dispatch();
}
Thanks
EDIT:
Here is a repro build: https://github.com/malytomas/deadCodeElimination (Thanks Ayjay for help, even if his/her example does not reproduce the problem.)
The problem happens only with libraries. (Thanks Employed Russian for bringing this up.)