Disclaimer: I am fairly new to Ubuntu. I have a reasonable amount of experience with RHEL and SLES, and have had to learn Ubuntu LTS versions recently because of some ARM64 hardware that doesn't want to boot anything else.

Summary

Ubuntu releases do, indeed, often come with some components from a later GCC than the releases' default GCC. They apparently do this so that they can provide later compiler versions, if you want to use them.

  • The gcc-10-base package just provides documentation.
  • The libgcc-s1 package provides an important library, libgcc_s.so.1. That provides helper functions for code generated by the compiler: I know it as important for handling C++ exceptions being thrown through a C call stack.
  • Another important library is libstdc++.so.6. That provides C++ support functions.
  • Glibc (libc.so.6, libm.so.6 and other libraries) is also important, but is not tied to a GCC version.

All of these libraries have very strong compatibility rules. Essentially, a later version of the library will always be compatible with earlier versions, discounting some ancient versions from the early history of GCC and Linux.

It isn’t obvious at first why Ubuntu and Debian provide run-times that are later than the compiler, but it gets clearer when you look at the range of GCC versions available on recent Ubuntu LTS versions:

Distribution Released Debian GCC run-times Default GCC Additional GCCs
Ubuntu 16.04 Apr 2016 9.x 5.x 5.4 4.7, 4.8, 4.9
Ubuntu 18.04 Apr 2018 10.x 8.x 7.5 4.8, 5.5, 6.5. 8.4
Ubuntu 20.04 Apr 2020 11.x 9.x & 10.x 9.3 7.4, 8.4, 10.3
Ubuntu 22.04 Apr 2022 12.x 12.x 11.4 9.5, 10.5, 12.3

At that that point, it becomes reasonably obvious. Ubuntu 20.04 has the run-times for code compiled with GCC 9.x and 10.x (GCC 10.x doesn't demand any additional library functions that GCC 9.x didn't use). You can install any mixture of GCC 7.4, 8.4 and 10.3 as extra compilers, and they'll all work. The run-time libraries on Ubuntu 20.04 will support code compiled with any of those compilers.

Why not ship GCC 10.3 with Ubuntu 20.04? Run-time libraries are generally more stable than compilers. GCC 10 would have been first released (as 10.1) about the time that 20.04 was being put together. Building an LTS release with a brand-new compiler would be foolhardy; shipping new run-time libraries, after testing them with code built with GCC 9, is a lot safer and allows GCC 10 to be added when it has stabilised.

Canonical don't provide GCC 11 for 20.04 because it doesn't have the necessary run-times. For those, you need a later Ubuntu.

How to find all this stuff out

/usr/share/doc/gcc/README.Debian has some information.

dpkg-query --listfiles gcc-10-base shows us that gcc-10-base only provides documentation.

dpkg-query --listfiles libgcc-s1 shows us that libgcc-s1 provides /lib/x86_64-linux-gnu/libgcc_s.so.1, which is one of the basic run-time libraries for GCC.

The other basic run-time libraries for C/C++ are glibc, which is independent of GCC, and libstdc++. dpkg-query -list | grep libstdc shows us two packages:

ii libstdc++-9-dev:amd64 9.3.0-17ubuntu1~20.04 amd64 GNU Standard C++ Library v3 ...
ii libstdc++6:amd64 10.3.0-1ubuntu1~20.04 amd64 GNU Standard C++ Library v3

libstdc++6 is the GCC 10.3 version; the -dev package is the GCC 9.3 version.

dpkg-query --listfiles libstdc++-9-dev shows us that this package provides header files, archive libraries and documentation for developing in C++ with GCC 9.

dpkg-query --listfiles libstdc++6 shows us that this package provides documentation, some Python scripts and two really important files:

/usr/lib/x86_64-linux-gnu/libstdc++.so.6
/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28

The .so.6 file is what programs are linked against. It is actually a softlink to the .so.6.028 file. That’s the name of the GCC 10 version of libstdc++, the GCC support library for C++. You can get the mapping between those names and GCC versions here. Scroll down, and you’ll find some tables.

GCC used for building Ubuntu

The easiest thing to check is glibc. Building this with a different compiler from the rest of the OS would be crazy, and you can find out what compiler was used to build it just by asking:

