Making and Using Dynamic Libs (C)

Making and Using Dynamic Libs (C)

In order to explain the significance of using dynamic libraries, I’ll need to define some terms. First of all, a function is a named section of a program that does a specific task. Functions can be used throughout the program to save time by employing aforementioned function to perform a task as opposed to rewriting that specific block of code when you want to do the task again later in the program. Although static libraries can be used by multiple programs, once the program has been compiled, it is no longer possible to change the different pieces of object code in an executable file because all the code is contained within a single file that was statically linked when the program was compiled.

This finally brings us to dynamic or shared libraries. Unlike static libraries, dynamic libraries consist of separate files containing separate pieces of object code. These files are dynamically linked together to form a single piece of object code. Furthermore, dynamic libraries contain extra information that the operating system will need to link the library to other programs.

The implication of this is that memory is conserved while using dynamic libraries since each application or program can access the dynamic library without needing an individual copy, as would be the case, if we were using static libraries. Although dynamic libraries afford the ability to alter source code without recompiling the entire program, static libraries’ execution speed at run-time is faster because the object code for the functions within the library are already in the executable file. As a result, multiple calls to functions are handled more efficiently than when using dynamic libraries.

How to Create a Dynamic Library (Linux)

To create a dynamic library in Linux, simply type the following command: gcc *.c -c -fPIC and hit return. 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

Using Dynamic Libraries (Linux)

The point of creating a dynamic library is to use it with other programs. You can compile your code as follows:


gcc -L test_code.c -lholberton -o test_code

In the above command it is worth noting that your source code, test_code.cin this case, needs to be listed before the -l flag. The expression, -lcombined with holberton tells the compiler to look for a dynamic library called libholberton.so, while the -L flag tells the compiler to look in the current directory for the library file. This is why it is important to use the standard format for naming that I described earlier. For instance if test_code.c was the following:

#include "holberton.h"

int main(void)
{
      _puts("Hello World!");
      return (0);
}

Typing and executing gcc -L test_code.c -lholberton -o test_code would generate an executable file called test_code. In order to accomplish this, the compiler looks through the library that is specified with the -l flag for the _puts function object code. Executing test_code like so: ./test_code would give us the following output: Hello World!. Now that you know how to create and use dynamic libraries, go and conquer the world!

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

Duvan Rodelo的更多文章

  • Ethical Challenges in AR / VR

    Ethical Challenges in AR / VR

    One of the big areas of ethical concerns surrounding augmented reality and virtual reality is privacy. These devices…

  • What happens when you type holbertonschool.com in your browser and press Enter

    What happens when you type holbertonschool.com in your browser and press Enter

    When you type an URL such as https://www.holbertonschool.

  • Internet Of Things

    Internet Of Things

    What is the Internet of Things? The Internet of Things, or IoT, refers to the billions of physical devices around the…

  • How recursion works & .... other stuff !

    How recursion works & .... other stuff !

    Recursion. The word alone brings back some painful and confused memory to every CS dev.

  • All stuff are Objects

    All stuff are Objects

    The first time when I hear about Pyhton , the message was "Python is dangerous". But in the coding way, like in the…

  • SCRUM Basics

    SCRUM Basics

    Whats a SCRUM ?, basically is team environtment to define objetives and personal contributions to a project…

  • What happens when you type ′ls -l*.c′ on your shell

    What happens when you type ′ls -l*.c′ on your shell

    What is a shell? First, let us define what is a shell. By Marrian-Webster (MW) a shell is: A hard rigid usually largely…

  • Static Libraries in C

    Static Libraries in C

    What are C libraries? One of the tools that compilers supply C programmers. A library file contains a collection of…

  • FANTASTIC 4 TO COMPILE IN C.

    FANTASTIC 4 TO COMPILE IN C.

    About the compiling language we have C. And you also know how to write a C program.

社区洞察

其他会员也浏览了