Software Libraries & Developers

What is behind it?

Sometimes the software developers have "divine inspiration" and create beautiful code in its preferred language; sometimes there are functions which are used multiple times and in order to time's optimization is better to reuse the existing code; sometimes other smart developers have coded nice functions and cordially have delivered it to us as public code.

In all the above-mentioned situations the better way to share these functions is by means of libraries. According to web Techopedia, a software library is a suite of data and programming code that is used to develop software programs and applications.

Types of Software Libraries

In C language & UNIX environment, we can find two different kinds of libraries, the statics, and the dynamics. The first one (static libraries) are a collection of object-type files that are linked to the main program during the compilation phase; the other one is the dynamic libraries which also are a collection of object-type files which main difference with static is that are not linked to the main program but are referenced to it. Let's explain in detail the differences between these two types of libraries.

Advantages & disadvantages

As the Static libraries are linked to object code in the linking process, the size of executable code would growth as many static libraries be called in the main program, and sometimes the memory resources are critical. Another disadvantage of the static libraries that in case of doing an update of the libraries is necessary to update also in the executable main file. This implies a great amount of time and effort on the part of the end user.

Both issues are solved with dynamic libraries. Topic related to size is sorted out when the library code is not part of the main program but as a call to a memory location. The other one is solved by the self-structure since being separate codes the update process is also a separated process.

How to create Dynamic Libraries (in UNIX)

Let's describe step-by-step the creation of a dynamic library with 2 functions inside it.

a) Functions.

Our library will have two functions inside it. The is_lower.c and the is_upper.c.

No hay texto alternativo para esta imagen
No hay texto alternativo para esta imagen

b) Compilation as Position Independent Code (PIC)

The position-independent code (PIC) is a body of code that, being placed somewhere in the memory. PIC is commonly used for shared or dynamiclibraries oordynamicc shared libraries, so that the same library code can be loaded in a location in each program address space where it will not overlap any other uses of memory (for example, other shared libraries).

With the compiler gcc, we generate the obj file for each one of these functions. After it, we generate the dynamic library with the following commands:

$gcc -fPIC -c _islower.c
$gcc -fPIC -c _isupper.c

Now, its time to generate the dynamic library with the following commands. In UNIX system the file extension for these libraries is .so while in Windows the extension is .dll

$gcc -shared -o libholberton.so _islower.o _islower.o

In order to verify the creation of the shared library, with the following commands it is possible to identify it. The command nm list symbols of an object file; the parameter -D is for dynamic.

julien@ubuntu:~/0x17. Dynamic libraries$ nm -D libholberton.so 
0000000000000c25 T _islower
0000000000000c46 T _isupper
                 U write

3) Using the Dynamic library.

With the dynamic library libholberton.h we can compile a code in C which requires the functions in the library. Per example the main.c

int main ()
{
int a;

string = "Hola como estas";
a = _islower (string);
b = _isupper (string);
return (a * b)
}

$gcc -o main main.c libholberton.so 

In order to use the dynamic library out of working directory, we can set up the Global Variable in this way,

export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH

Conclusion

Dynamic libraries are the most efficient way to generate libraries and in this way to reuse code in the same program or another one.



要查看或添加评论,请登录

Abdel Giovanny Perez的更多文章

社区洞察

其他会员也浏览了