Implementation of headers

I've recently been learning about command-line commands and how all that stuff works and I have gotten curious, for various of the standard headers in C++ (such as iostream, or time, etc.) are they built using these commands?

I know that there exists the std::system function that executes UNIX/Command-line commands and was just kind of wondering std::system (or something of the sort) is used to implement more system dependent actions (like creating files).

This is mainly a curiosity question as I've been kind of wondering how those libraries are implemented.
Last edited on
No, the standard library is normally not implemented using "command-line commands". Instead it uses system libraries like the WinAPI on Windows or the C POSIX library (and other system-specific functions) on Linux. It might also make use of special features that are built-in to the compiler itself.
Last edited on
The "standard" header files in the C/C++ programming language mostly provide the interface to the C Runtime Library (CRT) and to the C++ Standard Library. I said "mostly", because some functions are actually implemented "inline" right inside of the header files. And there also are so-called "intrinsic" functions that are actually built into the C/C++ compiler. But, for the most part, these "standard" header files really just delcare the API (Application Programming Interface), i.e., function prototypes, data types, etc. pp., that allow your application to use the "standard" functions that are implemented in the C Runtime Library (CRT) and the C++ Standard Library.

On Windows, the C Runtime Library (CRT) is MSVCRT.DLL and the C++ Standard Library is MSVCP.DLL, whereas on Linux the C Runtime Library (CRT) typically is libc.so and the C++ Standard Library is either libstdc++.so (GCC) or libc++.so (Clang/LLVM).

Now, how are the "standard" functions actually implemented in the C Runtime Library (CRT) and the C++ Standard Library? Well, to a large part, they are implemented right there, inside the respective library, written as C/C++ code, with some portions of Assembler code here and there. But, the C Runtime Library (CRT), and probably the C++ Standard Library too, also do certain things that require the help of the underlying operating system! File I/O would be a typical example of this. For this kind of stuff, the C Runtime Library (CRT) and the C++ Standard Library internally make use of syscalls to access the function that are provided by the operating system kernel.

For example, the standard C function fopen() ultimately maps to the Linux/Unix system call open:
https://cplusplus.com/reference/cstdio/fopen/
https://en.wikipedia.org/wiki/Open_(system_call)

(which also means that the C type FILE essentially wraps the actual file descriptor from the underlying operating system)


Windows is kind of "special" here, in that it puts yet another layer of user-space libraries, the Win32 API (e.g., kernel32.dll), in between the C library and the operating system kernel; Linux and other Unix-like operating systems do not have anything like that.

So, the standard C function fopen() maps to the Win32 API function CreateFile(), which in turn maps to the syscall NtCreateFile:
https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/fopen-wfopen?view=msvc-170
https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilew
https://learn.microsoft.com/en-us/windows/win32/api/winternl/nf-winternl-ntcreatefile


If you want to see how a C library is implemented, you can find the source code of the most popular C library, i.e., glibc, here:
https://github.com/bminor/glibc

Because glibc supports many different platfroms, it can be a bit difficult to understand. The musl libc is, IMHO, quite a bit simpler:
https://github.com/kraj/musl

The source code of Microsoft's C runtime library (MSVCRT) can also be found online, if you are interested:
https://github.com/cansou/msvcrt/tree/master/src


And, nope. The "standard" functions are not implemented by invoking any command-line tools. If that was the case, it would lead to an infinite recursion, since most of the command-line tools are themselves written in C and use the C Runtime Library (CRT) too 😏

(That plus: The overhead of creating a new process and parsing its output would be much higher than performing a simple syscall)
Last edited on
I see! thank you for the sources and stuff, I will have to check them out! I've always been curious about the inner workings of this stuff so I apperciate that you gave me some sources to look into!
Registered users can post here. Sign in or register to post.