Differences between static and dynamic libraries
https://www.nlb.gov.sg/VisitUs/BranchDetails/tabid/140/bid/337/Default.aspx?branch=library%40orchard

Differences between static and dynamic libraries

A library is a powerful tool in a programmer's arsenal, consisting of pre-compiled functions that can be easily reused throughout a script. These functions are blocks of code that save time and effort by eliminating the need for repeated coding. In fact, leveraging existing libraries can significantly boost a developer's productivity.

Typically, object files are generated by the compiler when the "-c" gcc flag is used, and they follow a naming convention ending with ".o". These files contain function definitions in binary form and are used to build libraries.

At runtime or compile time, libraries play a critical role in software development. In C programming, two types of libraries are available: dynamic libraries and static libraries. By selecting the right type of library for a particular application, developers can optimize their code for performance, scalability, and maintainability.

Static vs Dynamic

Static libraries are compiled into a program, with a copy of the library's files held within the executable file. This results in a larger file size as the external programs are compiled as well. However, all components are stored within the executable file, making it resistant to vulnerabilities. Additionally, the object code being in the executable file results in faster runtime speed since function calls are executed more quicker.

On the other hand, dynamic libraries occur as separate files outside of the executable file. This results in a much smaller library size and allows multiple running applications to use the same library, providing significant advantages. Furthermore, dynamic libraries are linked at runtime, meaning that changes can be implemented without recompilation or relinking. However, since the library is outside the executable file, it is vulnerable to breakage, which would render the executable file unusable.

In summary, while static libraries offer security and faster runtime speed, dynamic libraries provide more flexibility and better resource management.

CREATION

First, is needed to have all the .c files and the header file in the current working directory or at least in a known and accessible directory.

Dynamic library creation (Linux only)

Write the command listed below to generate the object files (step 1):

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

Next, create the library by using.

gcc *.o -shared -o liball.s        

As an alternative, it's possible to approach this in a single line by implementing the next line of code.

gcc -fPIC -Wall -Werror -Wextra -pedantic *.c -shared -o liball.s

o        

  • -fPIC flag: The generated machine code is not dependent on being located at a specific address in order to work.
  • -shared flag: Produce a shared object which can then be linked with other objects to form an executable file (the files are prepared to be used in the dynamic library).
  • *.c: Implementing wildcard to select all the .c files contained in the cwd.
  • o: Specifically refers to creating an executable file as output.
  • liball.so: Name of the executable file, that by convention must end in.so

The?compiler?will later identify a library by searching for files beginning with?'lib'?and ending with the naming convention,?.so

The program needs the path in order to look for the library files. So, is needed to type the next command to add that location to the environment variable called?LD_LIBRARY_PATH.

export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH        

Or like an alternative:

export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH        

This can be made also by sending the created library file to /usr/lib folder like as follow:

cp liball.so /usr/lib/        

But is highly recommended to use the first or the second way.

Static library creation (Linux only)

You can follow deeper the flow to create static libraries in the next resource:

https://www.dhirubhai.net/pulse/c-static-libraries-juan-sebastian-gonzalez

Creating a static library is much easier.

  1. create the object files the same way as step 1 above.
  2. Next, use the generated objects in the command as follow.

ar rcs liball.a *.o        

Program compilation

gcc -L. -lall -o app main.c        

When compiling program files, is mandatory to tell the compiler to use the library files and where to find them.

  • In this case ‘-l’ flag tells it we want to include library files. And ‘all’ tells it to look for the library liball.so. It’s important to leave the ‘lib’ and ‘.so’ out of the flag because of the compiler.
  • The?-L?flag?accesses the?linker?so the libraries can be found in the given directory. Also, it also searches for other locations where the compiler looks for system libraries. already identifies library files that way.

With this short explanation, I hope you have a better understanding of static and dynamic libraries.

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

Juan Sebastian Gonzalez的更多文章

  • Recursion

    Recursion

    What is it? Essentially is a technique that split a situation or a problem into shorter pieces of itself. Take a look…

  • Mutable, Immutable... everything is an object! O.o

    Mutable, Immutable... everything is an object! O.o

    In Python, everything is an object, which means every entity has some metadata (called attributes) and associated…

  • How integers are stored in memory using two’s complement

    How integers are stored in memory using two’s complement

    A computer is an amalgamation of hardware and software, where digital memories store information in bits (short for…

  • The worst abuse of the C preprocessor (IOCCC winner, 1986)

    The worst abuse of the C preprocessor (IOCCC winner, 1986)

    obfuscation is the deliberate act of creating machine code that is difficult for humans to understand. This article is…

  • C static libraries

    C static libraries

    This article is about exploring in C what are the static libraries, why should we use them, how they work, how can be…

  • Soft Link Vs. Hard link

    Soft Link Vs. Hard link

    Understanding the difference between a hard link and a symbolic link requires knowledge of how computers store and…

  • Compilation Process using in programs like C.

    Compilation Process using in programs like C.

    In programming, there are two types of languages, which are interpreted and compilated. The code of compiled language…

社区洞察

其他会员也浏览了