03-26-2012
There are static libraries - files usually in the /lib directory that end with .a.
There are shared libraries - files usually in the /lib directory that end with .so (or .sl), sometimes called dynamic libraries.
To link means 'Resolve external symbols or functions in your code like printf()'
By default, when you compile C code, you liink against a set of shared libraries.
You have to go out of your way to create an executable that is linked against a static library. Read your compiler documentation on how to do this.
There are a few valid uses for linking statically. Linking against shared libraries, also called dynamic linking, creates an executable that makes much better use of system resources, because eveybody else's executable file shares the same library with your code. MUCH less memory usage. That is why dynamic linking is better.
Got all that?
Next: LD_PRELOAD
This is an envrionment variable that anyone can set. If changes how the UNIX system looks for dynamic libraries. When you first run an executable, you have to find and open the dynamic libraries you linked against so all of the symbols like printf() will be there to use.
You use LD_PRELOAD to force the system to kind of auto-magically link against a library that was NOT one of the original linked libraries. So now the printf() function calls in your executable employ a different library with different code to execute printf.
There are limitations on LD_PRELOAD, to prevent bad guys from doing bad things, but that is beyond the scope of your question.
This User Gave Thanks to jim mcnamara For This Post:
10 More Discussions You Might Find Interesting
1. Programming
I need to create a shared library to access an in memory DB. The DB is not huge, but big enough to make it cumbersome to carry around in every single process using the shared library. Luckily, it is pretty static information, so I don't need to worry much about synchronizing the data between... (12 Replies)
Discussion started by: DreamWarrior
12 Replies
2. UNIX for Advanced & Expert Users
can someone explain whether my understanding is correct
lets suppose we have a program that uses library x.
if x is static then the code of x will be part of our program, so if we're going to have 5 executables of our program, then each executable will have x as part of it.
Also, x does not... (2 Replies)
Discussion started by: JamesByars
2 Replies
3. Programming
I am writing a shared library in Linux (but compatible with other UNIXes) and I want to allow multiple instances to share a piece of memory -- 1 byte is enough. What's the "best" way to do this? I want to optimize for speed and portability.
Obviously, I'll have to worry about mutual exclusion. (0 Replies)
Discussion started by: otheus
0 Replies
4. Shell Programming and Scripting
Hi:
I have a library that it only offers Makefile for building static library. It built libxxx.a file. How do I in any way build a shared library? (either changin the Makefile or direct script or command to build shared library)
Thanks. (1 Reply)
Discussion started by: cpthk
1 Replies
5. Linux
I have some static library(libxxx.a libyyy.a).
And I want to generate my library(libzzz.a), libzzz.a will use libxxx.a and libyyy.a
I wan't my application only use libzzz.a, (means libzzz.a had include libxxx.a, libyyy.a), how can I do that? Thank you.
example:
I have zzz.c.
I do ... (4 Replies)
Discussion started by: freemagic
4 Replies
6. Programming
I am having a devil of a time with a very simple make file. The program needs two shared and one static library. If I link the shared libraries only like below the mysql test app works
... (1 Reply)
Discussion started by: jadsys
1 Replies
7. Programming
if I could compile the same source file as shared/static/dynamic what are the advantages/ disadv of each.
PS:by dynamic i am asking about usage of "dlopen". How is it particularly diff from shared libs (2 Replies)
Discussion started by: dragonpoint
2 Replies
8. Programming
I'm writing a program which uses curl to be run on Linux PCs which will be used by a number of different users. I cannot make the users all install curl on their individual machines, so I have tried to link curl in statically, rather than using libcurl.so. I downloaded the source and created a... (8 Replies)
Discussion started by: BrandonShw
8 Replies
9. Programming
Hello.
I am new to this forum and I would like to ask for advice about low level POSIX programming.
I have to implement a POSIX compliant C shared library.
A file will have some variables and the shared library will have some functions which need those variables.
There is one special... (5 Replies)
Discussion started by: iamjag
5 Replies
10. AIX
I'm trying to install libiconv to AIX 7.1 from an rpm off of the perzl site. The rpm appears to install but I get this error message.
add shr4.o shared members from /usr/lib/libiconv.a to /opt/freeware/lib/libiconv.a
add shr.o shared members from /usr/lib/libiconv.a to ... (5 Replies)
Discussion started by: kneemoe
5 Replies
NMEDIT(1) General Commands Manual NMEDIT(1)
NAME
nmedit - change global symbols to local symbols
SYNOPSIS
nmedit -s list_file [-R list_file] [-p] [-A] [-] [[-arch arch_type] ...] object_file ... [-o output]
DESCRIPTION
Nmedit changes the global symbols not listed in the list_file file of the -s list_file option to static symbols. Undefined symbols and
common symbols are not affected and shouldn't be listed in list_file. For dynamic libraries symbols are turned into private extern symbols
that are no longer external (rather than static symbols). This is done so that the references between modules of a dynamic library are
resolved to the symbols in the dynamic library. Nmedit differs from strip(1) in that it also changes the symbolic debugging information
(produce by the -g option to cc(1)) for the global symbols it changes to static symbols so that the resulting object can still be used with
the debugger.
Nmedit like strip(1) is useful to limit the symbols for use with later linking. This allows control of the interface that the executable
wants to provide to the objects that it will dynamically load, and it will not have to publish symbols that are not part of its interface.
For example an executable that wishes to allow only a subset of its global symbols but all of the shared libraries globals to be used would
have its symbol table edited with:
% nmedit -s interface_symbols -A executable
where the file interface_symbols would contain only those symbols from the executable that it wishes the objects loaded at runtime to have
access to. Another example is an object that is made up of a number of other objects that will be loaded into an executable would built
and then have its symbol table edited with:
% ld -o relocatable.o -r a.o b.o c.o
% nmedit -s interface_symbols relocatable.o
which would leave only the symbols listed in the file interface_symbols (and the undefined and common symbols) as global symbols in the
object file.
The one or more of the following options is required to nmedit(1) is:
-s filename
Leave the symbol table entries for the global symbols listed in filename global but turn all other global symbols (except undefined
and common symbols) into static symbols. The symbol names listed in filename must be one per line. Leading and trailing white space
are not part of the symbol name. Lines starting with # are ignored, as are lines with only white space.
-R filename
Change the symbol table entries for the global symbols listed in filename into static symbols. This file has the same format as the
-s filename option above. If the -R filename option is specified without the -s filename option, then all symbols not listed in the
-R filename option's filename are left as globals. If both a -R filename and a -s filename are given the symbols listed in the -R
filename are basically ignored and only those symbols listed in the -s filename are saved.
-p Change symbols to private externs instead of static. This is allowed as the only option to change all defined global symbols to
private externs.
The options to nmedit(1) are:
-A Leave all global absolute symbols except those with a value of zero, and save objective-C class symbols as globals. This is
intended for use of programs that load code at runtime and want the loaded code to use symbols from the shared libraries.
- Treat all remaining arguments as file names and not options.
-arch arch_type
Specifies the architecture, arch_type, of the file for nmedit(1) to process when the file is a universal file (see arch(3) for the
currently know arch_types). The arch_type can be all to process all architectures in the file. The default is to process all
architectures that are contained in the file.
-o output
Write the result into the file output.
SEE ALSO
strip(1), ld(1), arch(3)
BUGS
The changing of the symbolic debugging information by nmedit is not known to be totally correct and could cause the debugger to crash, get
confused or produce incorrect information.
Apple Inc. May 29, 2007 NMEDIT(1)