Wednesday, June 6, 2012

[Linux] Understanding Shared Libraries

Have you ever wondered how shared libraries work? If you wondered and wanted some good information about it you can check out this website here. But how does shared libraries work with say a header file and maybe a main .cpp program instead of just c files? Well I just learnt how to do this and I thought I would share it with you. There are 3 files that I will list the code for and tell what each one is for. The first one is a header file called programmerFunction.h and it's affiliated .ccp file called programmerFunction.cpp. The last one is the main program called userProgram.cpp.

---------------- programmerFunction.h------------------
//This header defines the programmer function which just returns
//A value for an integer.
#ifndef PROGRAMMERFUNCTION_H
#define PROGRAMMERFUNCTION_H
#ifndef _cplusplus
extern "C"
{
#endif
void returnInteger(int*);
#ifndef _cplusplus
}
#endif
#endif
------------------------------------------------------------------------

--------------- programmerFunction.cpp------------------
//Here is the actual code to return an integer.
//As you can see nothing fancy
#include "programmerFunction.h"
void returnInteger(int * eger)
{
*eger=58008;
}
#endif
------------------------------------------------------------------------

------------------- userProgram.cpp ----------------------

//Here is the main program which just includes the
//The programmer defined function and prints out
//The value of the function
#include < stdio.h >
#include "programmerFunction.h"
int main()
{
int eger;
returnInteger(&eger);
printf("This is the value in eger: %d\n",eger);
return 0;
}

------------------------------------------------------------------------

As you can see there is nothing really to these programs and it was designed for simplicities sake.
First thing we have to do is turn the programmerFunction.cpp into an object file by typing in this command:

" g++ -Wall -fPIC -c programmerFunction.cpp "

This creates the object file that will enable us to create our shared library from.

Now in order to create a shared library we are going to enter in this command:
" g++ -shared -Wl,-soname,libmylib.so.1 -o libmylib.so.1.0 programmerFunction.o "

This creates our shared object file called libmylib.so.1.0. From here we are going to move in into this path (or another suitable path of your choice) which is /opt/lib/.

" sudo mv libmylib.so.1.0 /opt/lib/. "

The next thing we have to do is create two soft links that point to the libmylib.so.1.0 shared library.

" sudo ln -sf /opt/lib/libmylib.so.1.0 /opt/lib/libmylib.so "
" sudo ln -sf /opt/lib/libmylib.so.1.0 /opt/lib/libmylib.sh.1 " and
" sudo ln -sf /opt/lib/libmylib.so.1.0 /opt/lib/libmylib.so.1 "

Next we are going to compile our userProgramm.cpp and link our shared library with it.

" g++ -Wall -L/opt/lib userProgram.cpp -lmylib -o compiledProgram "

Now in order for the program to run we need to export the path of /opt/lib to the environment variable
LD_LIBRARY_PATH. We will do this by entering this command:

" export LD_LIBRARY_PATH=/opt/lib:$LD_LIBRARY_PATH "

Now finally to run our program and get it's output.

" ./compiledProgram "

After you run this command you should get the number " 58008 ".

So after you have done all this why would we want to create a shared or dynamic link? Well for such a small program we may not actually need it but to show the power of a shared library we will change the value of the integer without having to recompile our main program. This is what makes shared libraries powerful. You can update the shared libraries without having to redo massive compilations and might actually end up saving you time in the end.
Lets begin by changing our programmerFunction.cpp. change the value eger by replacing it with the value of " 123456 ".

Now run these commands:

" g++ -Wall -fPIC -c programmerFunction.cpp "
" g++ -shared -Wl,-soname,libmylib.so.1 -o libmylib.so.1.0 programmerFunction.o "
" sudo mv libmylib.so.1.0 /opt/lib/. "

What this effectively does is recompiles your programmerFunction.cpp file into a new object file. We also recreate the shared library and replace your old shared library with the new one. Now rerun your program by typing in this:

" ./compiledProgram "

You should now see the new number of " 123456 ". If you see this number congrats! You now know a little bit about shared libraries and maybe see some use for them.

Thanks,

Almightybeeij

From OKCLugNuts

No comments: