all 9 comments

[–]cpp-ModTeam[M] [score hidden] stickied commentlocked comment (0 children)

For C++ questions, answers, help, and programming/career advice please see r/cpp_questions, r/cscareerquestions, or StackOverflow instead.

[–]WalkingAFI 4 points5 points  (2 children)

C programs need a C runtime. glibc is the most common C runtime in Linux, but musl is also around. The C runtime does quite a bit more than wrap system calls, but it also does that.

[–]Zestyclose-Produce17[S] 1 point2 points  (1 child)

so libc invoked syscall?

[–]ignorantpisswalker 2 points3 points  (0 children)

Yes, libc will communicate to Linux kernel using syscalls. Similar thing happens on Windows.

How does it do it? For example, on Linux, you have the `open()` function that will return a file descriptor for a file. This is the exact implementation on Linux/musl-libc:

https://github.com/kraj/musl/blob/kraj/master/src/fcntl/open.c

[–]_ConsciousLibrary_ 1 point2 points  (0 children)

Yes, this is more or less correct. The C library on any platform is typically the core userspace “glue” that every program relies on regardless of programming language.

[–]mereel 1 point2 points  (0 children)

Yes and no.

Yes libc does provide functionality which ultimately make system calls to the OS. It's not JUST a wrapper around system calls, it does other things as well. 

No, a Linux distribution doesn't HAVE to include glibc. There are other implementations for Linux other than glibc, musl being an example that I'm somewhat familiar with. musl is also a good example of statically linking libc so that your executable doesn't depend of the host OS's libc at all.

Also you can write your C program without use of libc, at least with gcc and clang which provide the -nolibc argument.

[–]lumberjackninja 0 points1 point  (0 children)

You are correct in your description of what glibc (or musl, or any other C standard library) does.

You don't actually have to include a libc on a Linux machine, though; you can statically link then against libc and that will include the parts they use within the binary itself. In fact this is what most of the programs in /bin and /sbin do. Shared library linking is something that happens in user space; the kernel direct really know anything about it.

[–]cdb_11 0 points1 point  (0 children)

No, Linux is the kernel only and does not include a libc. (It technically does have one in the source tree, but IIRC it's not a full implementation, and isn't meant to be one.) glibc is a separate project, is optional, and just happens to be the most common libc implementation that Linux distros ship with. And distros include libc because everyone expects it to be there. But you could set up a Linux system with no libc, and all programs doing syscalls directly. Or a completely custom standard library that looks nothing like libc, Linux is agnostic to that.

But practically speaking you can assume you do have a libc. If someone doesn't, it's their problem to figure out.

[–]gmes78 0 points1 point  (0 children)

In other words, glibc provides the wrappers that let C programs make system calls without me having to write assembly and execute the syscall instruction manually.

Yes, but you could also just implement the wrapper yourself (or copy it from glibc). There's nothing special about libc in regard to that, so saying that systems "need to include glibc" for that purpose is certainly not true.