[Reprint] [libraries] dynamic and static libraries advantages and disadvantages [libraries] dynamic and static libraries advantages and disadvantages

[Libraries] dynamic and static libraries advantages and disadvantages

 

Transfer: https://blog.csdn.net/u010649766/article/details/78528601

Necessity function

We are writing a C language program, they often encounter a lot of duplicate or common part, if every time rewrite is certainly possible, but that would greatly reduce efficiency and affect the readability of the code, but do not conducive to the latter part of code maintenance. We can make them into a corresponding performance function, called directly will be very easy to use, can also function to upgrade later.

For example, I want to repeatedly swap the values ​​of two variables in a piece of code, I can write many times in your code

i=x;
x=y;
y=i;

But this is a bit of trouble we can write a change_two_int () function to simplify. 
Function is defined as follows:

Copy the code
void change_two_int( int *a,int *b )
{
    int c;

    c=*a;

    a=b;

    *b=c;
}
Copy the code

Just when this call every time you want to exchange change_two_int (& x, & y); can be, whether a lot of convenience?

So these have to do it we need to discuss? He said the popular library of object files is to package these common functions together, providing an interface to the appropriate function, easy to use programmer. Library is an existing, mature, others can reuse code written, we only need to know how to define its interface, you can freely use.

= Shared library dynamic library

In reality every program depends on many underlying foundation of the library, not every person's code from scratch, so there is unusual significance library. For example, we often use the printf function is a function of the C standard library provides. We only need to include the appropriate header files can be used in the use of (non-static compiler also have the appropriate library file). Printf function without concern for how the concrete is achieved, thus greatly improving the efficiency of programmers writing code. From the use of sub-libraries can be divided into two categories: static and shared libraries. In the windows in a static library is a file with the extension .lib, .dll shared library is a file with the extension. In linux .a static library is a file with the extension, the shared library suffix is .so file
Static and dynamic library in linux for example we look, first of all we look at the way their generation:

  • Static library:

    First, the source file is compiled into an object file: gcc -c ac bc 
    generate static libraries: ar -rc libstatic.a ao bo

  • Shared libraries:

    Like static library compiled into an object file: gcc -c ac bc 
    generate a shared library: gcc -fPIC -shared -o libshared.so ao bo

This shows that static and dynamic libraries is handled on the target file, it can be said that the library files are machine code files, loading the static libraries and shared objects are very different.

  • Static library linking method:

    gcc -o staticcode -L. -lstatic main.c -static (default library in the current folder)

  • Share link library method:

    gcc -o sharedcode -L. -lshared main.c (default library in the current folder)

Static library

When the program is connected with a static library, all functions of the machine code to be used in target library file copy to be included in the final executable file. This leads finally generated executable code amount becomes relatively, equivalent full complement code compiler, and advantages , so that comparatively faster running. But there is a drawback : take up disk and memory space static libraries will be added to each program and its connection, and the program is running, will be loaded into memory consumes virtually the addition of more memory. space.

Dynamic library

Executable files connected to the shared library contains only function it needs a reference table, and not all of the function code only during program execution, those functions required code was only copied into memory. Advantage , thus making relatively small executable file, saving disk space, and further, the operating system uses virtual memory, shared libraries so that a resident is using multiple programs in memory, and also saves memory. Shortcoming , but because the link to go to the library will take some time to run, the execution speed will be relatively slow, in general static library is at the expense of space efficiency, time efficiency in exchange, shared library is at the expense of time efficiency in exchange for space efficiency, there is no difference between good and bad, just look at the specific needs.

In addition, the program compiled after a good, sometimes need to make some changes and optimization, if we want to modify the function library happens to be the case, under the premise of the same interface, using shared libraries program only need to recompile shared libraries on it while using static library static library program you will need to re-compiled, the program will be a re-compile. It is also using the difference among the process to the current project, for example, when a remote update, if only * .so dynamic library package contents changed, then you only need to update * .so.


to sum up:

(A) the presence of the static and dynamic library in the form of two systems

  • Under Windows

  .dll dynamic libraries

  .lib static libraries

  Library is the source code binaries

  • Under Linux

  .so dynamic library

  .a static library

(B) the pros and cons of static and dynamic libraries

Static library program compiled will be connected to the object code, you will no longer need the static library program is running .

Dynamic library at the assembly time and is not connected to the object code, but the program was only running is loaded, so the program will also be required to run a dynamic inventory .

1. Under the linux library file is how to generate

