Forum Discussion
Altera_Forum
Honored Contributor
20 years agolinker question
hello, In my project, some global variables are placed in a library. These variables are used in a class that also has a global instance in that library. The constructor of that class places po...
Altera_Forum
Honored Contributor
20 years agoHi Stefaan,
I think I understand now ... In your example, is the FileSystem object ever referenced by your application? If not, it won't get pulled in -- it's just another library that is not needed for unresolved references. If you structure your FileSystem class (object) as a Singleton you can make sure it gets reference and linked in. The Singleton pattern will also allow you to dynamically allocate the FileSystem object. Something like: <div class='quotetop'>QUOTE </div> --- Quote Start --- class FileSystem { public: static FileSystem *Instance (); protected: FileSystem (); private: static FileSystem* _inst; }; /* Implementation */ FileSystem *FileSystem::_inst = NULL; FileSystem *FileSystem::Instance () { if (_inst ==NULL) _inst = new FileSystem; return _inst; }[/b] --- Quote End --- Then anywhere in your application code you can just grab a reference: FileSystem *fs = FileSystem::Instance(); fs->open (...); As long as you reference the Instance operation somewhere, it'll get pulled in. This also avoids any potential issues associated with static construction. From a linker point-of-view, it's really no different than dummy++, but from a style point-of-view, C++ programmers will see a familiar design pattern. Hope this helps ... and I hope I didn't completely miss the boat on this one ;-) Regards, --Scott