11

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?

22
  • 7
    Sort of explains why C is used a lot on embedded devices. Jun 26, 2014 at 16:07
  • 2
    Also, it would be a fairer test if you were to write the same exact code in both languages. Even then, I'm not sure that would rid you of the difference, since C++ is much more complicated to compile (and thus would be done differently). Jun 26, 2014 at 16:07
  • 3
    as far as I can tell, this has been asked and answered at SO: Why is a C/C++ “Hello World” in the kilobytes? @RobertHarvey FWIW Java ME CLDC 1.0 footprint is per my recollection 128K, not too much even for end '90s embedded devices
    – gnat
    Jun 26, 2014 at 16:12
  • 6
    There is no question here. Furthermore, even if there was, that question would be unanswerable without specific knowledge about compiler and compilation flags used. The only way to get a precise answer to a question why C++ executable would be so much bigger and what exactly takes up the bytes would be to use some kind of a binary analysis tool (e.g. readelf on Unix systems, part of GNU binutils) or a hex editor to investigate file sections and contents. What comes to languages themselves, there's absolutely no reason for a difference like that. C++ output could be just as small as C.
    – zxcdw
    Jun 26, 2014 at 18:30
  • 3
    Comparing "Hello World" programs is almost entirely meaningless as no one optimizes compilers for trivially small programs. It is very likely that your C++ compiler has flags that, if you switch to printf (available in C++) will produce a file of similar size to C (for example, -nostdlib and -nodefaultlibs. Jun 26, 2014 at 22:37

5 Answers 5

24

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.

4
  • 1
    Doesn't really explain a 1.33MB file size, though. Jun 26, 2014 at 21:06
  • 1
    He must be using Visual Studio :P
    – glampert
    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 space
    – Hawk
    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
    Sep 13, 2019 at 14:03
8

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.

5

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

2

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.

-1

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.