Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:

  1. There is one libname.hpp file the programmer who uses the library is supposed to #include in his projects.
  2. For every function fname declared in libname.hpp, there is an file fname.cpp implementing it.
  3. All fname.cpp files also #include "libname.hpp".
  4. 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?

like image 817
corazza Avatar asked Dec 31 '12 01:12

corazza


People also ask

Can you use C without libraries?

The C standard library is essential when writing C programs, or almost anything in any language for that matter. But can you do without? Sure! The standard library is just code, and like any code can be rewritten.

Can C++ use all C libraries?

Yes - C++ can use C libraries.

Can you use libraries in C?

Functions in a C library can be used and accessed by programmers to create several different programs. As a programmer, you may find yourself using the same function or functions repeatedly. In this case, it is best to put this function or functions in a library to speed up the compilation of the program.


2 Answers

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.

like image 64
Employed Russian Avatar answered Oct 20 '22 04:10

Employed Russian


The library definitions of the standard C++ library won't show up in your own statically library unless you explicitly include them there (i.e., you extract object files from the standard C++ library and include them into your library). Static libraries are not linked at all and will just have undefined references to other libraries. A static library is merely a collection of object files defining the symbols provided by the library. The definitions which come from the headers, e.g., inline functions and template instantiations, will be defined in such a way that multiple definitions in multiple translation units won't conflict. Where the code isn't actually inlined, it will define "weak" symbols which result in duplicates being ignored or removed at link time.

The only real concern is that the libraries linked into an executable need to use compatible library definitions. With substantial amount of code residing in header files, there are relatively frequent changes to the C++ header files, including standard C++ library headers (relative to the C library headers which contain a lot less code).

like image 39
Dietmar Kühl Avatar answered Oct 20 '22 04:10

Dietmar Kühl