Reputation: 3330
So I have been trying to understand what exactly is the C runtime, and had to go in depth while building my own kernel.
What I've understood is that you don't need a C runtime to build every C program (as is the case of a kernel). Also, the runtime provides an interface between the C program and the kernel. Here are my questions:
So what exactly does the runtime do ?
Does it only act as this interface (so that if we implement it ourselves, we don't need the runtime), or it has some other functions as well ?
Does the runtime link in any files when a program is compiled ?
Are common functions like printf
or scanf
a part of the runtime or the C library ? What kind of functions would be provided by the runtime ?
EDIT: I have referred to What is the C runtime library?, but it doesn't answer my questions. What I have asked is not being addressed there.
Upvotes: 4
Views: 392
Reputation:
I'm going to answer the question from a different perspective, that of a bare-metal embedded system with no operating system. There are two important functions that are performed by the c runtime in this case:
Uninitialized static variables are given a value of zero (the bss
section)
Explicitly initialized static variables get their intitial value
Those are the minimum responsibilities of the C runtime, although there may be other processor-dependent initialization functions. After performing these tasks the runtime just branches to the main
function. I haven't checked to see if crt1.o
initializes variables but I would assume that it does...my C runtime is just a short assembly program.
Upvotes: 4
Reputation: 363547
I'll answer the last question first to set some definitions:
Are common functions like printf or scanf a part of the runtime or the C library ? What kind of functions would be provided by the runtime?
If you follow this definition of runtime, then yes: it treats "runtime" as nearly synonymous with "standard library implementation" (or at least the compiled part of that). But that's not all there is to it.
Aside from the library, there's also something called the "start file" in common C implementations and this may also be called "the runtime" (e.g. GCC's crt1.o
). This little object does not typically export any public functions.
So what exactly does the runtime do?
Aside from providing the standard APIs (if we use the broad definition of runtime that includes the library), it contains an entry point (called _start
on Linux) for the program that conforms to OS conventions, and this entry point
main
using C conventions (with argc
and argv
);getenv
;main
or the argument to exit
back to the operating system;atexit
callbacks are run even when main
returns;malloc
;stdin
, stdout
, stderr
;libc
and whatever -l
flags for shared libs where given at compile time.Does it only act as this interface (so that if we implement it ourselves, we don't need the runtime), or it has some other functions as well?
That's really platform-dependent. The runtime will do all that is needed to give C programs the environment that the C standard and the compiler manual guarantee. E.g. in the bad old days of MS-DOS, runtimes called "DOS extenders" would also set the CPU in protected mode and do all kinds of API translation to call into DOS in this mode.
Does the runtime link in any files when a program is compiled?
The runtime is at work at runtime (duh), not compile time. If you mean, does the runtime link in files at runtime: it may, e.g. it might link in the DLLs that an executable needs. If you mean, does the compiler link in the runtime files: typically yes, but it may have a flag to disable this or pick different runtimes.
Upvotes: 3