Clang does not come with a linker, it relies on ld instead. And ld depends on libgcc.a and/or libgcc.so on your system (regardless this is the LLVM linker ld.lld or GNU ld). This is the reason why you have this error message.

So the answer is actually:

(a) the linker requires libgcc to do its own linking work

A lot more details on this are available here at omniprog.info:

If we want to get rid of GCC and use clang as our default compiler on the system, we may have to make some adjustments on some RPM-based systems. Clang does not provide a linker, but relies on the system's linker, typically ld, to link executables. This is the case even on FreeBSD and Mac OS X systems where Clang is the default compiler. We can see this using the -v option to clang++. Now, ld won't work without the following files:

libgcc.a
libgcc_s.so
[...]

Answer from Shlublu on Stack Overflow
🌐
GNU
gcc.gnu.org › onlinedocs › gccint › Libgcc.html
Libgcc (GNU Compiler Collection (GCC) Internals)
GCC provides a low-level runtime library, libgcc.a or libgcc_s.so.1 on some platforms.
🌐
OSDev Wiki
wiki.osdev.org › Libgcc
Libgcc - OSDev Wiki
You need to invoke the all-target-libgcc and install-target-libgcc make targets while building your GCC Cross-Compiler to build and install libgcc along with your cross-compiler. A static library called libgcc.a is installed into your compiler prefix along with the other compiler-specific files.
🌐
Haw-hamburg
users.informatik.haw-hamburg.de › ~krabat › FH-Labor › gnupro › 2_GNUPro_Compiler_Tools › Using_GNU_CC › gcclibgcca_and_CrossCompilers.html
libgcc.a and Cross-Compilers
Code compiled by GNU CC uses certain runtime support functions implicitly. Some of these functions can be compiled successfully with GNU CC itself, but a few cannot be. These problem functions are in the source file, ‘libgcc1.c’; the library made from them is called ‘libgccl.a’ · When ...
🌐
GNU
gcc.gnu.org › gcc-3.0 › libgcc.html
libgcc - GNU Project
February 20, 2023 - This page provides a summary of discussions about the pros and cons of distributing libgcc as a shared library, as well as a static library.
🌐
GNU
gcc.gnu.org › onlinedocs › gcc-8.3.0 › gccint › Libgcc.html
GNU Compiler Collection (GCC) Internals: Libgcc
GCC provides a low-level runtime library, libgcc.a or libgcc_s.so.1 on some platforms.
Find elsewhere
🌐
Vu
bio.vu.nl › thb › links › docs › libstdc++ › gccrebuild.html
How to automatically rebuild libgcc.a.
Easy as pi, er, pie. Just pass the pathname to the --enable switch (absolute pathnames are best), and build libstdc++ as you normally would. When it is finished, 'make' will go over to GCCobjdir and build a new libgcc.a for you.
🌐
Anaconda.org
anaconda.org › anaconda › libgcc
libgcc - anaconda | Anaconda.org
anaconda/libgcc · Community · The GCC low-level runtime library · Overview · Files 7 · Labels 1 · Badges · Versions · 15.2.0 · To install this package, run one of the following: $conda install anaconda::libgcc · Monthly · Downloads Updates · Version · 15.2.0 ·
Top answer
1 of 1
28

Yes, you do need it .... probably. If you don't need it then statically linking it is harmless. You can tell if you need it by using the -t link trace option (I think).

There are various things that you cant do in one instruction (typically things like 64-bit operations on 32-bit architectures). These things can be done, but if they use a non-trivial number of instructions then it's more space-efficient to have them all in one place.

