-3

All C language software I have seen so far makes extensive used of the so-called "C Standard Library", a piece of software that is part of (or actually is) the C standard itself. So, whatever the moderators think, this is not a book nor a commercial software. It's instead the basic software library containing the implementation for stuff like malloc(), printf() and strcpy() and is usually bundled with all systems.

Now, I need to write some software in C language without using that library as it's neither available nor viable in that environment. But I still need to read and write files and sockets.

Any idea or pointer to similar problems or environments?

EnzoR
  • 3,107
  • 2
  • 22
  • 25
  • 6
    If you don't like `FILE*`, then use your kernel's I/O calls instead. The point of the C library is to introduce portability and a common API layer – Mark Nunberg Oct 27 '14 at 16:01
  • What are you trying to accomplish by using an alternative library? The standard C library is the only library that's standard across all C implementations. – Barmar Oct 27 '14 at 16:05
  • **Why do you ask** ? – Basile Starynkevitch Oct 27 '14 at 16:10
  • 3
    While worded as it is now, question is clearly off-topic (and now closed as such), the idea itself should occur to every aspiring C programmer who has a brain. Therefore the amount of down votes and questions asking why do you ask are a bit... disappointing. But you got a good answer still :-) – hyde Oct 27 '14 at 16:52
  • A C library is a layer between the C program and the OS thus also pushing a certain paradigm at a definite cost. The Standard C library is one. I am not interested in portability but in other tings like efficiency and alternative paradigms. – EnzoR Oct 27 '14 at 20:23
  • +1 for the question not deserving such downvotes. Though it reminds me of that [commercial for string cheese](https://www.youtube.com/watch?v=BG7273yDpdA): *"Give me a pizza, but hold the tomato sauce. And hold the crust."* – HostileFork says dont trust SE Oct 28 '14 at 03:45
  • @Moderators. The question "I need to write a program in C without using the C standard library. How to?" I would have achieved the same goal without "recommend or find a book, tool, software library, tutorial or other off-site resource". I would then ask you to reconsider the "off-topic" tag. – EnzoR Oct 28 '14 at 07:28
  • @Paul Griffiths, etc. Please reconsider your decision: read the question again and possible my comments. – EnzoR Dec 02 '14 at 07:33
  • I reworded it, added comments too. But the question has kept its status of closed! Ah! – EnzoR Jan 29 '15 at 13:45

1 Answers1

2

Stricto sensu, a portable (hosted) C99 program not using the standard library is even unable to make any input/output, or probably make any observable side effect. And you won't even be able to allocate any data in the heap (like malloc does). BTW a libc is sort-of required by any C99 hosted implementation; you want a freestanding library for C (and it would by definition be "implementation specific"; indeed it could use OS specific services in an implementation specific way. See also this).

On old Linux (and IIRC, SVR4 e.g. SunOS 5), there used to be some libsys which only interfaced the syscalls(2). AFAIK, it is deprecated, and I cannot find it.

Some libraries (like Glib from GTK, ....) are offering equivalent of most standard C library functions (but they are expecting some libc, i.e. a hosted POSIX system).

Notice that you might consider using musl-libc and perhaps remove the functions you don't want from it. You could also consider using some libc providing a subset of the standard, e.g. dietlibc. See also wikipage on C standard library.

BTW, you might have issues in coding in C any user-land Linux application which does not use the libc (e.g. because of crt0).

The strange thing is that I cannot today even name a free software language which is not using somehow C. In principle that could be possible (e.g. on Linux compiled to only call the syscalls), but I don't know anything like that.

Notice however that (at least on Linux) if you code a statically linked binary which does not call any standard C function, that binary will embark only a small portion of the libc (the one reachable from crt0).

Some standard library facilities (like setjmp(3) or stdarg(3)...) usually require -or at least profit from- some compiler magic (even malloc, printf, free do with recent GCC). Some of the standard library functions are not implementable in portable C.

If using gcc don't forget the -ffreestanding option to gcc in that case. See this.

Refer to n1570, a (draft) C11 standard almost similar to the ISO one, or to n2573, close to the C20 standard. See also Modern C and this C reference website. Examples of freestanding open source applications includes the Linux kernel, but there are many others on github or gitlab and mentioned on OSDEV, but also for Arduino or RaspberryPi. I recommend downloading their source code and studying it for inspiration.

NB: you could consider that there are two dialects of the C99 language: the hosted C which contains standard libc functions like fprintf, malloc, etc... and starts with main ... and the freestanding C which does not contain them (but do contain setjmp etc...) and has no starting conventions.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Since every hosted implementation is required to provide a standard C library, it should be OK for the replacement to depend on it. – Barmar Oct 27 '14 at 16:04
  • You can use C as a mere "high level assembly". If you have an alternate way to interact with the OS you don't need the StdLibC. – EnzoR Oct 27 '14 at 20:28
  • @Enzo: you are right if you think of freestanding C. Hosted standard C99 *requires* functions like `malloc`, `setjmp`, `fprintf` ... – Basile Starynkevitch Oct 27 '14 at 20:31
  • Basile, it looks like the answer to my question lies between "there is none" and "I don't know". For example, I doubt that the Linux kernel contains any malloc, fprintf or setjmp. Nonetheless it's C. Very likely something in between C99 and C11. What I am talking is something about the library used in that kernel. I don't mind (and didn't asked) about C99/C11 compliance. I just need a "library" capable more or less of the same features of the "C standard library". Will I have issues? Well, that will be my job! – EnzoR Oct 28 '14 at 07:19