From Essence to Implementation: What are the C and C++ Standard Libraries?

Follow and star our public account for exciting content

From the public account: OSC Open Source Community

Link: https://www.oschina.net/translate/c-c-standard-library?origin=wechat

Collaborative Translation

Original Text: What are the C and C++ Standard Libraries?

Link: https://www.internalpointers.com/post/c-c-standard-library

Translators: Tocy, Tot_ziens, 雪落无痕xdj, 琪花亿草, 周其

This article provides a brief introduction to the field of writing C/C++ applications, the role of the standard library, and how it is implemented across various operating systems. I have been working with C++ for some time, and one of the things that puzzled me at first was its internal structure: where do the kernel functions and classes I use come from? Who invented them? Are they packaged somewhere in my system? Is there an official C++ manual? In this article, I will attempt to answer these questions by discussing the essence of C and C++ languages and their practical implementations.

How C and C++ are Defined

When we talk about C and C++, we are actually referring to a set of rules that define what a programming language should do, how it should behave, and what functionalities it should provide. C/C++ compilers must follow these rules to process source code written in C/C++ and generate binary applications. This sounds very similar to HTML: browsers follow a set of instructions so they can render web pages in a specific way. Like HTML, the rules for C and C++ are theoretical.A large group of people from the International Organization for Standardization (ISO) gathers several times a year to discuss and define the language rules. Yes, C and C++ are standardized. They eventually receive an official book called a standard, which you can purchase from their website.

As the languages evolve, new papers (referring to the official standard books) are published, each defining a new standard. This is why we have different versions of C and C++: C99, C11, C++03, C++11, C++14, etc., with the numbers corresponding to the year of publication/release.

These standards are very detailed and technical documents: I wouldn’t consider them manuals. They are usually divided into two parts:

1. Features and characteristics of C/C++;

2. The C/C++ API—a collection of classes, functions, and macros that developers can use in their C/C++ programs. This is also known as the standard library.

For example, here is an excerpt from the first part of the C standard library that defines the structure of the main function:

From Essence to Implementation: What are the C and C++ Standard Libraries?

1. Definition of main, the function called when the program starts.

This is another excerpt from the same standard that describes the C API member—the fmin function:

From Essence to Implementation: What are the C and C++ Standard Libraries?

2. Definition of the min function in the math.h header file.

As you can see, there is hardly any code involved. Someone has to read the standard and translate it into something that the computer can digest. This is the work done by people working on compilers and (functional) implementations: the former is a tool that can read and process C and C++ source files, while the latter converts the standard library into code. Let’s delve deeper.

C Standard Library

The C Standard Library, also known as the ISO C Library, is a collection of macros, types, and functions used to perform tasks such as input/output processing, string manipulation, memory management, mathematical computations, and many other operating system services. It is defined in the C standard (e.g., C11 standard). Its contents are distributed across different header files, such as the math.h I mentioned above.

C++ Standard Library

Similar to the concept of the C Standard Library, but specifically for C++. The C++ Standard Library is a set of C++ template classes that provide generic programming data structures and functions, such as linked lists, heaps, arrays, algorithms, iterators, and any other C++ components you can think of. The C++ Standard Library also includes the C Standard Library and is defined in the C++ standard (e.g., C++11 standard).

Implementing the C/C++ Standard Library

Now we start discussing real code. Developers working on the implementation of the standard library read the official ISO specifications and translate them into code. They must rely on the functionalities provided by their operating system (reading/writing files, allocating memory, creating threads, etc.—all of these are called system calls), so each platform has its own implementation of the standard library. Sometimes it is part of the system kernel, and sometimes it is an additional component that the compiler must download separately.

GNU/Linux Implementation

The GNU C Library, also known as glibc, is the GNU project’s implementation of the C Standard Library. Not all standard C functions can be found in glibc: most mathematical functions are actually implemented in the libm library, which is a separate library.

As of today, glibc is the most widely used C library on Linux. However, during the 1990s, there was a time when glibc had a competitor called Linux libc (or simply libc), which was derived from a branch of glibc 1.x. For a time, Linux libc was the standard C library in many Linux distributions. After years of development, glibc eventually gained an advantage over Linux libc, and all Linux distributions that used it switched back to glibc. So, if you find a file named libc.so.6 on your disk, don’t worry: it is the modern version of glibc. To avoid confusion with earlier versions of Linux libc, the version number was increased to 6 (they couldn’t name it glibc.so.6: all Linux libraries must start with the lib prefix). On the other hand, the implementation of the C++ Standard Library is located in libstdc++ or the GNU Standard C++ Library. This is an ongoing project to implement the standard C++ library on GNU/Linux. Generally, all mainstream Linux distributions default to using libstdc++.

