Library sizes for C vs C++ in an embedded Linux system

Posted by Cliff Brake on 2013-12-18 | 2 Comments to Read

Is the size of the libraries required for C++ (vs C) a concern in Embedded Linux systems?  Most Embedded Linux systems likely include some C++ code, so this is probably not even a decision we need to make in many cases.  However, often there is a need for a small initramfs that is used as part of the boot process (perhaps for software updates) before switching to the main root file system.  In this case, it makes sense to keep the initramfs as small as possible, and we might be concerned here with the size of C++ libraries.

To find out what additional libraries are required when compiling with a C++ compiler vs C, a simple hello-world app was created.  It was then compiled with gcc and g++.  The outputs were then parsed with readelf to determine the required dynamic libraries.  The results are shown below:

C:

0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]

C++:

0x0000000000000001 (NEEDED)             Shared library: [libstdc++.so.6]
0x0000000000000001 (NEEDED)             Shared library: [libm.so.6]
0x0000000000000001 (NEEDED)             Shared library: [libgcc_s.so.1]
0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]

The sizes of packages for these libraries in an OpenEmbedded build are:

  • libc: 1.4KiB (libm is inlcuded with libc)
  • libstdc++6: 256KiB
  • libgcc1: 204KiB

(The above sizes are compressed, so the actually will be some larger, but are useful for comparison.)

The installed sizes of libstdc++6 and libgcc1 on a ext3 file system are 1.4MiB and 750KiB.

It may also take slightly longer to load a C++ application as there is time required to dynamically load and link additional libraries.  Is an extra 2MiB of libraries a big deal?  It depends on your situation, but should be considered any time you are trying to create a small image.

  • kungjohan said,

    I guess if you want to make a small image you link the application statically to make the linker remove uneeded functions. With high probability you only use a small subset of the functions in the C++ library.

  • Cliff Brake said,

    Good point — even eglibc includes quite a few libs and many functions that may not be required.