EDIT: I know about include guards, but include files are not the issue here. I’m talking about actual compiled and already linked code that gets baked into the static library.
I’m creating a general-purpose utility library for myself in C++.
One of the functions I’m creating, printFile, requires string, cout and other such members of the standard library.
I’m worried that when the library is compiled, and then linked to another project that also uses string and cout, the code for string and cout will be duplicated: it will both be prelinked in the library binary the program is being linked with, and it will be again linked with the project that uses them itself.
The library is structured like this:
- There is one
libname.hppfile the programmer who uses the library is supposed to#includein his projects. - For every function
fnamedeclared inlibname.hpp, there is an filefname.cppimplementing it. - All
fname.cppfiles also#include "libname.hpp". - The library itself compiles into
libname.awhich is copied to/usr/lib/.
Will this even happen?
If yes, is it a problem at all?
If yes, then how can I avoid this?
Don’t worry: no modern compilation system will do that. The code for template functions is emitted into object files, but the linker discards duplicate entries.