Static and dynamic libraries in Linux

Source: Internet
Author: User
Static and dynamic libraries in Linux 1. make your own dynamic library and static 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...
Static and dynamic libraries in Linux 1. make your own dynamic library and static 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 following is the content of the two files // hello. c www.2cto.com # include Void my_lib_func () {printf ("Library routine called \ r \ n");} // test. c # include Www.2cto.com int main () {my_lib_func (); return 1;} test. c calls the hello. c method. We encapsulate hello. c as a library file. Both static and dynamic libraries are composed. o file composition, we first put gcc-c hello. c generation. o file production static library ar crv libmyhello. a hello. o, ar is the command to generate the static library, libmyhello. a is my static database name. The next step is to use the static library in my program.

The result of Library routine 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 to create a dynamic library www.2cto.com
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.

Run successfully 2. the calling rules of both the dynamic library and the static library can be found that, whether it is a dynamic library or a static library, the parameter-l is added 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 process. Therefore, we can draw a conclusion that both the dynamic library and the static library exist, gcc will give priority to the dynamic library author Lishu sunshine

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.