Creation and use of Linux static libraries and dynamic libraries (shared libraries) (attention to coverage issues)

Source: Internet
Author: User

One, the Linux operating system support function library Branch

Static libraries: LIBXXX.A, compiling libraries into executable programs at compile time

Advantage: No external function library is required in the program's running environment

Cons: Large executable program

Dynamic libraries: Also known as shared libraries, libxxx.so, loading libraries into executable programs while the program is running

Advantages: Small Executable Program

Disadvantage: The corresponding library must be provided in the running environment of the program

function Library Catalog:/lib/usr/lib

Second, the production of Static library

① generating the target file: Gcc-c file.c

② Creating a static library command AR

AR-CR Libfile.a FILE.O

The meaning of-c:crete

-r:replace means that when the inserted module FILE.O already exists in Libfile.a, it is overwritten. Conversely, AR displays an error message.

③ several examples of operating static libraries:

Scenario 1: If you get a static library Libunknown.a from Elsewhere, you want to know which modules are included.

Command---ar-t libunknown.a

Compile the Static library: Gcc-o main main.c-l. -lfile compiled MAIN.C will integrate the static library into main.

which

-L: Specifies the location of the static function library for lookup, and note that there is a later. , which indicates that the static library is looking in the current directory.

-L: The static library name is specified, because the static function library is named Lib***.a, where Lib and. A can be ignored.

④ Example

   

   

   

   

Three, the production of dynamic library

① generating the target file: Gcc-c file.c

②gcc-shared-fpic-o libfile.so FILE.O

-fpic: Generating location-independent code

-shared: Generating a shared library

Use the above command to generate the libfile.so dynamic function library.

Gcc-o out Main.c-l. -lfile

It is not yet possible to execute immediately./out, because when used in a dynamic library, the dynamic library is found by default in the/usr/lib or/lib directory, while the libraries we generate are not inside

③ Example

    

    

The first method:

Libfile.so side to/lib or/usr/lib.

The second method: The method of environment variables, assuming libsub.so in ~/coding/libsotest

Execute Export ld_library_path=~/coding/libsotest

Echo $LD _library_path to see if the path was added successfully

    

Third method: Modify the Sheel configuration script

Add our generated library directory to the/etc/ld.so.conf file and/sbin/ldconfig

/etc/ld.so.conf is a very important directory that contains the directories that the linker and the loader will check when searching for shared libraries, by default from/usr/lib

Or/lib, so if you want to run smoothly, you can add the directory of our library to this file and execute the/sbin/ldconfig

① Open ld.so.conf File

      

② adding a path to a file

      

③ execute Sheel script with Ldconfig

      

④ Executing an executable program

          

Iv. comparison of the size of static and dynamic libraries

      

Thus:

Compile successfully after executable program, static cubby large, because there is not much code here, so see there is a big gap

Http://www.cnblogs.com/jiangson/p/6077171.html

Creation and use of Linux static libraries and dynamic libraries (shared libraries) (attention to coverage issues)

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.