Professional Documents
Culture Documents
Debugging symbols.
?
Shared Libraries.
What they are, why they're useful.
Each program using routines from a static library has a copy of them in the executable file. This wastes disk space and (if more than one such program is in use at the same time) RAM as well. Also, when a static library is updated, all the programs using it must be recompiled in order to take advantage of the new code. When a program uses a shared library instead, the program binary does not include a copy of the code, but only a reference to the library. The run time loader, ld.so, finds the library and loads it into memory at the same time as the program.
libfoo.sa: what "exported initialized library data" means. What flags to compile with. Debugging symbols. Where they're installed.
The shared library ("libfoo.so.1.0.1") and the symbolic link with only the major version number ("libfoo.so.1") should be in /lib if the library is required by any program in /bin or /sbin. The link with no version number ("libfoo.so") is used only at compile time. It should be in /usr/lib.
How ld, ldconfig, ldd, and ld.so find them, and /etc/ld.so.conf.
At compile time, ld searches for shared libraries in standard directories and in directories added with -rpath dir and -Ldir command line options. During system boot, or when run manually (after updating libraries), ldconfig searches in standard places plus directories specified in /etc/ld.so.conf, and saves references to the libraries it finds in a cache. ld.so (at program run time) and ldd (as requested) look in directories specified by the -rpath option given at compile time, or in the cache built by ldconfig.
What "ldconfig -p", "ldconfig -D", nm and ldd can tell you.
"ldconfig -p" will display the contents of the cache. "ldconfig -D" will search for shared libraries and display them. "nm --dynamic /usr/lib/libfoo.so.X" will display the symbols defined by the library. "ldd foo" will display the shared libraries required by foo, and where they were found.
[To alter the search path for shared libraries without using -rpath, the ld man page suggests setting LD_RUN_PATH, and the ld.so man page says you set LD_AOUT_LIBRARY_PATH, but I could not get either one to work.]
History: how much more difficult a.out shared libraries were to work with. Pitfalls.
Multiple versions with same name (version scripts?). The libc5/libc6 problem.
$ nm -C /usr/lib/libstdc++-3-libc6.2-2-2.10.0.a | grep 'bool operator==' will find all the c++ equality operator in the referneced library file. The -C option for nm demangles the complier-generated C++ function names and makes them a bit more readable. Now we will move on to the topic about how can we generated our own library. The ar unility can also be used to create a library. For example, we have 2 functions: ascii which is in ascii.c and change_case which is in change_case.c files. //File: ascii.c char * ascii(int start, int finish) { char *b= new char(finish-start+1); for (int i= start; i<=finish; ++i) b[i-start]=char(i); return b; } //File: change_case.c #include<ctype.h> char* change_case(char *s) { char *t = &s[0]; while(*t) { if(isalpha(*t)) *t+=islower(*t)?-32:32; ++t; } return s; } $ g++ -c change_case.c $ g++ -c ascii.c $ ar cr libmydemo.a ascii.o change_case.o What I did above is the compile the code to object code, and the object code added to the archive with the utility ar. The prototypes for the functions in the mydemo library are placed in a corresponding header file called mydemo.h Be sure to use preprocessor
directives in that file to prevent it from being inadvertently included more than once. //File mydemo.h #ifndef MYDEMO_H #define MYDEMO_H char* ascii(int, int); char* change_case(char*); #endif Well, How to use the library I created? Here is an example: File: main.c #include<iostream> #include"mydemo.h" using namespace std; main() { int start, stop; char b[20]; cout<<"enter the start and stop value for string:"<<endl; cin>>start>>stop; cout<<"create string..."<<ascii(start, stop)<<endl; cin.ignore(80,'\n'); cout<<"enter a string :"; cin.getline(b,20); cout<<"coverted string: "<<change_case(b)<<endl; return 0; } Next step is to compile and link it: $ g++ -o main mani.c -L. -lmydemo Here, "-L." is to use to tell the system that when the compiler searches for library files it should also include the current directory. The name of the library is passed using the -l command option, you call the library without the lib prefix and the .a extension.. Ok, now let's run it? $ main enter the start and stop value for string: 56 68
create string... 89:,0<1321@ABCD enter a string : Hello, World. coverted string: hELLO, wORLD. aha, wonderful!