Dynamic library is a relocatable object file with metadata. Dynamic libraries are linked at run time. They are built from one or more object files ( .o files). In Linux Dynamic libraries are called shared objects and the extension is .so. The name of a Dynamic library should start with lib. For example libtest.so is a valid Dynamic library name.
Creating the Dynamic Library File
Let the name of the C Program file be p1.c,
- Creating object files: Use gcc to create object files as Position independent Code as follows.
gcc -fPIC -c p1.c
- Creating the Shared Object:
Storing & Linking the Static Library File
You need to change the LD_LIBRARY_PATH as follows,
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/newpath
- Store the dynamic library in the standard directory /usr/lib.
Compile & link the dynamic library file with target.c as follows,
gcc target.c -ltest
- Store the static library in a directory you have created.
Compile & link the static library file with target.c as follows,
gcc target.c -L/usr/student/mylib -ltest
- You can also change the LD_LIBRARY_PATH as follows so that the compiler will search even the new path,
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/student/mylib
Now compile & link the dynamic library file with target.c as follows,
gcc target.c -ltest
gcc target.c -ltest
No comments:
Post a Comment