Dynamic and static libraries under linux library file under linux programming

In essence, the library is a form of executable binary code, the operating system can be loaded into memory to perform. The so-called static, dynamic refers to the link.

It is called a static library [], because the link stage, will be compiled .o object files generated with reference to the library link to the executable file packed together. Therefore, the corresponding link is called static linking.

The same dynamic library.

Static library features are summarized as follows:

Static library for linked library is on the compile-time completion.

Program at run-time libraries with no connection to facilitate the transplant.

Waste of space and resources, because all object files and libraries involved are linked into one executable file. 

Dynamic library features are summarized as follows:

The dynamic link library for some library functions loaded postponed to the period running. 

Sharing resources between processes can be achieved. (Also known as shared libraries and therefore dynamic libraries) will upgrade some of the procedures easier.

Even can truly link load is completely controlled by the programmer in the program code (shows the call).

1. compiled into a static library

Whether static library, or DLL files are created by .o. Therefore, we must first compile the source code to gcc test.c by .o file. It can also be achieved by Makefile.

Create a library name libqww.a library, use the command ar, library name is qww

AR rcs libqww.a 1. 2. 3. 4.o 5.o

Create the interface header file library files

gcc -o testqww test.c -static -L. -lcal

(1), gcc -o testqww: Use gcc compiler, -o specifies the file name, behind testqww is the final generated file name

(2), - static: specifies the use of static libraries

(3), - L.:-L specified using the library, the latter indicates that the library file in the current directory.

(4), - lcal: indicate the name of the library file, where - indicates that option, l is shorthand lib, behind the cal is the real library file name extension is not needed

2. compiled DLL

Generate the target file 1.o 2.o 3.o 4.o 5.o test.o

Gcc compiler Ye Hao also make the line

gcc -shared -o -fPIC libqww.so 1 2 3 4 5

Where the top command description:

(1), gcc -o libqww.so: Use gcc compiler, -o specifies the file name, the name behind the libqww.so dynamic library is finally generated, the back is libqww.a.

(2), - shared: generating a specified dynamic library

(3), - fPIC .: This option tells gcc generates code do not contain references to functions and variables specific memory location, the address link runtime

gcc -o test test.c -L. -lqww

Where the top command description:

(1), gcc -o test: Use gcc compiler, -o specifies the file name.

(2), do not use -static: specifies the use of a dynamic library

(3), - L.:-L specified using the library, the latter indicates that the library file in the current directory.

(4), - lqww: indicate the name of the library file, where - indicates that option, l is shorthand lib, behind qww is the real library file name extension is not needed

View File Size:

total 84
-rw-rw-rw- 1 qiuww qiuww  117 Mar 19 20:07 1.c
-rw-RW-rw- 1 qiuww qiuww Mar 20 1232 20:16 1.o
-rw-rw-rw- 1 qiuww qiuww  107 Mar 19 20:08 2.c
-rw-RW-rw- 1 qiuww qiuww Mar 20 1232 20:16 2.o
-rw-rw-rw- 1 qiuww qiuww  111 Mar 19 20:08 3.c
-rw-RW-rw- 1 qiuww qiuww Mar 20 1232 20:16 3.o
-rw-rw-rw- 1 qiuww qiuww  180 Mar 19 20:08 4.c
-rw-RW-rw- 1 qiuww qiuww Mar 20 1248 20:16 4.o
-rw-rw-rw- 1 qiuww qiuww  107 Mar 19 20:48 5.c
-rw-RW-rw- 1 qiuww qiuww Mar 20 1232 20:16 5.o
---------- 1 qiuww qiuww  362 Mar 20 20:16 Makefile
-rwxrwxrwx 1 qiuww qiuww 8680 Mar 22 09:55 a.out
-rwxrwxrwx 1 qiuww qiuww Mar 20 8681 20:36 Bol
-rw-rw-rw- 1 qiuww qiuww  216 Mar 19 20:22 cal.h
-rw-rw-rw- 1 qiuww qiuww 6588 Mar 20 20:21 libqww.a
-rwxrwxrwx 1 qiuww qiuww 7657 Mar 20 20:42 qww.so
-rw-rw-rw- 1 qiuww qiuww  586 Mar 20 20:17 test.c
-rw-RW-rw- 1 qiuww qiuww Mar 20 2736 20:18 test.o
-rwxrwxrwx 1 qiuww qiuww 8681 Mar 20 20:42
testqww original static library is huge enough to have about 700K, but later realized the dynamic library, I do not know how a static library smaller.
And more dynamic than the static library before the library small.
This article is excerpted  Li Cheese and double the programming under linux library file https://www.cnblogs.com/guochaoxxl/p/7141447.html

Guess you like

Origin www.cnblogs.com/qww-qing/p/12544399.html