Sorry if I am duplicating other question but I don't know how to google it. I want to add some minor modularity to my program: some .cpp files should be compiled as "modules". The main requirement is that I should be able to add modules by just adding new .cpp file to the project, without changing other files in any way.
Achieving this looks easy with dynamic loading libraries. Main program can scan some folder for all .dll files, load each of them and call exported "load" symbol from every module. In destructor main program can call "unload" symbols so modules can clean up.
I want the same but with monolithic program. Is there any way for .cpp files to register themselves so main program can call their init() functions at some point? Or for main program to find all such modules?
How it is done in Linux kernel? I know simple adding .c files makes them work somehow.
You can add new
.cpp
files to a statically linked application without changing the existing code using a registration interface that is exposed from the main application (singleton).Something like
App.h:
NewModule.cpp: