Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do clang++ and gcc/g++ produce differently linked executables

When compiling and linking with g++ and clang++ there is an expectation that the resulting executable will be linked equal with respect to their linkage.

However in certain cases it appears that clang++ executables are linked to more libraries than g++ despite the same arguments passed in.

This can cause functional differences especially with respect to dynamically linked libraries that have automatic execution on load and unload.

Why does this happen?

like image 357
Catskul Avatar asked Mar 05 '23 00:03

Catskul


1 Answers

It turns out gcc has a feature enabled by default in ubuntu --as-needed which causes gcc to effectively ignore any library for which none of it's symbols are referenced in the chain of linked items

This can be disabled in gcc with --no-as-needed flag. Or alternatively you can issue -Wl,--as-needed as the first option to clang++ to behave the way gcc does.

like image 84
Catskul Avatar answered Mar 15 '23 10:03

Catskul