The wonders of chroot

In this article I explain how to use chroot to switch into a different filesystem and how to use it together with qemu to switch the architecture too.

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




C static libraries

When a C program is compiled, the compiler generates object code. After generating the object code, the compiler also invokes linker. One of the main tasks for linker is to make code of library functions (eg printf(), scanf(), sqrt(), ..etc) available to your program. A linker can accomplish this task in two ways, by copying the code of library function to your object code, or by making some arrangements so that the complete code of library functions is not copied, but made available at run-time. [1]

Static libraries are not required at runtime, so you do not need to include them when you distribute your executable. At compile time, linking to a static library is generally faster than linking to individual source files. [2]

[3]

Let’s suppose that in our application code, we are using a function from another library, in the static linking, when we compile our program, the object code (binary code) of the function’s library will be included in the finally generated executable file. [4]

For example, we have the header file called stclib.h that contains:

And we have the c files of those functions.

We compile:

Where:

a. “GNU Compiler Collection”.

b. Special flags that sow us error and warning (see more in the man.).

c. Compile or assemble the source files, but do not link, the generated files will end with .o.

d. Do the above described with all the files whose name ends in .c.

The basic tool used to create static libraries is a program called 'ar', for 'archiver'. This program can be used to create static libraries (which are actually archive files), modify object files in the static library, list the names of object files in the library, and so on. In order to create a static library, we can use a command like this: [5]

This command creates a static library named ‘libstc.a’ and puts copies of the object files that ends with “.o” in it. If the library file already exists, it has the object files added to it, or replaced, if they are newer than those inside the library. The 'c' flag tells ar to create the library if it doesn't already exist. The 'r' flag tells it to replace older object files in the library, with the new object files.

After an archive is created, or modified, there is a need to index it. This index is later used by the compiler to speed up symbol-lookup inside the library, and to make sure that the order of the symbols in the library won’t matter during compilation (this will be better understood when we take a deeper look at the link process at the end of this tutorial). The command used to create or update the index is called 'ranlib', and is invoked as follows: [5]

For example if we want to use the libstc.s library in the main.c file.

Note that we omitted the “lib” prefix and the “.a” suffix when mentioning the library on the link command. The linker attaches these parts back to the name of the library to create a name of a file to look for. Note also the usage of the '-L' flag - this flag tells the linker that libraries might be found in the given directory ('.', refering to the current directory), in addition to the standard locations where the compiler looks for system libraries. [5]

And the you execute your file that includes the library code and the main.c code:

[2] https://medium.com/@meghamohan/everything-you-need-to-know-about-libraries-in-c-e8ad6138cbb4

Add a comment

Related posts:

2K x Lego

Lego is partnering with 2K to bring gamers a range of Lego sports games, the gaming publisher has just signed a multi-game partnership with the global toy company themselves! This has come as quite a…

Wake and sleep and wake again

Wake and sleep and wake again, every night was broken into pieces by the rough hands of her tormentors, and every night was colder and crueler than the night before. Then I went to Blawan with…

Why Most valuable brands of Sri Lanka are from the Banking industry?

Consecutively once again this proved that word “Brand” is likely to equal the word “Trust” which is the most-searched criteria in customer decision of banking. Rationally Trust towards bank’s every…