Before starting this task I want to say something about "Shared Libraries Pros & Cons"

PROS

– They give to you the ability to split your code in conceptually separated sections. Each section will be loaded only if it is needed.

– The same shared library can be used by different programs, in such a way you can save space (it could be useful on small embedded systems)

– Last you can update a program, by updating the shared library without the unistall->re-install process.


CONS

– Incompatible versions

– DLL stomping

– Incorrect COM registration

– Shared in-memory modules

– Lack of serviceability

in few word make a serch "DLL Hell"

library


Now let's start building our shared library. With a bit of fantasy we are going to build an helloworld library.


The source code

helloworld.c

#include <stdio.h>

void printHelloWorld(void)

{

    printf("Hello World!\n");

}

helloworld.h

void printHelloWorld(void);

main.c

#include "helloworld.h"

int main(int argc, char **argv)

{

    printHelloWorld();

}


Building the library


first compile the source

gcc -fPIC -c helloworld.c 


then build the library, (take care, the name of the library must begin with "lib")

$ gcc -shared -o libhello.so helloworld.o 


Build the application

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

$ ls

helloworld.c  helloworld.h  helloworld.o  libhello.so  main.c  mainsl


Launch the mainsl application

$ mainsl
Hello World!

That's all,
Gg1