Static library suffix is ​​.a, it produces a two-step

Step 1. generated by the source file is compiled .o pile, each symbol table contains .o in this compilation unit

Step 2.ar command will convert .o .a lot written static library

Dynamic library suffix is ​​.so, which is generated by the gcc compiler plus specific parameters.

E.g:

gccfPICc.cgcc−fPIC−c∗.c gcc -shared -Wl,-soname, libfoo.so.1 -olibfoo.so.1.0 *.

2. How does the library file is named, there is no specification

In linux, library files are typically placed in the / usr / lib and / lib,

Static library name generally libxxxx.a, where xxxx is the name of the lib

The name of the dynamic library is generally libxxxx.so.major.minor, xxxx is the name of the lib's, major is the major version number, minor is the minor version number

3. How do you know which libraries depend on an executable program

ldd command to view an executable program depends on shared libraries,

For example # ldd /bin/lnlibc.so.6

=> /lib/libc.so.6 (0×40021000)/lib/ld-linux.so.2
=> /lib/ld- linux.so.2 (0×40000000) 

You can see the ln command depends on libc libraries and ld-linux library

4. executable program how to locate the shared library files during execution

When the system load executable code, when library can know the name of it depends, but also need to know the absolute path

At this time, the system requires a dynamic loader (dynamiclinker / loader)

For executable elf format, it is to be completed by the ld-linux.so *

It has a search elf files DT_RPATH segment - LD_LIBRARY_PATH- / etc / ld.so.cache list file environment variables - / lib /, / usr / lib directory

You will find the library file that is loaded into memory

5. After installing a new library system how to find him

If you install in / lib or / usr / lib, then the default ld be able to find, no further action.

If installed in another directory, /etc/ld.so.cache need to add it to a file, the steps of

1. Edit the /etc/ld.so.conf file and add the path to the directory where the file library

2. Run ldconfig, the command will rebuild /etc/ld.so.cache file

############################################################

The basic method linux compile a static library (.a) and dynamic libraries (.so) of

(C) static library

In the linux environment, use the ar command to create a static library file is the following command options:

d -----从指定的静态库文件中删除文件 m -----把文件移动到指定的静态库文件中 p -----把静态库文件中指定的文件输出到标准输出 q -----快速地把文件追加到静态库文件中 r -----把文件插入到静态库文件中 t -----显示静态库文件中文件的列表 x -----从静态库文件中提取文件

There are multiple modifiers modify the basic options more details please man ar Here are three:

After the new object files (* .o) is added to a static library files in the existing file - a

B - ***** Before

v - verbose mode 

Ar command line format command is as follows:

ar[-]{dmpqrtx}[abcfilNoPsSuvV][membername] [count] archive files…

Name parameter defined archive libraries, files is a list of object files contained in the library file, separate each file with a space.

For example, create a static library files command as follows:

r libapue.a air error.oerrorlog.o lockreg.o

Thus the libapue.a static library files, you can use a file included in the library display options t

After you create a library file, you can create the index static library files to help increase the speed of other programs and compile libraries connected:

Create a library program ranlib index, the index stored in the internal library file.

ranlib libapue.a

Index archive files are displayed with the nm program that can display symbols of the target file

nm libapue.a | more

If the goal is to display the symbol file:

nm error.o | more

? How to use it as follows:

gcc -o test test.c libapue.a

This function libapue.a can call in the test.c.

(Iv) dynamic libraries

1. Create a shared library

gcc -shared -o libapue.soerror.o errorlog.o

This creates a shared library!

2. Compile the shared library

Assume shared library in the current directory (ie program files with the same directory)

gcc -o test -L. -lapue test.c

This will compile the executable file does not contain the function code, but linux but will find a dynamic loader can not find the file you libapue.so runtime.

You can view the executable file with the ldd command depend what shared libraries:

ldd test

? How can the dynamic loader library files it found there are two ways to resolve:

  1. Environment Variables

    exportLD_LIBRARY_PATH=”$LD_LIBRARY_PATH:.”

  2. Modify /etc/ld.so.conf file.

    General application library files and system libraries are not placed in the same directory, the general application of the shared library file in the / usr / local / lib, create a directory of their own apue, then just libapue.so copy the past on the line

At the same time the new line in /etc/ld.so.conf:

/usr/local/lib/apue

After adding compiler option when compiling the program:

-L /usr/local/lib/apue -lapue

Configuration parameters can be seen by mangcc

-llibrary

Link library file called library.