/usr/lib/x86_64-linux-gnu/libc.so.6
GNU C Library (Ubuntu GLIBC 2.31-0ubuntu9.7) stable release version 2.31.
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
Compiled by GNU CC version 9.3.0.
libc ABIs: UNIQUE IFUNC ABSOLUTE
For bug reporting instructions, please see:
https://bugs.launchpad.net/ubuntu/+source/glibc/+bugs.

Answer from John Dallman on askubuntu.com
Top answer
1 of 1
3

Disclaimer: I am fairly new to Ubuntu. I have a reasonable amount of experience with RHEL and SLES, and have had to learn Ubuntu LTS versions recently because of some ARM64 hardware that doesn't want to boot anything else.

Summary

Ubuntu releases do, indeed, often come with some components from a later GCC than the releases' default GCC. They apparently do this so that they can provide later compiler versions, if you want to use them.

  • The gcc-10-base package just provides documentation.
  • The libgcc-s1 package provides an important library, libgcc_s.so.1. That provides helper functions for code generated by the compiler: I know it as important for handling C++ exceptions being thrown through a C call stack.
  • Another important library is libstdc++.so.6. That provides C++ support functions.
  • Glibc (libc.so.6, libm.so.6 and other libraries) is also important, but is not tied to a GCC version.

All of these libraries have very strong compatibility rules. Essentially, a later version of the library will always be compatible with earlier versions, discounting some ancient versions from the early history of GCC and Linux.

It isn’t obvious at first why Ubuntu and Debian provide run-times that are later than the compiler, but it gets clearer when you look at the range of GCC versions available on recent Ubuntu LTS versions:

Distribution Released Debian GCC run-times Default GCC Additional GCCs
Ubuntu 16.04 Apr 2016 9.x 5.x 5.4 4.7, 4.8, 4.9
Ubuntu 18.04 Apr 2018 10.x 8.x 7.5 4.8, 5.5, 6.5. 8.4
Ubuntu 20.04 Apr 2020 11.x 9.x & 10.x 9.3 7.4, 8.4, 10.3
Ubuntu 22.04 Apr 2022 12.x 12.x 11.4 9.5, 10.5, 12.3

At that that point, it becomes reasonably obvious. Ubuntu 20.04 has the run-times for code compiled with GCC 9.x and 10.x (GCC 10.x doesn't demand any additional library functions that GCC 9.x didn't use). You can install any mixture of GCC 7.4, 8.4 and 10.3 as extra compilers, and they'll all work. The run-time libraries on Ubuntu 20.04 will support code compiled with any of those compilers.

Why not ship GCC 10.3 with Ubuntu 20.04? Run-time libraries are generally more stable than compilers. GCC 10 would have been first released (as 10.1) about the time that 20.04 was being put together. Building an LTS release with a brand-new compiler would be foolhardy; shipping new run-time libraries, after testing them with code built with GCC 9, is a lot safer and allows GCC 10 to be added when it has stabilised.

Canonical don't provide GCC 11 for 20.04 because it doesn't have the necessary run-times. For those, you need a later Ubuntu.

How to find all this stuff out

/usr/share/doc/gcc/README.Debian has some information.

dpkg-query --listfiles gcc-10-base shows us that gcc-10-base only provides documentation.

dpkg-query --listfiles libgcc-s1 shows us that libgcc-s1 provides /lib/x86_64-linux-gnu/libgcc_s.so.1, which is one of the basic run-time libraries for GCC.

The other basic run-time libraries for C/C++ are glibc, which is independent of GCC, and libstdc++. dpkg-query -list | grep libstdc shows us two packages:

ii libstdc++-9-dev:amd64 9.3.0-17ubuntu1~20.04 amd64 GNU Standard C++ Library v3 ...
ii libstdc++6:amd64 10.3.0-1ubuntu1~20.04 amd64 GNU Standard C++ Library v3

libstdc++6 is the GCC 10.3 version; the -dev package is the GCC 9.3 version.

dpkg-query --listfiles libstdc++-9-dev shows us that this package provides header files, archive libraries and documentation for developing in C++ with GCC 9.

dpkg-query --listfiles libstdc++6 shows us that this package provides documentation, some Python scripts and two really important files:

/usr/lib/x86_64-linux-gnu/libstdc++.so.6
/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28

The .so.6 file is what programs are linked against. It is actually a softlink to the .so.6.028 file. That’s the name of the GCC 10 version of libstdc++, the GCC support library for C++. You can get the mapping between those names and GCC versions here. Scroll down, and you’ll find some tables.

GCC used for building Ubuntu

