The filename of the dynamic library file under Linux libxxx.so is like, where so is the abbreviation for shared object, which is the target file that you can share.
When a link dynamic library generates an executable file, the code for the dynamic library is not copied to the execution file, but a reference to the dynamic library is recorded in the execution file.
When the program executes, it then loads the dynamic library file. If the dynamic library is already loaded, it does not have to be loaded repeatedly, which saves memory space.
The steps to build and use a dynamic library under Linux are as follows:
- Write the source file.
- Compile a link from one or several source files to generate a shared library.
-L<path> -lxxxthe libxxx.so that is generated by the GCC option link.
- Put libxxx.so into the standard path of the link library, or specify
LD_LIBRARY_PATH , to run the program that links the libxxx.so.
The following examples are explained in detail.
Writing source files
To create a source file: Max.c, the code is as follows:
int max(int n1, int n2, int n3){ int max_num = n1; max_num = max_num < n2? n2: max_num; max_num = max_num < n3? n3: max_num; return max_num;}
Compile the build shared library:
max.c
We'll get libmax.so.
In fact, the above process is divided into two steps, compile and link,-fpic is the compilation option, PIC is Position independent code abbreviation, indicating to generate location-independent code, which is the nature of the dynamic library needs;-shared is a link option. Tells GCC to generate a dynamic library instead of an executable file.
The preceding line of commands is equivalent to:
gcc -c -fPIC max.cgcc -shared -o libmax.so max.o
Writing an interface file for a dynamic library
To let the user know which interfaces are available in our dynamic library, we need to write the corresponding header files.
To create a max.h, enter the following code:
#ifndef __MAX_H__#define __MAX_H__int max(int n1, int n2, int n3);#endif
Test, link dynamic library to generate executable file
Create a max test.c that uses a function, with the following code:
#include <stdio.h>#include "max.h"int main(int argc, char *argv[]){ int a = 10, b = -2, c = 100; printf("max among 10, -2 and 100 is %d.\n", max(a, b, c)); return 0;}
gcc test.c -L. -lmaxGenerates A.out, which -lmax represents the link to be linked libmax.so .
-L.Indicates that the current path is included when searching for the library file to be linked.
Note that if both dynamic and static libraries with the same name exist in the same directory, for example, libmax.so and libmax.a both are under the current path,
GCC will first link to the dynamic library.
Run
Running ./a.out will get the following error prompt.
./a.out: error while loading shared libraries: libmax.so: cannot open shared object file: No such file or directory
Cannot find libmax.so, originally Linux is through the /etc/ld.so.cache file search to link to the dynamic library.
Instead, the /etc/ld.so.cache ldconfig program reads /etc/ld.so.conf the file generated.
(Note that /etc/ld.so.conf it does not necessarily contain /lib and /usr/lib that the ldconfig program automatically searches for both directories)
If we libmax.so add the path to the /etc/ld.so.conf , and then run the program with root, ldconfig update /etc/ld.so.cache , a.out run, you can find it libmax.so .
But as a simple test example, let's change the system to something that doesn't seem appropriate.
There is another simple way to a.out specify LD_LIBRARY_PATH .
LD_LIBRARY_PATH=. ./a.out
The program will work as expected. LD_LIBRARY_PATH=.is to tell the a.out dynamic library to find the link first in the current path.
For the ELF format executable program, is done by ld-linux.so*, it has searched the elf file DT_RPATH segment, environment variables LD_LIBRARY_PATH ,/etc/ld.so.cache file list,/lib/,/usr/lib directory, When the library file is found, it is loaded into memory. (http://blog.chinaunix.net/uid-23592843-id-223539.html)
Makefile to automate your work
Write the makefile, which reads as follows:
.PHONY: build test cleanbuild: libmax.solibmax.so: max.o gcc -o [email protected] -shared $<max.o: max.c gcc -c -fPIC $<test: a.outa.out: test.c libmax.so gcc test.c -L. -lmax LD_LIBRARY_PATH=. ./a.outclean: rm -f *.o *.so a.out
make buildis generated libmax.so , make test generated a.out and executed, and the make clean compilation and test results are cleaned up.
2015-03-11 Wed
Linux dynamic Library generation and usage guide