Compilation and use of "Linux related" static libraries and dynamic libraries

Source: Internet
Author: User

Reference Blog http://www.cnblogs.com/feisky/archive/2010/03/09/1681996.html

The above blog on the static library and dynamic library of the explanation, very clear!

Definition of the library:

A library is essentially a binary format of executable code that can be loaded into memory for execution. There are two kinds of library, static library and dynamic library.

The difference between a static library and a dynamic library

1. Static function library

The name of such a library is generally libxxx.a; the file compiled from the static function library is larger, because all the data of the entire library is integrated into the target code, and his advantages are obvious, that is, the compiled execution program does not require external library support, because all the functions used are already compiled. This, of course, will also be a disadvantage, because if the static library changes, then your program must be recompiled.

2. Dynamic Function Library

The name of such a 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, and your program executes to the relevant function to invoke the corresponding function in the library, so the dynamic function library produces the smaller executable file. 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.
The Linux system has several important directories for storing the appropriate libraries, such as/lib/usr/lib.

Example:

Compile and use of static libraries:
1 //***static_lib1.c***2#include <unistd.h>3#include <fcntl.h>4#include <stdio.h>5 6 intPrintSelf1 ()7 {8printf"I am static lib 1\n");9     return 0;Ten}

1 //***static_lib2.c***2#include <unistd.h>3#include <fcntl.h>4#include <stdio.h>5 6 intprintSelf2 ()7 {8printf"I am static lib 2\n");9     return 0;Ten}
1 // ***main.c*** 2 #include <unistd.h> 3 #include <fcntl.h> 4 #include <stdio.h> 5 6 int Main () 7 {  8    printSelf1 (); 9     printSelf2 (); Ten     return 0 ;  One}

#***makefile***main:main.c Static_libGCCMain.c-l.-lstatic_lib-o main#Just compile Cfilestatic_lib1:static_lib1.cGCC-C static_lib1.c#Just compile Cfilestatic_lib2:static_lib2.cGCC-C static_lib2.c#Create Statlic lib***STATIC_LIB:STATIC_LIB1.O static_lib2.oar-RSV LIBSTATIC_LIB.A static_lib1.o static_lib2.oRM-RF STATIC_LIB1.O Static_lib2.oclean:RM-RF MainRM-RF LIBSTATIC_LIB.A

To perform the steps:

[email protected] static_lib]$ makecc    -c-o static_lib1.o static_lib1.ccc    -c-o static_lib2.o static_ LIB2.CAR-RSV libstatic_lib.a static_lib1.o static_lib2.or-static_lib1.or-static_lib2.orm-rf static_lib1.o static_li  B2.OGCC main.c-l-lstatic_lib-o main[[email protected] static_lib]$./mainI am static lib 1I am static Lib 2

Note: the AR command is related to the static library, which is used to query the library information.

Compilation and use of dynamic libraries:
1 //***dynamic.c***2#include <unistd.h>3#include <fcntl.h>4#include <stdio.h>5 6 intprintself ()7 {8printf"This is a dynamic lib src\n");9     return 0;Ten}
1 // ***main.c*** 2 #include <unistd.h> 3 #include <fcntl.h> 4 #include <stdio.h> 5 6 int Main () 7 {  8    printself (); 9     return 0 ; Ten }

#***makefile***main:main.c dynamic_lib    gcc main.c-l.-ldynamic_lib-o Main #* * *  Create dynamic lib libdynamic_lib.sodynamic_lib:dynamic.c    gcc -fpic-shared dynamic.c-o Libdynamic_lib.soclean:    rm -RF Main

After the compilation succeeds, the following steps are performed:

./main export ld_library_path=. [Email protected] dynamic_lib]$cshsetenv ld_library_path. ldd-r mainlinux-gate.so.1 =  (0x00b9b000) libdynamic_lib.so =/libdynamic_lib.so (0x0084a000) libc.so.6 =/lib/libc.so.6 (0x008dd000)/lib/ld-linux.so.2 (0x008bf000) [[email protected] dynamic_lib]$ . MainThis is a dynamic lib Src

Note: The direct execution will error, because there is no definition of the LD_LIBRARY_PATH environment variable, the dynamic library's lookup path is only/usr/lib,/lib; The dynamic library can only be found if the environment variable is set;

Using Ldd-r main is to check the library's dependencies

Where the CSH command is to switch the user's shell to Cshell, the user logs into the default shell as bash, where the switchover is done just to demonstrate how to set the environment variable under Csh,bash.

For the way that dynamic libraries are called dynamically in code, this is not recorded here, you can refer to the blog at the beginning of the article, I think it is very well written

Compilation and use of "Linux related" static libraries and dynamic libraries

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.