A Linux-link library (DLL, static library, the library on what path)

http://www.cppblog.com/wolf/articles/74928.html

http://www.cppblog.com/wolf/articles/77828.html

http://www.jb51.net/article/34990.htm

Much more than copy of the article.

Category 1 library

Depending on the connection time, the library there are points of static and dynamic libraries.

Static library is linked at the link stage (like nonsense, but the fact is that), so the resulting executable file is not affected by the bank, even if the library is deleted, the program can still run successfully.

Unlike static library, dynamic link libraries are linked when the program is executed. So, even though the program compiled, the library still remains on the system, calls for the program to run. (TODO: link dynamic link library stage in the end what has been done)

 

2 Comparison of static and dynamic libraries

Static library is actually a link to copy and paste a sense, the object of the operation but it is not the source object code only. Static library because the library will be linked directly into the executable file, so it brings up two questions.

The first is system space is wasted. This is obvious, imagine if multiple programs linked with a library, each resulting executable file will then have a copy of the library, the system will inevitably wasted space.

Moreover, people are not saints, even finely tuned libraries, inevitably there will be wrong. Once the library has found bug, save up more trouble. Eleven must link this library to find out, and then recompile.

The emergence of a dynamic library being more than made up for the shortcomings of static libraries. Because dynamic runtime library is linked, so simply keep a copy on disk, thus saving disk space. If you find a bug or you want to upgrade is very simple, as long as the new library to replace the original on the line.

Well, it is not static libraries are good for nothing it?

The answer: Definitely No. What is not there is a saying: there is reasonable. Since there is no static library lost in the history of the surging, it will inevitably have its uses. Imagine this situation: if you compile a program using libpcap library is running to give people without pcap library installed on his system, how to solve it? The easiest way is to compile the program is to link all libraries are linked to their static library, so that you can run the program directly on someone else's system.

The so-called gains will lose, because the dynamic link library version is bound to be compared, so programs run faster and linking static libraries when the program runs discounted. Judging, however, lack of dynamic libraries relative to its benefits in today's hardware is simply negligible, so the linker at link dynamic link library is usually a priority, unless specified parameters linked with -static static library.

Dynamic Link Library

1. Create a dynamic link library

#include<stdio.h>
void hello()
{
  printf("hello world/n");

}

 

With the command gcc -shared hello.c -o libhello.so compiled as dynamic libraries. You can see that the current directory of more than one file libhello.so.

2. re-edit a test file test.c, which reads as follows


#include<stdio.h>
int main()
{
printf("call hello()");
hello();
}

-Lhello compile test.c gcc
the -l option tells the compiler to use the library hello. Strange place is the name of the dynamic library is libhello.so, but here use hello.
But this is not enough, the compiler will go wrong.

`Main function an In ':
test.c :( text + 0x1d):. Reference to undefined` hello'
collect2: LD returned 1 Exit Status
This is because the library hello in our own path, the compiler can not find.
You need to use the -L option to tell the location of the library hello
gcc. -O the Test test.c -lhello -L
-L. To tell the compiler to locate the library file in the current directory

3. Perform ./test compiled successfully, still wrong

That can not find library

There are two ways:

A, can be added to the current path and then run the ldconfig /etc/ld.so.conf, or in the current path for the operating parameters of the ldconfig (job need to be root).

Second, the current path environment variable LD_LIBRARY_PATH to join in

, of course, if you feel not cause confusion, then the library can directly copy into the / lib, / usr / lib / and other locations (inevitably, have to have permission to do so) such links and loaders can be found on the library of accurate.

We use the second method:
Export LD_LIBRARY_PATH =:. $ LD_LIBRARY_PATH
so, then the implementation will be successful.

Here then talk about the static link library

Still use just hello.c and test.c.
1. gcc -c hello.c Note that no -shared option
2. goal filing ar -r libhello.a hello.o
    program with ar -r parameter lists to create a new library and command line libhello.a the object file inserted. In this way, if the library does not exist, -r parameter will create a new library, and if the inventory, it will replace the original module with a new module.
3. static library link in the program
           gcc test.c -lhello -L. -Static -o hello.static
or gcc test.c libhello.a -L. -O hello.static

Generated hello.static is no longer dependent on the libhello.a

Two useful commands

procedure is used to determine the file type of the file, in file command, all files of true colors.
By the way, a skill. Sometimes download in the windows browser tar.gz or tar.bz2 file extension will become a strange tar.tar, some newcomers to Linux I do not know how unzipped. But the Linux file types are not affected by file extension, so we can first use the command file xxx.tar.tar look at the file type, then extract with tar with appropriate parameters.

Further, the program can also use to determine ldd utility.
ldd is used to print the target program (by the command line arguments) information for all the dynamic libraries linked, if the program does not target the dynamic link library, print "not a dynamic executable", ldd usage please refer to the manpage.

E.g:

Check with the ldd command ffmpeg libraries depend on what command:  ldd `Which ffmpeg`

Guess you like

Origin www.cnblogs.com/liuzhenbo/p/11031130.html