Before starting this task I want to say something about "Shared Libraries Pros & Cons"
– 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.
– Incompatible versions
– DLL stomping
– Incorrect COM registration
– Shared in-memory modules
– Lack of serviceability
in few word make a serch "DLL Hell"
Now let's start building our shared library. With a bit of fantasy we are going to build an helloworld library.
The source code
1 #include <stdio.h>
2 void printHelloWorld(void)
4 printf("Hello World!\n");
1 void printHelloWorld(void);
1 #include "helloworld.h"
2 int main(int argc, char **argv)
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
helloworld.c helloworld.h helloworld.o libhello.so main.c mainsl
Launch the mainsl application