Comparing C++ vs C executable file sizes.
Carlos.Guerreiro@nokia.com
Carlos.Guerreiro@nokia.com
Thu Oct 11 00:19:00 GMT 2001
Hi,
I'm comparing C++ vs. C executable sizes when compiling C-only source code.
I'm using arm-linux-gcc to cross-compile to the arm-linux target:
- binutils-2.11.2
- gcc-2.95.3
- glibc-2.2.4
- glibc-linuxthreads-2.2.4
- linux-2.4.9 with patch-2.4.9-ac9 and patch-2.4.9-ac9-rmk1
I have a set of 9 .c source files. Let's call them s_1.c, s_2.c, ..., s_n.c
These containing .c only (no C++) source code. They use some system
functions and some standard c library functions.
I'm compiling 2 sets of 9 executables. Each executable contains one more
source file than the preceding one:
- c_n (where 1 <= n <= 9) is the result of compiling s_1.c to s_n.c as C
source files:
c_1 = s_1.c
c_2 = s_1.c + s_2.c
...
c_n = s_1.c + s_2.c + ... + s_n.c
- cpp_n (where 1 <= n <= 9) is the result of compiling s_1.c to s-n.c as C++
source files.
cpp_1 = s_1.cc
cpp_2 = s_1.cc + s_2.cc
...
cpp_n = s_1.cc + s_2.cc + ... + s_n.cc
s_n.cc is identical to s_n.c
If I compile with the default options, I find the C++ executables are a lot
larger than the C executables, although the source-code is exactly the same.
My goal is to get the C++ executable sizes as close as possible to the C
executable sizes by minimizing both C a C++ executables.
The closest I have managed to get is this:
Test C C++ Increase Ratio
1 1408 1408 0 1
2 1860 1876 16 1.008602151
3 2280 2344 64 1.028070175
4 9544 10788 1244 1.130343671
5 11068 12116 1048 1.094687387
6 12120 13268 1148 1.094719472
7 12572 13756 1184 1.094177537
8 12924 14168 1244 1.096255029
9 14004 15280 1276 1.091116824
It's a lot better than what I had when I started but it's still a little
over 9% more for C++ than for C.
I'm compiling with:
-Os -fomit-frame-pointer -fno-default-inline -fno-exceptions -fno-rtti
linking with:
-nostdlib -lc (I have to add libgcc.a manually, for some reason -lgcc is not
working for me).
and then I strip with:
arm-linux-strip -R=.notes -R=.comment
What else can I do? Should it be possible to achieve a 1:1 ratio for a
non-trivial program? Perhaps asymptotically as code size grows?
Carlos Guerreiro
More information about the Gcc-help
mailing list