I found an example with an interesting use case. While Ross makes a good point about DLLs, here's how you can use the -u option.
a.cpp :-
class A {
public:
static int init() {
Factory::getInstance()->addObject(new A());
return 0;
}
};
int linker_a = A::init();
Factory.cpp :-
class Factory {
public:
Factory* getInstance() { return _instance; }
void addObject(void* obj) { objects_.push_back(obj); }
private:
vector<void*> objects_;
static Factory* _instance;
};
main.cpp :-
#include "Factory.h"
int main() {
}
Now when we link, we can choose whether the A object get added to the factory or not based on whether we pass the -u linker_a to the command line of ld. If we pass it on the command line, an instance of A will get added to the factory otherwise it won't.
This allows development of main.cpp and Factory.{cpp,h} to be independent of A.{cpp,h} (i.e. Factory.cpp does not have to include A.h in order for an instance of A to be added to it's list of objects).
So the linking of additional modules ("A") is triggered by the linker flag -u.
Very neat feature!
This option is equivalent to the EXTERN linker script command.
, but the information on that command is the same but with a note that it's equivalent to-u
. – Killian