Static and dynamic libraries in Linux

Source: Internet
Author: User

1. Create your own dynamic and static libraries

In Linux, the dynamic library. the static library ends. end with A. They all start with Lib. For example, if a library name is net, its full name should be Libnet. so or Libnet. a.

We have two files, hello. C and test. C. The content of the two files is as follows:

// Hello. c

# Include <stdio. h>

 

Void my_lib_func ()

{

Printf ("libraryroutine called \ r \ n ");

}

 

// Test. c

# Include <stdio. h>

 

Int main ()

{

My_lib_func ();

 

Return1;

}

 

Test. c calls the hello. C method. We encapsulate hello. C as a library file. Both static and dynamic libraries are composed of. O files.
-Chello. C: generate the. o file

Create a static library

Ar CRV libmyhello. A hello. O, Ar is the command to generate the static library, and libmyhello. A is the name of my static library. The next step is to use the static library in my program.

The result of libraryroutine called is displayed, indicating that the call is successful.

Next we will delete libmyhello. A to see if the program is still running normally.

We found that the program is still running normally, indicating that the static library has been connected to our program.

Create a dynamic library

We can see that the dynamic library libmyhello. So has been generated. Continue to use

The library file cannot be found. At this time, we copy the so file to/usr/lib.

Running successful

 

2. Call rules for both dynamic and static databases

We can find that both the dynamic library and the static library are added with the-l parameter during program compilation and connection, so when they exist at the same time, the program will choose dynamic library or static library. Let's make a try.

Libmyhello. A And libmyhello. so, we found that the dynamic library could not be found during the running, so we can draw a conclusion that, when both the dynamic library and the static library exist, GCC will give priority to the dynamic library.

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.