0

Is it possible to compile a c program without the c runtime library that is usually linked automatically by the compiler.

I know this library provides basic routines that are os-dependent but, can a program run without it, if so, how can I unlink this library?

  • You need to explain what exactly "`the c runtime library that is usually linked automatically by the compiler`" is, because it is not a part of the C language standard. And your question is generally not about C, it's about (the) compiler. Now, there are many different compilers, each one may be designated for different OS and/or HW architecture. So there's generally no way to answer your question in a consistent and unified manner. –  Nov 03 '22 at 20:27
  • Possibly related to your query: https://stackoverflow.com/questions/30825151/is-there-a-meaningful-distinction-between-freestanding-and-hosted-implementation – hyde Nov 03 '22 at 20:29
  • Yes, it is possible. How to disable standard library linkage depends on compiler (linker). – dimich Nov 03 '22 at 20:33
  • To study the subject, learn to write pure assembly programs for the OS you are interested in. That is, use only assembler and linker, no C compiler. – hyde Nov 03 '22 at 20:38
  • You would have to write your code with no usage of the library. You would generally still need the startup code. Also, some compilers depend on implementation provided functions when generating code for some things. Such code may be packaged with the library. So maybe. It depends on details. I have done a bare metal embedded system where I didn't need the startup code or most of the library. I used a rebuilt library with just the few parts of it I used (For reasons not relevant here). – Avi Berger Nov 03 '22 at 20:38
  • 2
    It's definitely possible, since that's how the OS kernel is compiled. The C standard calls this a "freestanding" implementation. – Barmar Nov 03 '22 at 21:06
  • What program or execution characteristics are you looking to obtain by isolating a program from the "C runtime library"? I ask in part because what you describe sounds a lot like building a program for a freestanding C implementation. But you would not run such a program from within an OS -- that's the essential distinction between "freestanding" and "hosted". – John Bollinger Nov 03 '22 at 21:41
  • @JohnBollinger It is possible to compile freestanding program which run within an OS. Such program just have to satisfy some requirements for memory layout and invoke syscalls directly. – dimich Nov 03 '22 at 21:55
  • How does a C program invoke syscalls directly, @dimich? A C + [inline] assembly program can do that, but that's a different beast. In any event, this is a matter of definition. A freestanding environment is one "in which C program execution may take place without any benefit of an operating system." A program that relies on there being an OS around whose services can be obtained by any means is by definition not built for a freestanding environment. – John Bollinger Nov 03 '22 at 22:08
  • @JohnBollinger I agree, "freestanding" is wrong term i used. Let it be "program without standard library". – dimich Nov 03 '22 at 22:33
  • You can definitely code in C without standard library: 1) your new entry point will be _start(void), main() is the entry point to your code because libc calls it (and sending argc, argv, envp), 2) don't link libc when compiling to .o (with GCC: -nolibc -nostdlib -fno-builtin), 3) have a syscall() function writen in ASM (NASM is a good candidate), 4) link objects using ld, 5) from there, you can reimplement your own system calls, 6) you have everything to reimplement standard library – AR7CORE Nov 07 '22 at 09:53

0 Answers0