dynamic and static libraries small peack into them

dynamic and static libraries small peack into them

Static Library :

A static library is a programming concept in which shared libraries with special functionalities, classes or resources are linked to external applications or components, facilitating the creation of stand-alone and executable files. During linking, a static library's external parts are loaded into the address space or merged with application code

Dynamic Library :

A dynamic library is a programming concept in which shared libraries with special functionalities are launched only during program execution, which minimizes overall program size and facilitates improved application performance for reduced memory consumption. In most software programs, distributing specific functionalities into distinct modules allows loading as needed.

When to use Dynamic linking and static linking:

Aucun texte alternatif pour cette image

The operating system provides facilities for creating and using dynamically linked shared libraries. With dynamic linking, external symbols referenced in user code and defined in a shared library are resolved by the loader at load time. When you compile a program that uses shared libraries, they are dynamically linked to your program by default. The idea behind shared libraries is to have only one copy of commonly used routines and to maintain this common copy in a unique shared-library segment. These common routines can significantly reduce the size of executable programs, thereby saving disk space.

In statically-linked programs, all code is contained in a single executable module. Library references are more efficient because the library procedures are statically linked into the program. Static linking increases the file size of your program, and it may increase the code size in memory if other applications, or other copies of your application, are running on the system.

how to create a static Library :

1. Create a C file that contains functions in your library.

Aucun texte alternatif pour cette image

We have created only one file for simplicity. We can also create multiple files in a library.

2. Create a header file for the library that contains all the prototypes .

Aucun texte alternatif pour cette image

3. Compile library files.

Aucun texte alternatif pour cette image

4. Create static library. This step is to bundle multiple object files in one static library (see ar for details). The output of this step is static library.

Aucun texte alternatif pour cette image

how to create a Dinamic library :

To create a dynamic library in Linux, simply type the following command:

  gcc -c -fPIC *.c

This command essentially generates one object file .o for each source file .c . The -fPIC flag ensures that the code is position-independent. This means it wouldn’t matter where the computer loads the code into memory. Some operating systems and processors need to build libraries from position-independent code so that they can decide at runtime where they want to load it into memory. The -c options just ensures that each .o file isn’t linked yet.


Next, type in the following command: 

gcc *.o -shared -o

liball.so (substitute your desired library name with all) and hit return. The wildcard * tells the compiler to compile all the .o files into a dynamic library which is specified by the -shared flag. The naming convention for dynamic libraries is such that each shared library name must start with lib and end with .so . Other than that though, let your imagination run free when considering names for your dynamic libraries.

Finally, we’ll need to export the path for libraries so that programs know where to look for them by executing the following command: 

export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH

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

Jaafar Fares的更多文章

  • ?? Unveiling Staffy: My Stellar Graduation Project as a Software Engineering Graduate! ??

    ?? Unveiling Staffy: My Stellar Graduation Project as a Software Engineering Graduate! ??

    As I proudly take the stage as a recent Software Engineering graduate, I'm thrilled to share a major milestone in my…

  • ?? 10 Best Free APIs to Enhance Your Apps ??

    ?? 10 Best Free APIs to Enhance Your Apps ??

    API's play a crucial role in empowering Devolopers in their journeys. These effective equipment offer a gateway to get…

  • STEM

    STEM

    what is STEM: STEM is an approach to learning and development that integrates the areas of science, technology…

  • what happens when you type https://www.google.com

    what happens when you type https://www.google.com

    Every day you open up your browser and navigate to your favorite websites — whether it be social media, news, or…

  • Internet of Things

    Internet of Things

    The internet of things or (IoT) : is a system of interrelated computing devices, mechanical and digital machines…

  • the concept of recursion

    the concept of recursion

    Recursion definition: Recursion is a process in which a function calls itself as a subroutine. This allows the function…

  • In Python Everything is an Object

    In Python Everything is an Object

    in Python everything is an object : >>> Classes are objects, >>>instances of classes are objects, >>>modules are…

  • Python how Object and class attributs work how they Works

    Python how Object and class attributs work how they Works

    lets define an empty class called Dog: Classes contain characteristics called Attributes. We make a distinction between…

  • integers are stored in memory using two’s complement

    integers are stored in memory using two’s complement

    Integers are whole numbers which will be stored in computer using 4 bytes (32 bit) of memory. for example : Binary…

  • Worst abuse of the C preprocessor 1986

    Worst abuse of the C preprocessor 1986

    What’s IOCCC? The International Obfuscated C Code Contest is an annual programming contest with a obfuscated C code. In…

社区洞察

其他会员也浏览了