How to avoid including the same code when using C++ libraries?

c++, include, linker, static-libraries

Solution

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

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.

Problem

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.hpp` file the programmer who uses the library is supposed to `#include` in his projects. - For every function `fname` declared in `libname.hpp`, there is an file `fname.cpp` implementing it. - All `fname.cpp` files also `#include "libname.hpp"`. - The library itself compiles into `libname.a` which 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?

Original source