The easiest thing to check is glibc. Building this with a different compiler from the rest of the OS would be crazy, and you can find out what compiler was used to build it just by asking:

/usr/lib/x86_64-linux-gnu/libc.so.6
GNU C Library (Ubuntu GLIBC 2.31-0ubuntu9.7) stable release version 2.31.
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
Compiled by GNU CC version 9.3.0.
libc ABIs: UNIQUE IFUNC ABSOLUTE
For bug reporting instructions, please see:
https://bugs.launchpad.net/ubuntu/+source/glibc/+bugs.

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.

Discussions

linker - difference between -lgcc_s and gcc - Stack Overflow
In this case, the problem is usually solved by adding either "-l gcc" or "gcc -print-libgcc-file-name" to the linking flags (LDFLAGS). However, unlike my other regular platforms (i386, amd64, sparc64) here it wasn't enough. After a lot of head-banging (to be fair, it also comes from the music) ... More on stackoverflow.com
🌐 stackoverflow.com
Do I need to link with libgcc if I use my own compiler?
libgcc is not the same as the system libc. Libgcc will provide functions for GCC code generation. If your codegen also does GCC-like things, you may find it appropriate to use it. However, note that libgcc is dependent on things like GCC compiler revision, so it's really not portable. An example of a function in libgcc is __ashlsi3(), the documentation writes: "[This function returns] the result of shifting a left by b bits". Why would this be needed? Honestly, I don't know - I haven't read the GCC codegen source. But this is the kind of stuff that libgcc implements. GCC will always assume libgcc to be present, even if you do -nodefaultlibs during compilation, and may generate code which uses it. Unless your compiler also assumes the presence of libgcc for some reason, you have no reason to use it. Nobody will directly use libgcc functions in their own code, but GCC might insert them during codegen. More on reddit.com
🌐 r/C_Programming
12
7
March 29, 2024
What is the relationship between gcc , libstdc++ , glibc , binutils ?
These things are separate because turning a set of C or C++ source files into an executable is a multi-step process. gcc is the GNU Compiler Collection. It deals with turning source files into architecture-dependant assembly code. It's the first step in compilation, and is typically the only user-invoked bit of the process (gcc typically deals with calling the assembler and linker automatically) glibc is the GNU implementation of the Standard C library. The Standard C library is a set of C header files that provide core definitions and low-level functionality needed for non-trivial programs to operate. See here http://en.wikipedia.org/wiki/C_standard_library libstdc++ is the C++ standard library. It provides the same core definitions and low-level functionality as the Standard C library, plus a whole heap of other stuff too. (stuff that, if you'd done the program in C, would need additional external libraries linked in) See here http://en.wikipedia.org/wiki/C%2B%2B_standard_library binutils are programs for translating assembly language into object code, and various manipulation techniques for linking object files together. http://en.wikipedia.org/wiki/Binutils An example might be helpful here, so here goes. main.c int main() { hello(); bye(); } hello.c #include void hello() { printf ("Hello World!\n"); } bye.c #include void bye() { printf ("Goodbye World!\n"); } As you can see from the source files, we're including the input/output functions from the Standard C library. Calling gcc in the conventional way will just compile the thing in one go, without showing you any of the intermediate steps. gcc -o foo main.c hello.c bye.c You get an executable foo However, if you tell gcc not to do anything other than compile to assembly, you can see the intermediate steps. gcc -S -o main.S main.c gcc -S -o hello.S hello.c gcc -S -o bye.S bye.c Now you have 3 assembly files, translated from the C source code. View these in a text editor to see the assembly hello.S .file "hello.c" .section .rodata .LC0: .string "Hello World!" .text .globl hello .type hello, @function hello: .LFB0: .cfi_startproc pushq %rbp .cfi_def_cfa_offset 16 .cfi_offset 6, -16 movq %rsp, %rbp .cfi_def_cfa_register 6 movl $.LC0, %edi call puts popq %rbp .cfi_def_cfa 7, 8 ret .cfi_endproc .LFE0: .size hello, .-hello .ident "GCC: (Gentoo 4.8.2 p1.0, pie-0.5.8) 4.8.2" .section .note.GNU-stack,"",@progbits Then you can run the assembly through the assembler to get the 3 object files as -o main.o main.S as -o hello.o hello.S as -o bye.o bye.S These object files are binary, so not viewable by a text editor. Then you can link the 3 object files together into a single executable (ld is a pain to use directly, so I'm using it via gcc) gcc -o foo main.o hello.o bye.o Bingo, your foo executable. More on reddit.com
🌐 r/linuxquestions
9
19
December 22, 2013
gcc - Why does `libc6 ` depend on `libgcc1`? - Unix & Linux Stack Exchange
If I don't want gcc on my system, would it be safe to remove afterwords? ... Libgcc is even more fundamental than libc. More on unix.stackexchange.com
🌐 unix.stackexchange.com
October 30, 2014
🌐
GNU
gcc.gnu.org › onlinedocs › gccint › Libgcc.html
Libgcc (GNU Compiler Collection (GCC) Internals)
This includes integer multiply and divide on some machines, and all floating-point and fixed-point operations on other machines. libgcc also includes routines for exception handling, and a handful of miscellaneous operations. Some of these routines can be defined in mostly machine-independent C. Others must be hand-written in assembly language for each processor that needs them. GCC will also generate calls to C library routines, such as memcpy and memset, in some cases.
🌐
OSDev Wiki
wiki.osdev.org › Libgcc
Libgcc - OSDev Wiki
The GNU Compiler Collection uses ... on the particular target, configuration and even command line options. GCC unconditionally assumes it can safely emit calls to libgcc symbols as it sees fit, thus all code compiled by GCC must be linked with libgcc....
🌐
Quora
quora.com › What-is-the-difference-between-glibc-and-libgcc
What is the difference between glibc and libgcc? - Quora
Answer (1 of 3): To add what Prathik said, glibc or GNU C Library is the fundamental library on a linux-like machine that provides access to basic facilities such as opening files, allocating memory, creating threads. One of its main functions is to encapsulate "system calls" - allowing user mode...
🌐
Reddit
reddit.com › r/c_programming › do i need to link with libgcc if i use my own compiler?
r/C_Programming on Reddit: Do I need to link with libgcc if I use my own compiler?
March 29, 2024 -

