Dynamic link library and static link library under Linux What the hell is that? (a) The compilation and use of the static link library

Source: Internet
Author: User

Knowledge is not equal to technology, this sentence really is the more work of the longer the more deeply have experience, learning to the knowledge only constant practice, just into the heart of their own roots, become a part of their own, so anyway, I hope my blog can be written down, slowly precipitate, will have a return, bored time to think, indeed, Writing code is also a kind of entertaining!

A few days ago in the project, there is a problem, the general is: Makefile in the compilation of executable TESTAPPD when the use of-l to refer to a libtest.so, can be compiled through, but when the load runs, the system prompts to find this lib and cause the process to start failure. Later I knew that when the product version was compiled and packaged, this lib was not packaged, and the problem was solved later. But out of curiosity, I want to figure out the concepts of dynamic and static libraries, and the different ways to refer to dynamic libraries and makefile in Dlopen Way before doing another project. Find some information on the Internet, see understand, but also need to manually verify the rest assured.

First of all, say static link library, to tell the truth, sometimes really feel the computer these related to all kinds of tall words are paper tigers, look advanced not, in fact, when you poke open that layer of paper, then back to the matter.

The so-called static link library, white is when you write the code compiled, you refer to the library together to make up, from then you made out of the execution program and outside all no longer have any relationship, even if the library updated, you do not take the edge, second, if many similar programs in the system need to use this library, Then each in the compilation of the need to put this library into, waste storage space (load into memory should also be a waste of memory space). The name of a static library in a Linux system is generally called

XXX.A, so if you see a file that ends in. A then it's mostly a static link library file.

Talk less, we go straight to the dry, to see how the static library is compiled and how to be used

First we wrote a SUM.C, as follows, very simple, there is only a sum function, add two numbers and return

1 int Sum (intint  Number2)2{3     return Number1 +  Number2; 4 }

And, of course, write a sum.h that declares it.

int Sum (intint Number2);

Finally, a main function that calls this SUM function prints the returned results.

1#include <stdio.h>2#include <sum.h>3 4 voidMain ()5 {6     intNUM1 =1;7     intNum2 =2;8     intIRet =0;9 TenIRet =Sum (NUM1, Num2); Oneprintf"Num1 + Num2 =%d.", IRet); A     return; -}

Next we compile the static library, my notebook is the Mac OS environment, after opening the terminal with vim to write code, GCC and other related tools can be used directly, version is not noticed, anyway can be used on the line

1 192: Zch kane$ls2Main.csum. Csum. h3 192: Zch kane$GCC-Csum. C4 192: Zch kane$ls5Main.csum. Csum. hsum. O6 192: Zch kane$7 192: Zch kane$8 192: Zch kane$arCR LIBSUM.Asum. O9 192: Zch kane$lsTenLIBSUM.A MAIN.Csum. Csum. hsum. O One 192: Zch kane$

As above, we use the AR tool to compile the static library, the CR flag tells AR to encapsulate the object file

Then we compile the main program and run:

192 gcc -o sumappd main.c-l. -lsum192lslibsum.a    main.o        sum. h        sumappdmain.c         sum. C        sum. o192: Zch kane$./3

"-L." Indicates that the current Lib path is in this directory,-L is the link Libsum.a this Lib library, well understood.

Dynamic link library and static link library under Linux What the hell is that? (a) The compilation and use of the static link 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.