Bigger executable size compared to clang

Why, when I compile a pure C++ project with zig build (setting zig_lib_dir=null in build options), is the executable bigger than just compiling with clang?

Might be because of static linking. What does the output of ldd say for both binaries?

1 Like

Also, Zig has different defaults than Clang/GCC, (they always compile with nothing unless asked to, whereas Zig, will always compile by default in debug, which means with debug symbol, and probably some flags/sanitizer too. It could also be static linking).

6 Likes

We always link libcxx and libcxxabi statically, correct. If you want to reduce the size, you could try setting exe.link_gc_sections = true. This is the default for release modes, so it will only potentially have any effect on debug mode. Additionally, you can strip debug info with exe.strip = true. This one is the default only for release-small.

2 Likes

Is it possible to force dynamic linking to system libcxx and libcxxabi?

Not yet, no.

Personally, I never want a dynamically linked libc++ because it’s just one more thing that can go wrong. Better yet if there is no libc++ dependency at all.

Fun fact, if you limit your C++ feature usage to only templates and some other basic stuff, you can actually write C++ code that has no libc++ dependency.

4 Likes

Agree, preferably fully static binary with direct syscalls.

Actually, one of dependencies called uWebSockets is header only (was a bit difficult to properly package in zig build system using “namedWriteFiles” through).

Comparing to Meson build, there is small performance degradation (~18%) in “–release=fast” mode, trying to figure out possible reasons.