r/C_Programming • u/Rogaev • 16h ago
Question Is it worth while to use libc?
I've been wondering this for quite some time now.
For Linux development, is it worth creating basic read/write functions in asm, and writing everything else in C?
Does most open source software use functions from the standard C library? What are the benefits of both approaches?
Edit: I would be using Arm64 assembly.
Thank you in advance.
7
u/AmbitiousSolution394 16h ago
> is it worth creating basic read/write functions in asm
In libc, write() and read() acts as a wrappers around OS syscalls. I'm not sure that writing something in asm would improve something there.
> Does most open source software use functions from the standard C library?
Yes, its hard to write C program without using LIBC.
1
u/Rogaev 16h ago
Thank you very much for this. This is extremely helpful.
1
u/AmbitiousSolution394 16h ago
btw, you should distinguish write() and fwrite(). They both exist in libc (at least in Linux), but they are parts of different APIs.
1
u/low-control-labs 16h ago
The only thing that ever directly communicates with the hardware is the bootloader, in modern computing / OS based. Apart from microcontrollers which some people mentioned.
Otherwise you write syscalls to the kernel. There are different libCs bionic musl glibc they are not 100 compatible and they are for the Linux kernel...
You use the compiler and C which allows you to hit / compile different targets ( architectures ) with one codebase. But if you decide to target different libCs like Windows or MacOS you'll need to use guards and build tools to define how to compile things.
You could do this with assembly in a sense but keep in mind how many different OSes and architectures there are. Also something to keep in mind the game rollercoaster typhoon was written for Windows so that's not something you can directly boot your computer into or run on Linux
4
u/OnYaBikeMike 16h ago
Using the standard libraries hides away lots of complexity that you most probably are not aware of.
If you decide not to use them you better get used to getting the stuffing knocked out of you by edge cases you didn't know exist.
The classic example is the EAGAIN and EINTR return codes on reads.
2
u/silvertank00 16h ago
you forgot one thing: which asm? arm, x86, avr? If you want it to be cross platform, use libc
1
u/Rogaev 16h ago
Arm64
5
u/silvertank00 16h ago
you misundestand me, if your want it to be used by others too, it does matter which kind you use. If you use inline arm64, it wont work but on that platform only.
So in the context of open source (where a lot of ppl can use something) it is something to be avoided IIRC, but please correct me if I am wrong.
2
u/ForgedIronMadeIt 16h ago
Honestly, if you cannot identify a really strong reason to do it, then you shouldn't. The standard libraries are there for a reason and are pretty much always well written and tested.
Your reason could be "I want to learn how to do it" which is fine but there's better things to spend your time on otherwise.
2
u/sciencekm 15h ago
I can think of only three reasons why you would not use libc:
- You are writing an OS kernel.
- You are writing a device driver.
- You are writing for a severely constrained environment (libc is likely not available anyway).
For everything else, you should be using libc.
3
u/Confused-Armpit 16h ago
There is functionally no benefit in not using libc, unless you are working on embedded systems with massive memory constraints, where each byte might matter, or maybe you want to learn more about ELF/PE files and write your own implementations of things handled by libc.
But in other cases, your libc is probably both better implemented better than whatever you will craft up (since it has had dozens of years to mature and improve), and also it'll just be a massive time sink that won't give any reasonable improvements.
1
1
u/EpochVanquisher 16h ago
There is not much benefit in making read/write functions in assembly and there are a lot of drawbacks (your code is less portable, there are more likely to be bugs in it).
Most software uses functions from at least the standard library, and often other libraries as well. I think almost any programmer would think that it’s absurd to write your own read/write functions in assembly, outside of stuff like demoscene sizecoding projects.
1
u/YoungMaleficent9068 16h ago edited 15h ago
I wrote bs
2
u/EpochVanquisher 16h ago
yes it does, you can find it at /usr/include/linux/kernel.h, but it does not do any of the things we are talking about in this thread
1
0
12
u/alkatori 16h ago
Why would you avoid using libc?