I wrote a c-compiler from scratch and link it manually. I saw another c-compiler https://github.com/rui314/chibicc including libgcc for his linking. After some research about it it seems that it's just for gcc some compiler specific functions.

I tried linking without libgcc and it still worked fine so I don't know why chibicc links with it?

Is it safe to link without or are there some functions that people expect from a c compiler that are only in libgcc?

Top answer
1 of 5
7
libgcc is not the same as the system libc. Libgcc will provide functions for GCC code generation. If your codegen also does GCC-like things, you may find it appropriate to use it. However, note that libgcc is dependent on things like GCC compiler revision, so it's really not portable. An example of a function in libgcc is __ashlsi3(), the documentation writes: "[This function returns] the result of shifting a left by b bits". Why would this be needed? Honestly, I don't know - I haven't read the GCC codegen source. But this is the kind of stuff that libgcc implements. GCC will always assume libgcc to be present, even if you do -nodefaultlibs during compilation, and may generate code which uses it. Unless your compiler also assumes the presence of libgcc for some reason, you have no reason to use it. Nobody will directly use libgcc functions in their own code, but GCC might insert them during codegen.
2 of 5
2
Sometimes GCC generates calls into libgcc instead of generating code. For example: long long example(long long x) { return 3 / x; } If I compile this for x86-32: $ i686-linux-gnu-gcc -S -o - -O example.c | grep -vF . example: ; ... pushl 20(%esp) pushl 20(%esp) pushl $0 pushl $3 call __divdi3@PLT ; ... ret Where __divdi3 comes from libgcc. Some targets require more such help than others. It sounds like your compiler doesn't do this — as you wouldn't be asking otherwise — but if you statically link with GCC-compiled code then it may expect to link -lgcc implicitly.
🌐
Reddit
reddit.com › r/linuxquestions › what is the relationship between gcc , libstdc++ , glibc , binutils ?
r/linuxquestions on Reddit: What is the relationship between gcc , libstdc++ , glibc , binutils ?
December 22, 2013 -

what is exactly the relationship between those , and why they are seperate?

and if i was to upgrade them what is the hiearchy , should i upgrade gcc first then use that new gcc to compile the rest or what exactly?

