I'm trying to understand why the output file sizes are significantly different when using a C and a C++ compiler.
I was writing a small hello world program in C and C++, I noticed that in C version, the size of the executable was 93.7KB and in C++, the size of the same hello world program was 1.33MB. I am not sure why that is. I think it may be because C++ has more libraries and namespaces to use so I removed the using namespace std
line and simply used std::cout
and still the result was the same.
C
#include <stdio.h>
int main()
{
printf("hello world");
return 0;
}
// size 93.7KB
C++
#include <iostream>
int main()
{
std::cout<<"Hello world";
return 0;
}
// size 1.33MB
There doesn't seem to be much difference in the code above. Is there some sort of compiler difference that creates the differing file sizes?
5 Answers 5
Most of the C++ standard library, including all the streams which cout
is part of, are inline template classes. Whenever you #include
one of these inline library components, the compiler will copy and paste all that code to the source file that is including it. This will help the code to run faster, but will also add a lot of bytes to the final executable. This is likely the reason for the results you got.
Doing a similar test with the clang compiler on OSX (Apple LLVM version 5.1), using default flags, I got comparable results:
hello_cpp_cout:
#include <iostream>
int main()
{
std::cout << "Hello world" << std::endl;
return 0;
}
Size: 14,924 bytes
hello_c:
#include <stdio.h>
int main()
{
printf("hello world\n");
return 0;
}
Size: 8,456 bytes
And, as a bonus, I tried to compile a .cpp
file with the exact same code as hello_c
, i.e.: using printf
instead of cout
:
hello_cpp_printf:
#include <stdio.h>
int main()
{
printf("hello world\n");
return 0;
}
Size: 8,464 bytes
As you can see, the executable size is hardly related to the language, but to the libraries you include in your project.
Update:
As it was noted by several comments and other replies, the choice of compiler flags will also be influential in the size of the compiled executable. A program compiled with debug flags will be a lot larger than one compiled with release flags, for example.
-
1Doesn't really explain a 1.33MB file size, though.Robert Harvey– Robert Harvey2014年06月26日 21:06:13 +00:00Commented Jun 26, 2014 at 21:06
-
1He must be using Visual Studio :Pglampert– glampert2014年06月27日 01:17:42 +00:00Commented Jun 27, 2014 at 1:17
-
thanks, dude..i'm using dev c++ and it did work.. you're right, the libraries indeed do give too much spaceHawk– Hawk2014年07月02日 07:59:30 +00:00Commented Jul 2, 2014 at 7:59
-
I compiled these examples with VS2015 x64 and the results support this answer. Release: hello_cpp_cout 12288 Bytes, hello_c 10240 Bytes, hello_cpp_printf 10240 Bytes. Debug: hello_cpp_cout 56320 Bytes, hello_c 51200 Bytes, hello_cpp_printf 51200 Bytes.pschill– pschill2019年09月13日 14:03:15 +00:00Commented Sep 13, 2019 at 14:03
There doesn't seem to be much difference in the code above.
Yes there is. It's a totally different code. The c++ iostream library relies heavily on template which creates more inline code and so the C++ executable is bigger.
Another reason is that you did not remove debug sympols from the executable files, and for C++ the symbols are quite verbose. If you are under linux, you can use the "strip" command to remove those symbols and the executable size will shrink.
The difference in executable sizes will be heavily dependent on what type of linkage is specified, optimisations and the compiler(s) being used.
Given the significant difference in final sizes, it looks like the C++ variant is being statically linked with the runtime. Remove or change that and you should see a drop in the C++ size; for gcc (g++) look for --static*
et. al and for msvc /MD
and /MT
There is actually quite a lot of difference. The C code uses unformatted, unlocalized output. The C++ equivalent has quite a lot of gunk in it for changing locales and that kind of thing. Functionally, they are far from equivalent. You just happen to be using only a tiny subset of the C++ interface.
However more generally, the larger code sizes are a defect of this particular portion of the Standard library in particular, which is well known to be over-engineered, slow, and large, rather than the C++ language in general.
With CMake MinSizeRel BUILD_TYPE cpp is 8744, while c is 8296 bytes. Not that much difference also if you are in an embedded environment.
printf
(available in C++) will produce a file of similar size to C (for example,-nostdlib
and-nodefaultlibs
.