4 ms·
It certainly does have a runtime and it isn't optional. It's usually in object files starting with crt, e.g. crt1.o. "c-run-time-1.o". That does whatever is ne
by JonChesterfield 2y ago
It certainly does have a runtime and it isn't optional. It's usually in object files starting with crt, e.g. crt1.o. "c-run-time-1.o". That does whatever is necessary to make the machine look like the C world expects.
Setting up the stack pointer before jumping to main is important. Arranging for argc/argv to work. Waking init array or section for global constructors.
There's also libc which is roughly a pile of more-or-less useful functions, some of them implemented in terms of syscalls. That one is nominally optional.
There's probably a compiler runtime involved as well, libgcc or libclang_rt.builtins or similar, which is likely to be cyclically dependent on libc. Compiler-dependent whether that is optional, and thus likely whether libc is optional in practice.
- PhilipRoman 2y agoYou absolutely can write void _start(void) and as long as you finish using exit() everything will work just fine. I've written some super small programs this way (on the order of few kilobytes) which don't interface with system libraries in any way.
- planede 2y agoC code conforming to the ISO C standard generally requires a (very small) runtime.
- JonChesterfield 2y agoThat'll work on some systems and not others. Whatever compiled and linked your C arranged to _start to be at the place the instruction pointer starts from. Setting up your own stack from C is dicey but sometimes the default value for the stack register is usable as-is. I tend to go with ffreestanding and the crt*.o for the platform instead of going from _start directly but sure, you can write whatever startup code your platform wants yourself and link with -nostdlibs or similar. Some platforms are OK with just the address of the entry point burned into an elf. Compiler test code sometimes looks like that.
- jstimpfle 2y agoAny other language can implement the same conventions, and those conventions aren't specified in the C standard but really platform specific.
- YZF 2y agoI would not call this a runtime myself. Go's runtime is "stuff Go does while your program is running" which is very different than something like crt0 that does some initial setup. Either way, this is optional in C. Some setup is done by the OS as well (it's not like the "run time" makes up where the stack is going to be).