Mac and iOS Implementation

On Mac and iOS, the implementation of the C Standard Library is part of libSystem, which is the core library located in /usr/lib/libSystem.dylib. LibSystem contains other components such as the math library, threading library, and other low-level utilities.

Regarding the C++ Standard Library, before OS X Mavericks (V10.9), libstdc++ was the default option on Macs. This is the same implementation found on modern Linux-based systems. Starting with OS X Mavericks, Apple switched to using libc++, which is an alternative to the GNU libstdc++ standard library introduced by the LLVM project—the official compiler framework for Mac. iOS developers can access the standard library through the iOS SDK (Software Development Kit), which is a set of tools that allow the creation of mobile applications.

Windows Implementation

On Windows, the implementation of the standard library has been strictly confined to Visual Studio, which is Microsoft’s official compiler. They usually refer to it as the C/C++ runtime library (CRT), which covers the implementations for both C and C++. Initially, the CRT was implemented as the CRTDLL.DLL library (I guess there was no C++ standard library available at that time). Starting with Windows 95, Microsoft began migrating it to MSVCRT [version number].DLL (MSVCR20.DLL, MSVCR70.DLL, etc.), which presumably also included the C++ standard library.

Around 1997, they decided to simplify the filename to MSVCRT.DLL, which unfortunately led to the annoying DLL confusion. This is why, starting from Visual Studio 7.0, they switched back to using separate DLLs for each version.

Visual Studio 2015 introduced a deep restructuring of the CRT. The implementation of the C/C++ standard library was migrated to a new library, the Universal C Runtime (Universal CRT or UCRT), compiled as UCRTBASE.DLL. UCRT has now become part of the Windows group, provided as part of the operating system starting with Windows 10.

Android Implementation

Bionic is the C Standard Library implementation written by Google for its Android operating system, which operates directly at the low level. Third-party developers can access Bionic through the Android Native Development Kit (NDK), which allows you to write Android applications using C and C++ code.

On the C++ side, the NDK provides several versions of implementations:

  • libc++, which has been used as the C++ standard library since Lollipop in the official Android system and modern Mac operating systems. Starting from NDK release 17, it will become the only available C++ standard library implementation in the NDK;

  • gnustl, an alias for libstdc++, which is the same library found in GNU/Linux. This library has been deprecated and will be removed in NDK release 18;

  • STLport, a third-party implementation of the C++ standard library written by the STLport project, which has been inactive since 2008. Like gnustl, STLport will be removed in NDK release 18.

Can Different Versions of Implementation Code Replace the Default Implementation?

If you are working with a resource-constrained system, you may often need to reference different implementations of the C Standard Library. For example, uClibc-ng, musl libc, and diet libc, all of which are suitable for developing embedded Linux systems, providing smaller binaries and less memory usage. The C++ Standard Library also has different implementation versions: Apache C++ Standard Library, uSTL, and EASTL, etc. The latter two focus primarily on the template portion rather than the complete library and are developed with speed as a priority. The Apache version of the library emphasizes portability.

What if We Stray from the Standard Library?

It is simple to not use the standard library: just don’t include any of its header files in your program, and you’re done. However, to make this operation more meaningful, you need to interact with the operating system in some way using the system calls provided.

As I mentioned earlier, this is what the functions/methods in the standard library use when they are implemented at a low level. You will likely also have to call these methods to interact with hardware devices.

If this sounds exciting to you, some people have already started trying to create workflows online without importing the standard library. Since you rely on functions provided by a specific operating system, this approach loses portability. However, using this challenging method will certainly teach you more and give you a better understanding of what you are doing, even when using high-level libraries.

Besides knowledge, when working on embedded operating systems, you wouldn’t want to introduce the standard library: because the code doesn’t need to be portable, every byte is important in limited memory, which will make you write code more precisely.

Another context of use is the demoscene, where people try to preserve high-quality audio and video within limited binary sizes—4K is still not the minimum: some demoparties use 1K, 256 bytes, 64 bytes, or even 32 bytes to compete. In that context, the use of the standard library is not allowed!

Copyright Statement: Content sourced from the internet, copyright belongs to the original author. Unless unable to confirm, authors and sources will be indicated. If there is any infringement, please inform us, and we will delete and apologize immediately!

‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧  END  ‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧

Leave a Comment