Dynamic link library and static link library under Linux What the hell is that? (ii) Compilation and use of dynamic link libraries

Source: Internet
Author: User

In the previous article, I probably described the compilation and use of static link libraries under Linux, and the methods of compiling and using dynamic link libraries.

1. What is a dynamic link library

The so-called dynamic link library, refers to the compilation of the program will not be referenced to the library inserted into the execution of the program, but at the time of execution to load the relevant library, all the program used in this library can share a piece of code.

The benefit is that the executable program takes up less space, and if the library needs to be upgraded, you don't need to recompile your program, just upgrade the relevant library.

2. How to compile and use dynamic link library

Use the same code as the previous article: Main.c sum.c sum.h

Dynamic link library files under Linux This is usually the name: libxxx.so

copy code [email protected]: $ lsmain.c    sum.c    sum.h[email protected]: $ [email protected]: $ gcc-c-fpic sum.c                    ##
   -FIPC tells the compiler to compile the source code into a shared object file, and the PIC (position-independent code) means the non-positional dependency code [email protected]: $ lsmain.c    sum.c     sum.h    Sum.o[email protected]: $ gcc-shared-fpic-o libsum.so sum.o  # #  generate a dynamic link library file Libsum.so[email Protected]: $ lslibsum.so    main.c        sum.c        sum.h        sum.o[email protected]: $ [email protected]: $ gcc-o SUMAPPD main.c-l. -lsum      # #  Generate executable program [email protected]: $ lslibsum.so    sum.c        sum.omain.c        sum.h        sumappd[ Email protected]: $./SUMAPPD                             # #  Run Build results Num1 + Num2 = 3

Conclusion: The method of "-lsum" at compile time is not able to distinguish whether static or dynamic link is present.

If you have both a static-link library and a dynamic-link library in the same directory, the system will refer to the dynamic-link library by default, and if you want to use a static-link library, you will need to add the "-static" parameter at compile time.

(Note: If you write in C + +, you can use GCC instead of g++ when compiling)

Dynamic link library and static link library under Linux What the hell is that? (ii) Compilation and use of dynamic link libraries

Related Article

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.