./Libraries
By Juan Calle

./Libraries

For starting to talk about libraries without doubt we have to start talking about ¿what Functions are? , functions are just amounts of code that are reclaimable throughout a program and using them saves time, displacing the fact to rewrite code multiple times. However let's imagine that functions are like some books that we are going to "store" at something that we call Libraries (looks kind of familiar, don't you think ? ) and like functions also save time that they make functions reusable in multiple programs.

Where's the Static Libraries section?

Referring now to the substance of this blog, we have to contrast some main characteristics between Static and Dynamic libraries and their advantages and disadvantages. For static libraries they could be sustainable in multiple programs, are locked into a program at compile time, instead of this, dynamic, or shared libraries, exist as separate files outside of the executable file.

One of the main disadvantages of using a static library is that it’s code is locked into the final executable file and cannot be modified without a re-compile. Over and above, a dynamic library can be modified without need to re-compiling, because dynamic libraries allocates outside of the executable file, the program need only make one copy of the library’s files at compile-time, and multiple running applications can use the same library without the need of having it’s own copy. Whereas using a static library means every file in your program must have it’s own copy of the library’s files at compile-time. Another benefit of using static libraries is execution speed at run-time. Because the code in machine language is already included in the executable file, multiple calls to functions might be handled much more quickly than a dynamic library’s code, which needs to be called from files outside of the executable.

In contrast a disadvantage of using a dynamic library is that a program is much more susceptible to breaking. If a dynamic library for example corrupts, the executable file may no longer work. A static library, however, is untouchable because it lives inside the executable file.

At the end you are going to be concerned about which of both could be better? Well, imagine you’re a the person in charge of a certain program who has been released for thousands of users. When you want to make a few updates to it, would you prefer to re-make the entire program, or would you prefer just releasing updates in the form of modified libraries? The entire answer depends on the downsides your application might afford. If you have a lot of files, multiple copies of a static library means an increase in the executable file’s size. However, the benefits of execution time gain offsets the need to save space, the static library it's the path for following.

“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” –
Martin Fowler


I don't care about Martin Fowler, lets start!

And How do I Create those libraries ?

For dynamic libs the .c source files need to be prepared, since multiple programs can all use one instance of it, the library can’t store data at fixed addresses. This is because the location of the library in memory will vary between programs. This is done by using the compiler flag -fpic. Since we need to apply this step after the compile process has generated the object code, the compiler must be told to halt and return one object file (.o) for each source file. This is done by using the -c flag.

gcc *.c -c -fpic

The object files are now ready to be compiled into a dynamic library. This is done by compiling all of the .o files using by using the -shared flag. Later when compiling program files, the compiler identifies a library by looking for files beginning with ‘lib’ and ending with a library extension (.so for dynamic, .a for static). Therefore it’s important to name a library accordingly.

gcc *0 -shared -o liball.so

Because a program needs to know where to look for library files, we must add that location to the environmental variable LD_LIBRARY_PATH.

And finally for creating a static library is much simpler. First, create the object files the same way as the first step for dynamic libraries above. Then archive the library using ‘ar rcs liball.a *.0’ and your program should include a prototype for each of the functions that exist in your library. If you’re using a header file for these prototypes, make sure to include the name of that file in your other files.




To view or add a comment, sign in

More articles by Juan Fernando Calle Herrera

  • Ubidots NLP Plugin

    At the beginning of the project, the main goal was adding Natural Language Processing capabilities using Ubidots data…

    7 Comments

Insights from the community

Others also viewed

Explore topics