Connectors to find the real name of the library file, library files in the standard search directories

 
Category:  [4.1] .Misc

Transfer: https://blog.csdn.net/u010649766/article/details/78528601

Necessity function

We are writing a C language program, they often encounter a lot of duplicate or common part, if every time rewrite is certainly possible, but that would greatly reduce efficiency and affect the readability of the code, but do not conducive to the latter part of code maintenance. We can make them into a corresponding performance function, called directly will be very easy to use, can also function to upgrade later.

For example, I want to repeatedly swap the values ​​of two variables in a piece of code, I can write many times in your code

i=x;
x=y;
y=i;

But this is a bit of trouble we can write a change_two_int () function to simplify. 
Function is defined as follows:

Copy the code
void change_two_int( int *a,int *b )
{
    int c;

    c=*a;

    a=b;

    *b=c;
}
Copy the code

Just when this call every time you want to exchange change_two_int (& x, & y); can be, whether a lot of convenience?

So these have to do it we need to discuss? He said the popular library of object files is to package these common functions together, providing an interface to the appropriate function, easy to use programmer. Library is an existing, mature, others can reuse code written, we only need to know how to define its interface, you can freely use.

= Shared library dynamic library

In reality every program depends on many underlying foundation of the library, not every person's code from scratch, so there is unusual significance library. For example, we often use the printf function is a function of the C standard library provides. We only need to include the appropriate header files can be used in the use of (non-static compiler also have the appropriate library file). Printf function without concern for how the concrete is achieved, thus greatly improving the efficiency of programmers writing code. From the use of sub-libraries can be divided into two categories: static and shared libraries. In the windows in a static library is a file with the extension .lib, .dll shared library is a file with the extension. In linux .a static library is a file with the extension, the shared library suffix is .so file
Static and dynamic library in linux for example we look, first of all we look at the way their generation:

  • Static library:

    First, the source file is compiled into an object file: gcc -c ac bc 
    generate static libraries: ar -rc libstatic.a ao bo

  • Shared libraries:

    Like static library compiled into an object file: gcc -c ac bc 
    generate a shared library: gcc -fPIC -shared -o libshared.so ao bo

This shows that static and dynamic libraries is handled on the target file, it can be said that the library files are machine code files, loading the static libraries and shared objects are very different.

  • Static library linking method:

    gcc -o staticcode -L. -lstatic main.c -static (default library in the current folder)

  • Share link library method:

    gcc -o sharedcode -L. -lshared main.c (default library in the current folder)

Static library

When the program is connected with a static library, all functions of the machine code to be used in target library file copy to be included in the final executable file. This leads finally generated executable code amount becomes relatively, equivalent full complement code compiler, and advantages , so that comparatively faster running. But there is a drawback : take up disk and memory space static libraries will be added to each program and its connection, and the program is running, will be loaded into memory consumes virtually the addition of more memory. space.

Dynamic library

Executable files connected to the shared library contains only function it needs a reference table, and not all of the function code only during program execution, those functions required code was only copied into memory. Advantage , thus making relatively small executable file, saving disk space, and further, the operating system uses virtual memory, shared libraries so that a resident is using multiple programs in memory, and also saves memory. Shortcoming , but because the link to go to the library will take some time to run, the execution speed will be relatively slow, in general static library is at the expense of space efficiency, time efficiency in exchange, shared library is at the expense of time efficiency in exchange for space efficiency, there is no difference between good and bad, just look at the specific needs.

In addition, the program compiled after a good, sometimes need to make some changes and optimization, if we want to modify the function library happens to be the case, under the premise of the same interface, using shared libraries program only need to recompile shared libraries on it while using static library static library program you will need to re-compiled, the program will be a re-compile. It is also using the difference among the process to the current project, for example, when a remote update, if only * .so dynamic library package contents changed, then you only need to update * .so.


to sum up:

(A) the presence of the static and dynamic library in the form of two systems

  • Under Windows

  .dll dynamic libraries

  .lib static libraries

  Library is the source code binaries

  • Under Linux

  .so dynamic library

  .a static library

(B) the pros and cons of static and dynamic libraries

Static library program compiled will be connected to the object code, you will no longer need the static library program is running .

Dynamic library at the assembly time and is not connected to the object code, but the program was only running is loaded, so the program will also be required to run a dynamic inventory .

1. Under the linux library file is how to generate

Static library suffix is ​​.a, it produces a two-step

Step 1. generated by the source file is compiled .o pile, each symbol table contains .o in this compilation unit

