2

It's just a piece of "hello world" C++ code.

#include <stdio.h>   
int main(int argc, char* argv[])
{
    printf("hello");
    return 0;
}

I compiled the code with clang 3.9. The command is: clang -m32 hello.cpp -o hello.exe, and the target executable size is about 44kb. It didn't make any difference with "O3" option added to the command.

While I compiled it with visual studio 2010, the size of the target executable file is only 6kb for release version and 28kb for debug version.

Could anyone help to explain the difference between the two versions' binary. Any help would be appreciated.

user3113626
  • 649
  • 8
  • 17
  • 1
    My crystal ball says that Clang is defaulting to statically linking the runtime libraries, whereas MSVC defaults to dynamically linking them. This means that the MSVC-generated binaries are smaller, but require an auxiliary runtime DLL. Check your compiler/linker settings to verify that this is indeed the case. – Cody Gray - on strike Dec 05 '16 at 13:52
  • You can also try `-Oz` flag for Clang. – arrowd Dec 05 '16 at 14:03
  • 1
    @CodyGray Thanks. Does your crystal ball know how to link the runtime libraries dynamically. I've googled without a hint. – user3113626 Dec 06 '16 at 01:33
  • 1
    @arrowd -Oz flag doesn't change anything. Thanks any way. – user3113626 Dec 06 '16 at 01:34

1 Answers1

0

I use clang-cl that accepts the flag /MD instead of clang. Problem resolved. I still need to explore what's the difference between clang and clang-cl.

user3113626
  • 649
  • 8
  • 17
  • No idea what the native Clang flag is for dynamic linking. clang-cl is a frontend/wrapper for the Clang compiler that presents an interface compatible with MSVC's compiler, cl.exe. You're perfectly safe doing it the way you described. – Cody Gray - on strike Dec 06 '16 at 06:17