When you disable optimization using -O0 (that's actually the default anyway) then GCC pretty much always uses the libgcc routines.

When you enable speed optimization then GCC may choose to insert the instruction sequence directly into the code (if it knows how). You may find that it ends up using none of the libgcc versions - it will certainly use fewer libgcc calls.

When you enable size optimizations then GCC may prefer the function call, or may not - it depends on what the GCC developers think is the best speed/size trade-off in each case. Note that even when you tell it to optimize for speed, the compiler may judge that some functions are unlikely to be used, and optimize those for size - even more so if you use PGO.

Basically, you can think of it in the same way as memcpy or the math-library functions: the compiler will inline functions it judges to be beneficial, and call library functions otherwise. The compiler can "inline" standard functions and libgcc function without looking at the library definition, of course - it just "knows" what they do.

Whether to use static or dynamic libgcc is an interesting trade-off. On the one hand, a dynamic (shared) library will use less memory across your whole system, and is more likely to be cached, etc. On the other hand, a static libgcc has a lower call overhead.

The most important thing though is compatibility. Obviously the libgcc library has to be present for your program to run, but it also has to be a compatible version. You're ok on a Linux distro with a stable GCC version, but otherwise static linking is safer.

I hope that answers your questions.

🌐
GNU
gcc.gnu.org › onlinedocs › gcc › Link-Options.html
Link Options (Using the GNU Compiler Collection (GCC))
One of the standard libraries bypassed by -nostdlib and -nodefaultlibs is libgcc.a, a library of internal subroutines which GCC uses to overcome shortcomings of particular machines, or special needs for some languages. (See Interfacing to GCC Output in GNU Compiler Collection (GCC) Internals, ...
🌐
GitHub
github.com › gcc-mirror › gcc › tree › master › libgcc
gcc/libgcc at master · gcc-mirror/gcc
libgcc2.h · libgcov-driver-system.c · libgcov-driver-system.c · libgcov-driver.c · libgcov-driver.c · libgcov-interface.c · libgcov-interface.c · libgcov-merge.c · libgcov-merge.c · libgcov-profiler.c · libgcov-profiler.c · libgcov-util.c · libgcov-util.c · libgcov.h · libgcov.h · memcmp.c · memcmp.c · memcpy.c · memcpy.c · memmove.c · memmove.c · memset.c · memset.c · mkheader.sh · mkheader.sh · mkmap-flat.awk ·
Author   gcc-mirror
🌐
Glandium
glandium.org › blog
glandium.org » Blog Archive » libgcc.a symbol visibility considered harmful
March 6, 2012 - GCC will emit a call to __aeabi_idivmod for the % operation. With GCC 4.6.3, this function is in _divsi3.o under libgcc.a.
🌐
GitHub
github.com › OP-TEE › optee_os › issues › 4673
cannot find libgcc.a · Issue #4673 · OP-TEE/optee_os
June 7, 2021 - Getting the following error when trying to build an external TA in Yocto: arm-XXX-linux-gnueabi-ld.bfd: cannot find libgcc.a: No such file or directory The error occur when trying to compile the TA part. The Makefile and sub.mk are almos...
Author   OP-TEE
🌐
Hewlett Packard Enterprise Community
community.hpe.com › t5 › operating-system-hp-ux › how-to-include-libgcc-a-in-the-makefile › td-p › 3235977
how to include libgcc.a in the makefile - Hewlett Packard Enterprise Community
March 31, 2004 - If it ain't broke, I can fix that. ... IMHO you copy libgcc.a into /usr/local/lib and then do ./configure and then make; this has generally resolved my problems on linux OS's; seems to be true for HP_UX as well but have not tried what you are attempting.
🌐
Narkive
gcc.gcc.gnu.narkive.com › 1pzMObWC › why-are-lib-a-and-lib-eh-a-compiled-with-fvisibility-hidden
Why are libgcc.a and libgcc_eh.a compiled with -fvisibility=hidden?
That change just moves the libgcc build out of gcc over to libgcc as a top-level directory. It didn't introduce the visibility hiding. I think that dates back to revision 50063. 2002-02-26 Jakub Jelinek <***@redhat.com> * configure.in (libgcc_visibility): Substitute.