Top answer
1 of 3
27
These things are separate because turning a set of C or C++ source files into an executable is a multi-step process. gcc is the GNU Compiler Collection. It deals with turning source files into architecture-dependant assembly code. It's the first step in compilation, and is typically the only user-invoked bit of the process (gcc typically deals with calling the assembler and linker automatically) glibc is the GNU implementation of the Standard C library. The Standard C library is a set of C header files that provide core definitions and low-level functionality needed for non-trivial programs to operate. See here http://en.wikipedia.org/wiki/C_standard_library libstdc++ is the C++ standard library. It provides the same core definitions and low-level functionality as the Standard C library, plus a whole heap of other stuff too. (stuff that, if you'd done the program in C, would need additional external libraries linked in) See here http://en.wikipedia.org/wiki/C%2B%2B_standard_library binutils are programs for translating assembly language into object code, and various manipulation techniques for linking object files together. http://en.wikipedia.org/wiki/Binutils An example might be helpful here, so here goes. main.c int main() { hello(); bye(); } hello.c #include void hello() { printf ("Hello World!\n"); } bye.c #include void bye() { printf ("Goodbye World!\n"); } As you can see from the source files, we're including the input/output functions from the Standard C library. Calling gcc in the conventional way will just compile the thing in one go, without showing you any of the intermediate steps. gcc -o foo main.c hello.c bye.c You get an executable foo However, if you tell gcc not to do anything other than compile to assembly, you can see the intermediate steps. gcc -S -o main.S main.c gcc -S -o hello.S hello.c gcc -S -o bye.S bye.c Now you have 3 assembly files, translated from the C source code. View these in a text editor to see the assembly hello.S .file "hello.c" .section .rodata .LC0: .string "Hello World!" .text .globl hello .type hello, @function hello: .LFB0: .cfi_startproc pushq %rbp .cfi_def_cfa_offset 16 .cfi_offset 6, -16 movq %rsp, %rbp .cfi_def_cfa_register 6 movl $.LC0, %edi call puts popq %rbp .cfi_def_cfa 7, 8 ret .cfi_endproc .LFE0: .size hello, .-hello .ident "GCC: (Gentoo 4.8.2 p1.0, pie-0.5.8) 4.8.2" .section .note.GNU-stack,"",@progbits Then you can run the assembly through the assembler to get the 3 object files as -o main.o main.S as -o hello.o hello.S as -o bye.o bye.S These object files are binary, so not viewable by a text editor. Then you can link the 3 object files together into a single executable (ld is a pain to use directly, so I'm using it via gcc) gcc -o foo main.o hello.o bye.o Bingo, your foo executable.
2 of 3
5
Ideally somebody else will point you to a writeup on this, but here's quick overview as I understand it. Here I'm writing from the perspective of building a cross-compiler, which isn't quite what you're asking, but it's the sequence I know and may be of help: binutils. You need to build these first, and these you get a set of tools including the assembler, linker, etc. Bootstrapping a cross-compiling gcc is a pain in the arse. My memory is that the next step is to build a basic gcc. Next we can build glibc, the C run time library. Now we can rebuild gcc including the C++ compiler. Finally libstdc++, the C++ run time library, can be built with our shiny new C++ compiler. The last time I had to play this game I used crosstool-NG to do the heavy lifting for me.
Find elsewhere
Top answer
1 of 2
30

One of the reasons is that GCC can be built and used on (e.g. proprietary Unix systems like MacOSX, Solaris, HPUX or some FreeBSD) systems having their own C standard library.

Even on Linux, you can have a C standard library which is not the GNU Glibc. In particular, you can build GCC (or use it) on Linux systems with musl-libc or with Bionic (Android systems) or with dietlibc, etc. And a Linux system could have the GNU Glibc and use some other C compiler (like Clang or TinyCC).

Also, the C library heavily depends upon the Linux kernel. Some old versions of the kernel might require some particular kind (or version) of libc

And GCC is buildable as a cross-compiler.

And details like "how to call a main function" also depends on the compiler, but in fact, those details are supplied by libc.so on a Linux system.

That is not exactly correct. The main function is called (in a hosted environment) by the crt0 stuff, some of which is provided by GCC (e.g. /usr/lib/gcc/x86_64-linux-gnu/6/crtbegin.o on my Debian/Sid/x86-64 is from the libgcc-6-dev package). Read also about libgcc

Actually, there is some half-hidden relation between libc and GCC, e.g. because many libc headers are (optionally) using some gcc builtins or function attributes.

(hence the GCC developers and the GNU libc developers do have to interact)

.... if I change the compiler to work with another ABI...

You'll need to .../configure the GCC compiler and rebuild it, and you might even need to patch the GCC compiler (to describe your ABI and calling conventions). The x32 ABI is a good example.

