13

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?

Hawk
  • 361

5 Answers5

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.

glampert
  • 1,141
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.

Nikko
  • 652
6

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

Niall
  • 1,851
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.

DeadMG
  • 36,914
-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.