Linux Dynamic Library Static Library

Source: Internet
Author: User
Tags crypt

What is a library

In essence, a library is a binary form of executable code that can be loaded into memory by the operating system. Because of the different nature of Windows and Linux, the binary of the libraries is incompatible. The library functions supported by Linux operating system are divided into static and dynamic libraries, and dynamic libraries are also called shared libraries. Linux system has several important directories to hold the corresponding library functions, such as/lib,/usr/lib; the header file is placed in the/usr/include.

What is a static library

The name of the static library is generally libxxx.a; the file compiled from the static function library is larger, because all the data of the library is integrated into the target code, and his advantages are obvious, that is, the compiled execution program does not require the support of the external function library, Because all the functions used have been compiled into the executable file. Of course this will also be his disadvantage, because how the static function library changes, then your program must be recompiled, and the volume is relatively large.

What is a dynamic library

The name of the dynamic library is generally libxxx.so, relative to the static function library, the dynamic function library is not compiled into the target code when it is compiled, your program executes to the relevant function, the corresponding function in the library is called, so the dynamic library function produces less executable files. Since the library is not integrated into your program, it is applied and invoked dynamically while the program is running, so you must provide the appropriate library in the running environment of the program. The change of dynamic function library does not affect your program, so the upgrade of dynamic function library is more convenient. And if multiple applications are going to use the same library, a dynamic library is a great fit to reduce the volume of your application.

Production of static libraries:

Gcc-c file1.c

Gcc-c file2.c

......

Gcc-c FILEN.C

Ar-rcs libname.a file1.o file2.o ... filen.o

Dynamic Library Production:

Gcc-shared-fpic-o libname.so file1.c file2.c ... filen.c

Compile:

Dynamic Library Compilation: GCC main.c-o test_li-llib_patch-lname

Static Library compilation: Gcc-static main.c-o Test_li-llib_patch-lname

(Note: Error at compile time:/usr/lib/ld:cannot find LC and other errors See link: http://www.cnblogs.com/xiaohexiansheng/p/5500870.html)

Case:

1 crypto.c2#include <stdio.h>3 4 voidCryptovoid)5 {6printf"Start Crypt ...");7 8     return ;9 }Ten  One crypto.h A #ifndef _crypto_h_ - #define_crypto_h_ -  the voidCryptovoid); -  - #endif -  + FUNC.C -#include <stdio.h> +  A voidFuncvoid) at { -printf"Start func ..."); -  -     return ; - } -  in func.h - #ifndef _func_h_ to #define_func_h_ +  - voidFuncvoid); the  * #endif

To generate a static library:

[[email protected] test]$ ls
CRYPTO.C crypto.h func.c func.h main.c
[Email protected] test]$ gcc-c crypto.c-o crypto.o
[Email protected] test]$ gcc-c func.c-o FUNC.O
[Email protected] test]$ Ar-crs libcrypfunc.a crypto.o FUNC.O
[[email protected] test]$ ls
CRYPTO.C crypto.h crypto.o func.c func.h func.o libcrypfunc.a main.c

To generate a dynamic library:

[Email protected] test]$ gcc-shared-fpic-o libcrypfunc.so crypto.c func.c
[[email protected] test]$ ls
CRYPTO.C crypto.h crypto.o func.c func.h func.o libcrypfunc.a libcrypfunc.so main.c

Using the generated dynamic library:

[Email protected] test]$ gcc main.c-l.-lcrypfunc-o app

(Note:-l Specifies the location of the function library lookup, and a '. ' After-L, which indicates that the current directory looks for the-l specified library function name, where Lib and. A (. so) are omitted)
[[email protected] test]$ ls
app crypto.c crypto.h crypto.o func.c func.h func.o libcrypfunc.a libcrypfunc.so main.c
[email protected] test]$./app
./app:error while loading shared libraries:libcrypfunc.so:cannot open Shared object file:no such file or directory
[email protected] test]$ pwd
/home/xiaohexiansheng/cc/library/test
[email protected] test]$ export ld_library_path= '/home/xiaohexiansheng/cc/library/test '
[email protected] test]$./app
Start Crypt ... Start func ... [Email protected] test]$

Using the generated static library:

[[email protected] test]$ ls
App crypto.c crypto.h crypto.o func.c func.h func.o libcrypfunc.a libcrypfunc.so main.c
[Email protected] test]$ gcc-static main.c-l.-lcrypfunc-o app
[[email protected] test]$ ls
App crypto.c crypto.h crypto.o func.c func.h func.o libcrypfunc.a libcrypfunc.so main.c
[Email protected] test]$./app
Start crypt ... Start func ... [Email protected] test]$

Let's look at the difference between the size of the static library and the dynamic library:

[Email protected] test]$ gcc main.c-l.-lcrypfunc-o Sharedapp
[Email protected] test]$ gcc-static main.c-l.-lcrypfunc-o Staticapp

[Email protected] test]$ du-sh Sharedapp Staticapp
8.0K Sharedapp
632K Staticapp

Attention:

When static and dynamic libraries are present, GCC uses a dynamic library by default. If you force the use of a static library, add the-static option.

The executable file generated by the dynamic library does not work correctly because the system is looking for a dynamic library by default under/lib,/usr/lib, if you want the program to run properly, you can put the dynamic library into either of these two directories, or---> Ld_library_path : Tells the system to execute the program, in addition to/lib,/usr/lib to find the dynamic library.

The executable file that links the static library is much larger than the executable file for the dynamic library.

Reference Link: http://blog.chinaunix.net/uid-26833883-id-3219335.html

Linux Dynamic Library Static Library

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.