Linux Programming – Part 2: Libraries

Take a look at Linux libraries.

The definition of system-call and libraries are blurred nowadays. So, let’s take a closer look at what differentiates libraries from system-call and vice-versa.

Library and library functions

When we program, there are some functions available to us aside from system-calls. Here are some library functions I can think of at the moment…

  • printf()
  • exit(()
  • strlen()

They are functions stored inside the library, and the process we use them is called ‘linking’. This ‘linking’ process is something we’ll probably learn later…

System-call and library functions

Some library functions use system-call to accomplish their process, while others accomplish it all through the library functions themselves. A library function, printf(), uses the system-call, write(). On the other hand, another library function, strlen(), doesn’t use any system-call.

However, the borderline between the two, library functions and system-call, is blurred. A process that used to be a system-call is now integrated as a library function, and vice-versa. So, an occasion where we really have to tell the difference between the two is when we build a program that interacts with the kernel and the libraries.

Man pages help us see the difference between the two. printf(), for example, has its command and library-function versions. If you want to see the library-function version, you’ll need to type the below command:

man 3 printf

write() also has its command and system-call versions, and if you want to see the latter one, you’ll need to type the below command:

man 2 write

libc

The most important Linux library is the standard C library, known as libc. In Ubuntu, you can find it in the following directly:

/lib/x86_64-linux-gnu

In the directory, you can find a file called libc.so.6. This file composes the deepest core of libc.

image 01: libc

The libc generally used by Linux is GNU libc, known as glibc, and is a GNU product. As I mentioned in the previous post, the Linux kernel is built by Linus, but GNU libc is built by someone else – this is essentially why Linux is the kernel itself, not those external elements.

In the /lib directly, you can find other libraries, such as libm.so.6. Libraries are files, and there are multiple of them. libm.so.6, for instance, accommodates mathematical functions, such as sin() or cos().

image 02: libm

APIs

Since I mentioned system-call and libraries, I need to talk about API (Application Programming Interface). As the name suggests, it is an interface when you program something. For example, the APIs of the C’s library includes functions and macros, while the kernel’s APIs are system-call. In other cases, setting files and Linux commands is also considered as APIs. So, API is mistakenly understood as a term, which includes wide range of definitions.

Leave a Reply