分类: 系统运维
2012-08-30 23:41:26
This methodology, also known as "shared components" or "archive libraries", groups together multiple compiled object code files into a single file known as a library. Typically C functions/C classes and methods which can be shared by more than one application are broken out of the application's source code, compiled and bundled into a library. The C standard libraries and C STL are examples of shared components which can be linked with your code. The benefit is that each and every object file need not be stated when linking because the developer can reference the individual library. This simplifies the multiple use and sharing of software components between applications. It also allows application vendors a way to simply release an API to interface with an application. Components which are large can be created for dynamic use, thus the library remain separate from the executable reducing it's size and thus disk space used. The library components are then called by various applications for use when needed.
Linux Library Types: |
There are two Linux C/C library types which can be created:
Library naming conventions: Libraries are typically names with the prefix "lib". This is true for all the C standard libraries. When linking, the command line reference to the library will not contain the library prefix or suffix.
Thus the following link command: gcc src-file.c -lm -lpthread
The libraries referenced in this example for inclusion during linking
are the math library and the thread library. They are found in
/usr/lib/libm.a and /usr/lib/libpthread.a.
Static Libraries: (.a) |
How to generate a library:
void ctest1(int *i) { *i=5; } |
void ctest2(int *i) { *i=100; } |
#include void ctest1(int *); void ctest2(int *); int main() { int x; ctest1(&x); printf("Valx=%d\n",x); return 0; } |
Note for MS/Windows developers: The Linux/Unix ".a" library is conceptually the same as the Visual C static ".lib" libraries.
Dynamically Linked "Shared Object" Libraries: (.so) |
How to generate a shared object: (Dynamically linked object library file.) Note that this is a two step process.
Compiler options:
Library Links:
Compile main program and link with shared object library:
Compiling for runtime linking with a dynamically linked libctest.so.1.0: gcc -Wall -I/path/to/include-files -L/path/to/libraries prog.c -lctest -o prog Use: gcc -Wall -L/opt/lib prog.c -lctest -o prog Where the name of the library is libctest.so. (This is why you must create the symbolic links or you will get the error "/usr/bin/ld: cannot find -lctest".)List Dependencies:
The shared library dependencies of the executable can be listed with the command: name-of-executable
Example: ldd prog libctest.so.1 => /opt/lib/libctest.so.1 (0x00002aaaaaaac000) libc.so.6 => /lib64/tls/libc.so.6 (0x0000003aa4e00000) /lib64/ld-linux-x86-64.so.2 (0x0000003aa4c00000)
Run Program:
Man Pages:
Links:
Library Path: |
In order for an executable to find the required libraries to link with during run time, one must configure the system so that the libraries can be found. Methods available: (Do at least one of the following)
Sample: /etc/ld.so.conf