Step 2.ar command will convert .o .a lot written static library

Dynamic library suffix is ​​.so, which is generated by the gcc compiler plus specific parameters.

E.g:

gccfPICc.cgcc−fPIC−c∗.c gcc -shared -Wl,-soname, libfoo.so.1 -olibfoo.so.1.0 *.

2. How does the library file is named, there is no specification

In linux, library files are typically placed in the / usr / lib and / lib,

Static library name generally libxxxx.a, where xxxx is the name of the lib

The name of the dynamic library is generally libxxxx.so.major.minor, xxxx is the name of the lib's, major is the major version number, minor is the minor version number

3. How do you know which libraries depend on an executable program

ldd command to view an executable program depends on shared libraries,

For example # ldd /bin/lnlibc.so.6

=> /lib/libc.so.6 (0×40021000)/lib/ld-linux.so.2
=> /lib/ld- linux.so.2 (0×40000000) 

You can see the ln command depends on libc libraries and ld-linux library

4. executable program how to locate the shared library files during execution

When the system load executable code, when library can know the name of it depends, but also need to know the absolute path

At this time, the system requires a dynamic loader (dynamiclinker / loader)

For executable elf format, it is to be completed by the ld-linux.so *

It has a search elf files DT_RPATH segment - LD_LIBRARY_PATH- / etc / ld.so.cache list file environment variables - / lib /, / usr / lib directory

You will find the library file that is loaded into memory

5. After installing a new library system how to find him

If you install in / lib or / usr / lib, then the default ld be able to find, no further action.

If installed in another directory, /etc/ld.so.cache need to add it to a file, the steps of

1. Edit the /etc/ld.so.conf file and add the path to the directory where the file library

2. Run ldconfig, the command will rebuild /etc/ld.so.cache file

############################################################

The basic method linux compile a static library (.a) and dynamic libraries (.so) of

(C) static library

In the linux environment, use the ar command to create a static library file is the following command options:

d -----从指定的静态库文件中删除文件 m -----把文件移动到指定的静态库文件中 p -----把静态库文件中指定的文件输出到标准输出 q -----快速地把文件追加到静态库文件中 r -----把文件插入到静态库文件中 t -----显示静态库文件中文件的列表 x -----从静态库文件中提取文件

There are multiple modifiers modify the basic options more details please man ar Here are three:

After the new object files (* .o) is added to a static library files in the existing file - a

B - ***** Before

v - verbose mode 

Ar command line format command is as follows:

ar[-]{dmpqrtx}[abcfilNoPsSuvV][membername] [count] archive files…

Name parameter defined archive libraries, files is a list of object files contained in the library file, separate each file with a space.

For example, create a static library files command as follows:

r libapue.a air error.oerrorlog.o lockreg.o

Thus the libapue.a static library files, you can use a file included in the library display options t

After you create a library file, you can create the index static library files to help increase the speed of other programs and compile libraries connected:

Create a library program ranlib index, the index stored in the internal library file.

ranlib libapue.a

Index archive files are displayed with the nm program that can display symbols of the target file

nm libapue.a | more

If the goal is to display the symbol file:

nm error.o | more

? How to use it as follows:

gcc -o test test.c libapue.a

This function libapue.a can call in the test.c.

(Iv) dynamic libraries

1. Create a shared library

gcc -shared -o libapue.soerror.o errorlog.o

This creates a shared library!

2. Compile the shared library

Assume shared library in the current directory (ie program files with the same directory)

gcc -o test -L. -lapue test.c

This will compile the executable file does not contain the function code, but linux but will find a dynamic loader can not find the file you libapue.so runtime.

You can view the executable file with the ldd command depend what shared libraries:

ldd test

? How can the dynamic loader library files it found there are two ways to resolve:

  1. Environment Variables

    exportLD_LIBRARY_PATH=”$LD_LIBRARY_PATH:.”

  2. Modify /etc/ld.so.conf file.

    General application library files and system libraries are not placed in the same directory, the general application of the shared library file in the / usr / local / lib, create a directory of their own apue, then just libapue.so copy the past on the line

At the same time the new line in /etc/ld.so.conf:

/usr/local/lib/apue

After adding compiler option when compiling the program:

-L /usr/local/lib/apue -lapue

Configuration parameters can be seen by mangcc

-llibrary

Link library file called library.

Connectors to find the real name of the library file, library files in the standard search directories

Guess you like

Origin www.cnblogs.com/jinanxiaolaohu/p/10943329.html