Why does clang still need libgcc.a to compile my code?

You must compile with the option -nodefaultlib or -nostdlibs.

Here a quote from GCC documentation (clang interface is just the same):

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.

You may have to execute c++ static initialization routines and/or use what is provided by the object files crt<x>.o in the lib directory. These files are part of libc and provides executable entry point.


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
[...]