At last, some contributors to or maintainers of GCC (including me) have signed a copyright assignment which covers GCC but not the GNU glibc.

(regarding GCC license, read carefully GCC runtime library exception)

Notice that some standard headers, like <limits.h> or <stdint.h> are provided by GCC; others, like <stdlib.h> are "fixed" during GCC build: the compiler build procedure takes them from the Libc implementation and patches them. Still, other standard headers (probably <stdio.h> and the internal headers it is including) are taken from the libc. Read more about GCC FIXINCLUDES and Fixed Header Files.

(the fixincludes thing is something I (Basile) still don't understand well)

You could compile with gcc -v -H to understand more precisely which actual programs are run (since gcc is a driver, running the cc1 compiler, the ld & collect2 linkers, the as assembler, etc...) and which headers are included, which libraries and object files are linked (even implicitly, including the C standard library and the crt0). Read more about GCC options.

BTW, you can use a C standard library different of the one your GCC expects or was built for (e.g. musl-libc or some dietlibc), bypassing appropriate extra arguments to gcc ...

2 of 2
-5

The short answer is that if the two were 'bundled' together, glibc would be licensed under the GPL*, and it would therefore be completely unsuitable for proprietary projects. While the FSF and GNU project has no love for proprietary software, glibc was licensed LGPL as a strategic choice to advance adoption of GCC and the free software ecosystem. GCC is actually licensed under the GPL with a specific runtime linking exception, because the situation is somewhat muddy. glibc is licensed per the LGPL to permit sensible shared-library situations.

https://www.gnu.org/licenses/gcc-exception-faq.html

Additionally, glibc has all kinds of shims and other components to adapt it for varying operating systems, and distributing that as the same package as gcc would also make things messy.

*Alternativly, GCC could be licensed under something other GPL, though the FSF's thoughts on that would be along the lines of "over my dead body".

🌐
GitHub
github.com › conda-forge › ctng-compilers-feedstock › issues › 60
Should libgcc-ng be the same version as gcc? · Issue #60 · conda-forge/ctng-compilers-feedstock
July 27, 2021 - Issue: I use the cxx-compiler (gcc 9.3, linux-64, micromamba) to compile and sanitize (with -fsanitize=address) some code, which used to work well. Now, installing cxx-compiler installs gcc 9.3 but libgcc-ng 11.1.0 and I get a linker error
Author   conda-forge
🌐
Stack Overflow
stackoverflow.com › questions › 78613107 › what-is-the-difference-between-libgcc-and-libc
c - What is the difference between libgcc and libc? - Stack Overflow
June 12, 2024 - Per the documentation, libgcc is a low-level runtime library, that is used by gcc internals "automatically".
🌐
GNU
gnu.org › software › gcc › gcc-3.0 › libgcc.html
libgcc - GNU Project
February 20, 2023 - There is no real choice. It must be possible for shared libraries built with different versions of libgcc to work together well. Therefore, a shared version of libgcc will be distributed with GCC 3.0.
🌐
Reddit
reddit.com › r/c_programming › questions about c standard library, glibc and gcc
r/C_Programming on Reddit: Questions about C standard library, glibc and gcc
July 15, 2020 -

I downloaded glibc source from https://sourceware.org/git/?p=glibc.git and was expecting that I would be able to find all the header files from https://en.wikipedia.org/wiki/C_standard_library#Application_programming_interface and their implementation.

(also found on the latest draft for the latest C standard here https://web.archive.org/web/20181230041359if_/http://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf)

Fortunately my expectations weren't met and I am now on a journey of understanding things a bit better.

This prompted me to search for the missing header files and I found them in /usr/lib/gcc/{triple}/{version}/include folder

Now according to the standard and the question here https://stackoverflow.com/questions/37385899/cannot-find-iso646-h-in-glibc there are 2 forms of conforming implementation:

  1. The freestanding implementation confined to the contents of the standard headers <float.h>, <iso646.h>, <limits.h>, <stdalign.h>, <stdarg.h>, <stdbool.h>, <stddef.h>, <stdint.h>, and <stdnoreturn.h>

  2. The hosted implementation which accepts any strictly conforming program i.e. all 29 headers.

Questions:

  1. Why does glibc also contain files float.h and limits.h?

  2. Why is there an equivalent file to the ones in the freestanding implementation in glibc in the folder glibc_root/conform/data/[freestanding_file.h]-data

  3. Does gcc only provide the freestanding implementation headers?

  4. Are the freestanding implementation headers header-only (no linkable code)?

  5. Do the C standard implementation libraries for bare-metal need to implement all the functionality in the rest of the 20 headers? Or does each implementation implement only a subset?

  6. Will arm-none-eabi-gcc and gcc both always provide all the freestanding implementation header files? Or is there an impementation e.g. musl that will implement/provide some of the freestanding headers?

Thanks for any help.

Top answer
1 of 5
6
gcc provides all freestanding implementation header files when you use the -ffreestanding , but will provide the rest if you are using it in a hosted environment . The C Standard implementation for bare-metal environments doesnt necessarily need to act like a hosted environment either.
2 of 5
5
This page gives some more detail about what GCC does and doesn't provide (specifically, it provides some but not all parts of the "freestanding environment" demanded by the standard). Basically, the "C runtime" consists of several components that are automagically linked into any normal userspace program you build, but are usually disabled by stuff like kernels and embedded firmware: The C library: this is your usual glibc, musl or whatever. Almost(?) all the normal functions defined in the C standard are in here, including things from the "freestanding environment" like memcpy. The reason for that is that memcpy can get very complicated (it is probably the most commonly called function ever, so people care very much about optimizing it and there are many possible trade-offs to make) and GCC cannot easily provide a "one size fits all" version. The -nolibc flag can tell GCC not to link this if you want to write your own. The "startup files": the exact details here are architecture and OS dependent, but usually this is a file called crt0.o (you can probably find and disassemble that in your /lib or /usr/lib if you're curious) that defines the function _start, which is where the real entry point of the program is. It handles a few odd features like library constructors (code that a library wants to run before the program starts, can be written with __attribute__((constructor))), assembles the command line arguments in the commonly expected form, and then calls main. This can be disabled with -nostartfiles if you're writing something that's not a userspace program (you probably want a linker script as well). libgcc: this is the library installed directly by GCC (unlike libc) that you found under /usr/lib/gcc. It contains stuff that needs to be so tightly coupled to the compiler that expecting other libc developers to provide it wouldn't make much sense. I believe is here, and things like software emulation for math operations that an architecture doesn't support in hardware. The -nostdlib flag even disables this one (in addition to all of the above) and then you can find fun issues like when you think you just have "pure" C code like a / b somewhere but you suddenly get an undefined reference error for something called _uldivmod, because it turns out that your architecture doesn't natively support 64-bit division and libgcc has just always been software-emulating it for you behind your back.
🌐
UNIX Packages
unixpackages.com › home › support › help
GCC/Libgcc - UNIX Packages
January 8, 2012 - You can determine if one of these libraries is needed by running ldd on an executable to see the library dependencies like libgcc,so or libstdc, etc. The software used to create gcc 3.4.6 (the steps are very similar for earlier versions of gcc) was all taken from packages on UNIX Packages: gcc-3.3.2, bison-2.5, flex-2.5.35, texinfo-4.13a, autoconf-2.68, make-3.82, and automake-1.11.1.
🌐
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
libgcc1.a · ’ that does the job it is expected to do. To compile ‘ · libgcc1.c · ’ with the cross-compiler itself does not work. The functions in this file are supposed to implement arithmetic operations that GNU CC does not know how to open code for your target machine.
🌐
GitHub
cs107e.github.io › guides › gcc
CS107E Guide: Bare-Metal Programming using gcc
Most of the compiler support routines used by GCC are present in libgcc, but there are a few exceptions.
🌐
Red Hat
bugzilla.redhat.com › show_bug.cgi
1688766 – gcc: Linking against libgcc_s should imply linking with libgcc
May 7, 2019 - Red Hat Bugzilla – Bug 1688766 · This site requires JavaScript to be enabled to function correctly, please enable it · Privacy Contact FAQ Legal
🌐
Yocto Project
lists.yoctoproject.org › g › poky › topic › question_re_gcc_runtime_vs › 61284522
question re gcc-runtime vs libgcc - Poky
November 21, 2019 - Yocto Project™ | An open source collaboration project of the Linux Foundation The Yocto Project™ is an open source collaboration project that helps developers create custom Linux-based systems regardless of the hardware architecture. * If you are interested in membership, please visit ...
🌐
GNU
gcc.gnu.org › onlinedocs › gcc-7.5.0 › gcc › Link-Options.html
Using the GNU Compiler Collection (GCC): Link Options
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, ...