Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Creating and Using Shared Libraries (.so) on Linux

Tech 1

Creating and Using Shared Libraries (.so) on Linux

Overview

A shared library, known as a dynamic linking library in Linux, is a file that can be loaded into memory at runtime rather than being linked during compilation. This approach reduces executable size and enables centralized updates of libraries.

These libraries typically use the .so extension on Linux systems. Unlike static libraries, shared libraries allow multiple programs to utilize the same memory space for the library code, thus optimizing resource usage. Common system libraries like libc, libpthread, and libssl are implemented as shared libraries.

Building a Shared Library

To create a shared library from source files using GCC, follow these steps:

# Example directory structure
# --sodir
#   |-- libsource.c
#   |-- libsource.h
#   |-- libmylib.so

gcc -fPIC -shared -o libmylib.so libsource.c

Key flags used:

  • -fPIC: Generates position-independent code required for shared libraries.
  • -shared: Instructs the linker to produce a shared object instead of an executable.
  • -o: Specifies the output filename.

The naming convention for shared libraries requires a lib prefix followed by the library name and the .so suffix. Deviating from this convention may prevent the system from recognizing the library.

Using a Shared Library

First, define a function in a source file named libhello.c:

#include <stdio.h>

void hello() {
    printf("Hello, world!\n");
}

Compile it into a shared library:

gcc -fPIC -shared -o libhello.so libhello.c

Next, write enother program that uses this library in main.c:

void hello();

int main() {
    hello();
    return 0;
}

Link the shared library when compiling the main program:

gcc -o main main.c -L. -lhello

In this command:

  • -L.: Inndicates the current directory where the shared libray resides.
  • -lhello: Refers to libhello.so, omitting the lib prefix and .so suffix.

When executing the program, you might encounter an error indicating the library cannot be found. To resolve this issue, update the LD_LIBRARY_PATH environment variable:

export LD_LIBRARY_PATH=$(pwd)

Alternative methods include:

  1. Temporarily setting the environment variable.
  2. Adding the path to /etc/profile.
  3. Including the path in ~/.bashrc.
  4. Modifying /etc/environment.
Tags: Linux

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.