Linking in Linux

Linux like Windows also supports all the linking of Windows. We shall discuss all the topics one by one briefly.

linux linking

Static Linking in Linux

Static linking is the process of linking an object code directly to the executable code of the application during linking or building time of the executable. Static linking is a compilation / build time process. Let us consider mathlib.c file. We want to make it as a static library.
First we compile it with position independent flag on(-fPIC). This is needed for dynamic/static linking.

Linux library project

linux-mathlib-source

Linux library source

int add (int a, int b)
{
  return (a + b);
}

int sub (int a, int b)
{
  return (- b);
}

int mul (int a, int b)
{
  return (* b);
}

int div (int a, int b)
{
  return (/ b);
}

Linux library header

int add (int, int);
int sub (int, int);
int mul (int, int);
int div (int, int);

Linux static library

$cc -fPIC -c mathlib.c

Now make a archive or static lib with the object file.
$ar rc libmath.a mathlib.o

Linux library application

linux-lib-app

#include <stdio.h>
#include "mathlib.h"

int main (int argc, char *argv[])
{
  int result;
  result = add(1, 2);
  printf ("1 + 2 = %d\n", result);
  result = sub(3, 2);
  printf ("3 - 2 = %d\n", result);
  result = mul(3, 2);
  printf ("3 * 2 = %d\n", result);
  result = div(4, 2);
  printf ("4 / 2 = %d\n", result);
  return 0;
}

To use this static library in a application we need to do the following steps:

  1. compile the application code (place math.h in include folder)
    $cc -c libapp.c -Iinclude -o app.o
  2. Link with static library math.a
    $ld app.o libmath.a -o app
  3. Run the application
    $./app

Dynamic Linking

Dynamic linking is the process of linking a library function at the time of loading the application or during the runtime of the application. In contrast to static linking this happens during execution. Dynamic linking is divided into two categories - "Implicit dynamic linking" and "Explicit dynamic linking".

Implicit Dynamic Linking

Implicit dynamic linking is the process of linking a function from a shared library during the loading of the application. Let us consider once again mathlib.c file for dynamic linking. We want to make it as a dynamic library. First we compile it with position independent flag on(-fPIC). This is needed for dynamic/static linking.
$cc -fPIC -c mathlib.c

Now make a shared library with the object file.
$cc -shared libmath.so mathlib.o

To use this shared library in a application we need to do the following steps:

  1. compile the application code (place math.h in include folder)
    $cc -c libapp.c -Iinclude -o app.o
  2. Link with import library math.lib
    $ld app.o -lmath -o app
  3. Copy the libmath.so in lib path or current path and run the application
    $./app

Explicit Dynamic Linking

Explicit dynamic linking is the process of linking a function from a shared library during the runtime of the application using verious helper function of the dynamic loader. Let us consider once again math.c file for explicit linking. The steps for creating a shared library are same as that of implicit linking.
First we compile it with position independent flag on(-fPIC). This is needed for dynamic/static linking.

$cc -fPIC -c mathlib.c

Now make a shared library with the object file.
$cc -shared libmath.so mathlib.o

To use this shared library in a application we need to load the library then find the function pointer address, invoke the function, and at last unload the library.
Linux provides some dynamic link/load library APIs to achieve this. Her are some useful frequently use APIs:
  • dlopen() - loads a dynamic link binary
  • dlsym() - returns the function pointer if found the function entry
  • dlclose() - unloads the dynamic link binary

Linux Explicit library App project

linux-mathapp-source

#include<stdio.h>
#include<dlfcn.h>

typedef int (add_func) (int a, int b);
void *lib_handle = NULL;
add_func * add;
int main (int argc, char *argv[])
{
  int a = 1, b = 2, c;
  lib_handle = (void *)dlopen("./libmath.so", RTLD_LAZY); 
  if(lib_handle) 
  {
    printf("dlopen returns %p\n", lib_handle);
    add = dlsym(lib_handle, "add");
    if(add)
    {
      printf("dlsym returns add = %p\n", add);
      c = add (a, b);
      printf("%d + %d = %d\n", a, b, c);
    }
    else
    {
      printf("Function entry not found in DLL");
    }
    dlclose(lib_handle);
  }
  else
  {
    printf("Unable to open DLL");
  }
}



Explicit SO, App build and run

linux-mathapp-source

About our authors: Team EQA

You have viewed 1 page out of 27. Your DLL learning is 0.00% complete. Login to check your learning progress.

#