Welche Rolle spielt libc(glibc) in unserer Linux-App?

Lesezeit: 5 Minuten

Benutzeravatar von basketballnewbie
Basketballneuling

Wenn wir ein Programm mit debuggen gdbsehen wir normalerweise Funktionen mit seltsamen Namen, die in definiert sind libc(glibc?). Meine Fragen sind:

  1. Ist libc/glibc die Standardimplementierung einiger C/C++-Standardfunktionen wie z strcpy,strlen,malloc?
  2. Oder ist es nicht nur die erste Verwendung wie oben beschrieben, sondern auch ein Wrapper von Unix/Linux-Systemaufrufen wie open,close,fctl? Wenn dem so ist, warum können wir Systemaufrufe nicht direkt, ohne ausführen libc?
  3. Tut libc bestehen nur aus einer lib (.a oder .so)-Datei oder viele lib-Dateien (in diesem Fall libc ist der allgemeine Name dieses Satzes von Bibliotheken)? Wo befinden sich diese lib-Datei(en)?
  4. Was ist der Unterschied zwischen libc und glibc?

libc implementiert beide Standard-C-Funktionen wie strcpy() und POSIX-Funktionen (die Systemaufrufe sein können) wie getpid(). Beachten Sie, dass nicht alle Standard-C-Funktionen enthalten sind libc – Die meisten mathematischen Funktionen sind in libm.

Sie können Systemaufrufe nicht direkt auf die gleiche Weise wie normale Funktionen ausführen, da Aufrufe an den Kernel keine normalen Funktionsaufrufe sind und daher nicht vom Linker aufgelöst werden können. Stattdessen werden architekturspezifische Assembler-Thunks verwendet, um den Kernel aufzurufen – Sie können diese natürlich auch direkt in Ihr eigenes Programm schreiben, müssen dies aber nicht, weil libc stellt sie für Sie bereit.

Beachten Sie, dass es sich bei Linux um die Kombination von Kernel und handelt libc das die POSIX-API bereitstellt. libc fügt eine anständige Menge an Wert hinzu – nicht jede POSIX-Funktion ist notwendigerweise ein Systemaufruf, und für diejenigen, die es sind, ist das Kernel-Verhalten nicht immer POSIX-konform.

libc ist eine einzelne Bibliotheksdatei (sowohl .so und .a Versionen sind verfügbar) und befindet sich in den meisten Fällen in /usr/lib. Das Projekt glibc (GNU libc) bietet jedoch mehr als nur libc – es bietet auch die libm bereits erwähnt, und andere Kernbibliotheken wie libpthread. So libc ist nur eine der von glibc bereitgestellten Bibliotheken – und es gibt andere alternative Implementierungen von libc außer glibc.

In Bezug auf die ersten beiden ist glibc sowohl die C-Standardbibliothek (z. B. “Standard-C-Funktionen”) als auch ein Wrapper für Systemaufrufe. Sie können Systemaufrufe nicht direkt absetzen, da der Compiler nicht weiß, wie — glibc enthält den “Klebstoff”, der zum Absetzen von Systemaufrufen erforderlich ist und in Assembler geschrieben ist. (Es ist möglich, dies selbst neu zu implementieren, aber es ist weitaus mehr Mühe als es wert ist.)

(Die C++-Standardbibliothek ist eine separate Sache; sie heißt libstdc++.)

glibc ist keine Single .so (dynamische Bibliothek) — es gibt eine Menge, aber libc und libm sind die beiden am häufigsten verwendeten. Alle statischen und dynamischen Bibliotheken sind in gespeichert /lib.

libc ist ein Oberbegriff für alle C-Standardbibliotheken – es gibt mehrere. glibc ist die am häufigsten verwendete; andere sind eglibc, uclibc und dietlibc.

Benutzeravatar von paulsm4
paulsm4

Es ist die “Standardbibliothek”. Es ist genau wie “MSVCRTL” in der Windows-Welt.

Die Gnu-Standardbibliothek (“glibc”) ist die Implementierung von libc, die am häufigsten (fast universell?) auf Linux-Systemen zu finden ist. Hier sind die relevanten Dateien auf einem alten SusE-Linux-System:

ls -l /lib =>
-rwxr-xr-x  1 root root 1383527 2005-06-14 08:36 libc.so.6

ls -l /usr/lib =>
-rw-r--r--  1 root root 2580354 2005-06-14 08:20 libc.a
-rw-r--r--  1 root root     204 2005-06-14 08:20 libc.so

Dieser Link sollte alle zusätzlichen Fragen beantworten, die Sie möglicherweise haben (einschließlich Verweisen auf den vollständigen und vollständigen GLibc-Quellcode):

Sie können die detaillierten Informationen über „libc“ und „glibc“ auf den Manpages Ihres Linux-Systems überprüfen, indem Sie „man libc“ in die Shell eingeben, wie unten kopiert;

LIBC(7)      Linux Programmer's Manual      LIBC(7)   

NAME
       libc - overview of standard C libraries on Linux

DESCRIPTION
       The term "libc" is commonly used as a shorthand for the "standard C library", a library of standard functions that can be
       used by all C programs (and sometimes by programs in other languages).  Because of some history (see below), use  of  the
       term "libc" to refer to the standard C library is somewhat ambiguous on Linux.

   glibc
       By  far  the most widely used C library on Linux is the GNU C Library ⟨http://www.gnu.org/software/libc/⟩, often referred
       to as glibc.  This is the C library that is nowadays used in all major Linux distributions.  It is  also  the  C  library
       whose details are documented in the relevant pages of the man-pages project (primarily in Section 3 of the manual).  Doc‐
       umentation of glibc is also available in the glibc manual, available via the command info libc.  Release 1.0 of glibc was
       made in September 1992.  (There were earlier 0.x releases.)  The next major release of glibc was 2.0, at the beginning of
       1997.

       The pathname /lib/libc.so.6 (or something similar) is normally a symbolic link that points to the location of  the  glibc
       library,  and executing this pathname will cause glibc to display various information about the version installed on your
       system.

   Linux libc
       In the early to mid 1990s, there was for a while Linux libc, a fork of glibc 1.x created by  Linux  developers  who  felt
       that  glibc  development  at  the  time  was  not  sufficing for the needs of Linux.  Often, this library was referred to
       (ambiguously) as just "libc".  Linux libc released major versions 2, 3, 4, and 5 (as well as many minor versions of those
       releases).  For a while, Linux libc was the standard C library in many Linux distributions.

       However, notwithstanding the original motivations of the Linux libc effort, by the time glibc 2.0 was released (in 1997),
       it was clearly superior to Linux libc, and all major Linux distributions that had been using  Linux  libc  soon  switched
       back to glibc.  Since this switch occurred long ago, man-pages no longer takes care to document Linux libc details.  Nev‐
       ertheless, the history is visible in vestiges of information about Linux libc that remain in some manual pages,  in  par‐
       ticular, references to libc4 and libc5.

1419960cookie-checkWelche Rolle spielt libc(glibc) in unserer Linux-App?

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy