STATIC VS DYNAMIC LIBRARIES

STATIC VS DYNAMIC LIBRARIES

A library in a programming language is a collection of pre-compiled routines that a program can use. The routines, sometimes called modules, are stored in object format. Libraries are particularly useful for storing frequently used routines because you do not need to explicitly link them to every program that uses them. In libraries there is the code previously written and it has functions already designed to be used in other files as soon as it is called in their respective header (read more about functions), and the libraries could be used when you are creating and writing your own code and projects.

There are two different types of libraries. The libraries that are defined in the program and you need to use them in the header with the < >. On the other hand, there are the static and dynamic libraries created by the programer and when the importing user defined .h files use quotation marks. As well as, there are two ways to linked a library: statically and dynamically, turning them into either a static library, or a dynamic (shared) library respectively.

Defined by the programer:
#include "absolute or relative path to library"
Defined by the program:
#include <include the library defined>

For example, if you want to use printf() function, it is necessary to use and include the header file <stdio.h>, as well as the example is showing the uses of the holberton.h library created by the programmer:

No hay texto alternativo para esta imagen

?WHY SHOULD YOU USE LIBRARIES?

The principal advantages of using c libraries functions are:

  1. They help to write a efficient programs and shorter code, as well as they are easy to use.
  2. They enable you to do a code more optimized for a maximum performance. It saved time!!
  3. The libraries functions help you in do the same thing and functions on every computer, and every devices that you are calling them and using them.

?HOW THEY WORK AND HOW TO USE THEM?

Libraries consist of a set of related functions to perform a common task; for example, the standard C library, ‘libc.a’, is automatically linked into your programs by the “gcc” compiler and can be found at /usr/lib/libc.a. Standard system libraries are usually found in /lib and /usr/lib/ directories. Check out those directories. By default the gcc compiler or more specifically its linker needs to be directed to which libraries to search other than the standard C library - which is included by default.

There are a number of conventions for naming libraries and telling the compiler where to find them that we will discuss in this lecture. A library filename always starts with lib. The last part of the name determines what type of library it is:

.a: static, traditional libraries. Applications link to these libraries of object code.

.so: dynamically linked shared object libraries. These libraries can either be linked in at runtime but statically aware or loaded during execution by the dynamic link loader.

STATIC VS DYNAMIC LIBRARIES: differences and advantages

  • Static libraries are usually faster than the shared libraries because a set of commonly used object files is put into a single library executable file. One can build multiple executables without the need to recompile the file. 
  • Shared libraries are .so (or in Windows .dll, or in OS X .dylib) files. Static libraries are .h
  • A Static library or statically-linked library is a set of routines, external functions and variables which are resolved in a caller at compile-time and copied into a target application by a compiler
No hay texto alternativo para esta imagen

?HOW TO CREATE AND USE A LIBRARY?

?How to create and use a static library?

To create your own library it is important to follow the below steps:

  1. Create an interface of your library: mylib.h and create and the implementation of it: mylib.c

How should be a .h file looks like:

#ifndef _MYLIB_H_
#define _MYLIB_H_

    // a constant definition exported by library:
    #define MAX_FOO  20

    // a type definition exported by library:
		struct foo_struct {  
        int x;
        float y;
    };
    typedef struct foo_struct foo_struct;

    // a global variable exported by library
    // "extern" means that this is not a variable declaration, it 
    // just defines that a variable named total_foo of type int
    // exits and you can use it (its declaration is in some library source file)
    extern int total_foo; 	

    // a function prototype for a function exported by library:
    extern int foo(float y, float z);   // a very bad function name

#endif

2. Create a library object file that can be linked with programs that want to use our library code.

How should be a .h file looks like:

 #include "mylib.h"

    ...
    int total_foo;

    int foo(float y, float z) { 
	...
    }

3. Create the shared object file: .o files that can be linked with programs that want to use your library code.

gcc -o mylib.o -c mylib.c

or 

$ gcc -Wall -c *.c

4. Creating a library file:

$ ar -cvq libtseutil.a *.o
q - dictionary.o
q - file.o
q - hash.o
q - html.o

5. Then you should listing files in a library:

$ ar -t libtseutil.a
dictionary.o
file.o
hash.o
html.o

6. Finally you could use the library in the other code #include "mylib.h" and link in the library code.

?How to create and use a dynamic library?

1.To create a dynamic library using all the *.c files in our current directory. To start it is necessary to compile the .c files into .o object code and make them position independent:

$ gcc -Wall -pedantic -Werror -Wextra -fPIC -c *.c

2. Creating our dynamic library and add files to it, using the below commands:

$ gcc -shared -o liball.so *.o

To create a dynamic library, also could be done using the next line of code:

$ gcc -g -fPIC -Wall -Werror -Wextra -pedantic *.c -shared -o liball.so

3. After that, it is needed to verify if the shared library created contains all of the necessary dependencies. We can check that by running the command:

$ gcc -Wall -pedantic -Werror -Wextra -L. 0-main.c -lholberton -o len
$ ldd len
No hay texto alternativo para esta imagen

4. If the location is “not found” that means that is missing dependencies. It must added the location of the dynamic library to the environmental LD_LIBRARY_PATH:

export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH
No hay texto alternativo para esta imagen

To use this dynamic library (liball.so) need to create a program — let’s call it program.c. The program need to be compiled the same way we did a static library:

gcc -lliball -L. program.c -o program

After having created this new shared library, we can use the ldconfig command to create the necessary links and cache (for use by the run-time linker, ld.so) to the /etc/ld.so.conf directory.


References

https://medium.com/meatandmachines/shared-dynamic-libraries-in-the-c-programming-language-8c2c03311756

https://hackernoon.com/static-and-dynamic-libraries-fe5d23daffe3

https://www.webopedia.com/TERM/L/library.html

https://www.youtube.com/watch?v=-vp9cFQCQCo&t=429s

https://www.geeksforgeeks.org/difference-between-static-and-shared-libraries/


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

Maria Alejandra Coy Ulloa的更多文章

  • Implementation from Scratch: Forward and Back Propagation of a Pooling Layer

    Implementation from Scratch: Forward and Back Propagation of a Pooling Layer

    You could find the implementation of the code for the forward propagation here and the backpropagation here A…

    1 条评论
  • Forward and Back Propagation over a CNN... code from Scratch!!

    Forward and Back Propagation over a CNN... code from Scratch!!

    The name “convolutional neural network” indicates that the network employs a mathematical operation called convolution.…

    5 条评论
  • Transfer Learning using Keras

    Transfer Learning using Keras

    The transfer learning is a technic based on how the human being acquires knowledge or gain while learning about one…

  • ???WEB POSTMORTEM!!!

    ???WEB POSTMORTEM!!!

    Do not get panic!!! Let′s get into the postmortem style The key to learning from our mistakes is to document our…

  • What Happens When You Type an URL in Your Browser and Press Enter?

    What Happens When You Type an URL in Your Browser and Press Enter?

    The internet has became a part of our lives, and typing the URL in the browser of Google and search for a websites is a…

  • The Internet of the Things - IoT

    The Internet of the Things - IoT

    Basically, the Internet of Things is actually a pretty simple concept, it means taking all the things in the world and…

  • HEY Grandma... Do not worry, Artificial Intelligence is easy!

    HEY Grandma... Do not worry, Artificial Intelligence is easy!

    Artificial Intelligence is applied when a machine starts to mimic the behavior of the human. The constant development…

  • PYTHON 3

    PYTHON 3

    Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. With Python it is…

  • LIBRARIES IN A COMPUTER LANGUAGE

    LIBRARIES IN A COMPUTER LANGUAGE

    In a library there is the code previously written and it has functions already designed to be used in other files as…

  • Compiling a C file using gcc

    Compiling a C file using gcc

    To Compiling c programs in Ubuntu using the compiler command gcc we need to follow the next stops: We first need to…

社区洞察

其他会员也浏览了