Static & Dynamic Linking
Static Libraries: libtest.a
Name can be anything but should start with ‘lib’
Steps:
- Create source files
- Compile with –c option
- gcc –Wall –c test1.c test2.c
- Create library
- ar –cvq libtest.a test1.o test2.o
- List files in library
- ar –t libtest.a
- Linking the library
- gcc –o main main.c libtest.a
Dynamically linked “Shared Object” libraries: (.so)
This is a two step process
- Create object code
- Create library
- Optional: create default version using a symbolic link
- Commands:
- Compile all files
- gcc –Wall –fPIC –c *.c
- create a .so file with .o files
- gcc –shared –o libtest.so test1.o test2.o
Using standard .so path: /usr/lib/ is a place where .so files are kept and it is predefined to the OS, so it is searched by default.
- Create a soft link in /usr/lib
- ln –s /path/to/.so file /usr/lib/link name
- Compile the main file with the soft link
- gcc –o main main.c –lxxx
To list the dependency
- ldd binary_name
Remark: so name is started with lib and end with .so like libxxx.so
Link is created with the same name in other location and is start with –l ex: -lxxx for above .so file.
Prefix and suffix of xxx are removed by system and while link xxx is used.
PIC stands for “Position Independent Code”
Using user defined path: -L option
use –L option to tell the path where .so is exists
gcc –L /path/to/.so file –o main main.c –lxxx
Note: at compile time gcc search for the included so file, if it exists at predefined path, it automatically links, but if programmer save it at some other place, its path must be specified with –L option at compile time.
For running the binary: there are following ways
either .so is kept at predefined path
or create a soft link of desired .so at predefined path
or set environmental variable LD_LIBRARY_PATH
or define path in ld.so.conf
Static Libraries: libtest.a
Name can be anything but should start with ‘lib’
Steps:
- Create source files
- Compile with –c option
- gcc –Wall –c test1.c test2.c
- Create library
- ar –cvq libtest.a test1.o test2.o
- List files in library
- ar –t libtest.a
- Linking the library
- gcc –o main main.c libtest.a
Dynamically linked “Shared Object” libraries: (.so)
This is a two step process
- Create object code
- Create library
- Optional: create default version using a symbolic link
- Commands:
- Compile all files
- gcc –Wall –fPIC –c *.c
- create a .so file with .o files
- gcc –shared –o libtest.so test1.o test2.o
Using standard .so path: /usr/lib/ is a place where .so files are kept and it is predefined to the OS, so it is searched by default.
- Create a soft link in /usr/lib
- ln –s /path/to/.so file /usr/lib/link name
- Compile the main file with the soft link
- gcc –o main main.c –lxxx
To list the dependency
- ldd binary_name
Remark: so name is started with lib and end with .so like libxxx.so
Link is created with the same name in other location and is start with –l ex: -lxxx for above .so file.
Prefix and suffix of xxx are removed by system and while link xxx is used.
PIC stands for “Position Independent Code”
Using user defined path: -L option
use –L option to tell the path where .so is exists
gcc –L /path/to/.so file –o main main.c –lxxx
Note: at compile time gcc search for the included so file, if it exists at predefined path, it automatically links, but if programmer save it at some other place, its path must be specified with –L option at compile time.
For running the binary: there are following ways
either .so is kept at predefined path
or create a soft link of desired .so at predefined path
or set environmental variable LD_LIBRARY_PATH
or define path in ld.so.conf