Comparison between Linux dynamic library and static library usage

Source: Internet
Author: User

Under Windows, the use of dynamic library DLLs is often accompanied by LIB guidelines, while Linux uses dynamic libraries and static libraries are significantly different.

The difference between a Linux static library and a dynamic library

1. Static Library

The name is generally libxxx.a; the file compiled from the static function library is larger because all the data from the library is integrated into the target code, and the compiled execution program does not require external library support, but the upgrade is cumbersome. Each version update needs to be recompiled.

2. Dynamic Library

The name is usually libxxx.so; The dynamic library is not compiled into the final program and is dynamically loaded into memory when needed. The compiled program does not contain the dynamic library part, the operation of the program needs dynamic library support, upgrade convenience.

Use of static libraries

Operation tools for static libraries: GCC (g++) and AR commands.

writing and using static libraries

(1) Design library source code test1.cpp and Test2.cpp

Test1.cpp code content is as follows:

#include <stdio.h>

void Bye1 ()

{

printf ("jack,goodbye\n");

}

Test2.cpp code content is as follows:

#include <stdio.h>

void Bye2 ()

{

printf ("Tom, byebye\n");

}

(2) Compiling source files

$ g++-o-c test1.cpp test2.cpp//command-line operation

When done, use the LS command to see if two test1.o test2.o files are generated

(3) packaging a static library

In order to correctly locate the library file in the compiler, the static library must be named according to the LIB[NAME].A rules, as in the following example [Name]=test.

$ AR-RSV libtest.a test1.o test2.o//command-line operation, packaging. o Files into libtest.a


$ ls-l *.a//command line to view the generated. A Files


$ ar-t LIBPR.A//View the files contained in the library file


(4) Call library function code main.cpp

Code in Main.cpp:


#include "Test1.cpp"

#include "Test2.cpp"

int main ()

{

Test1 ();

Test2 ();

return 0;

}

(5) Compile Link Options

The-L and-l parameters are placed behind. Where,-l loads the library file path,-l indicates the library file name.

$ gcc-o Main main.c-l./-ltest//Generate executable file

(6) Execution of target procedures

$./main


Use of dynamic libraries

Writing a dynamic library

(1) Design Library Code

Design the source file code to generate the dynamic library:

The Dlluse.cpp code is as follows:

#include <stdio.h>

void print () {

printf ("Linux dlluse test!\n");

}


(2) generate a dynamic library

$ gcc-o-fpic-shared-o dl.so dlluse.cpp//Generate dynamic Library

$ ls-l *.so//View the generated dynamic library

Implicit invocation of the State Library (explicit comparison of headaches)

Specify the location and name of the dynamic library when compiling call library function code, see the following example

Call function Main.cpp:

#include "Dlluse.cpp"

int main ()

{

Print ();

return 0;

}

$ gcc-o main main.c./dl.so//chain delivered as executable program

$./main//Execution procedure

When the location of the dynamic library name changes, the program will not function properly; One of the benefits of replacing a static library with a dynamic library is to upgrade the contents of the library at any time by updating the dynamic library.


View of library dependencies

Use the LDD command to see which libraries the execution file depends on.


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.