Linux static library and dynamic library compilation

Source: Internet
Author: User

Using static and dynamic libraries today, a few examples have been written to solidify the foundation.
hello1.c ———————————————————— #include <stdio.h>
void print1 (int i) {int J; for (j=0;j<i;j++) {printf ("%d *%d =%d\n", j,j,j*j);}  }
hello2.c _________________________________________________#include <stdio.h>
void Print2 (char *arr) {char c; int i=0; while (c=arr[i++])! = ' + ') {printf ("%d****%c\n", I,c);}}
hello.c ____________________________________________________ void print1 (int); void Print2 (char *);
int main (int argc,char **argv) {int i=100; char *arr= "This is Laymu ' S home!"; Print1 (i); Print2 (arr);
return 0; }
You can see that the HELLO.C uses the Print1 function in hello1.c and the Print2 function in hello2.c. Therefore, you can combine these two functions into a library for more programs to be called as components.
Method one: Compile hello1.c and hello2.c into a static link library. A [[email protected] main5] #gcc-C hello1.c hello2.c//compile hello1.c and hello2.c into HELLO1.O respectively and HELLO2.O, where-c option means compile-only not linked. [[email protected] main5] #ar-R libhello.a hello1.o hello2.o//Combine HELLO1.O and HELLO2.O as libhello.a this static link library [email protected ] Main5] #cp libhello.a/usr/lib//Copy libhello.a to the/usr/lib directory as a system-shared static link library [[email protected] main5] #gcc-o Hello Hel Lo.c-lhello//compile hello.c as executable hello, this process uses the-lhello option, which tells the GCC compiler to/usr/ The Lib directory goes down to find libhello.a static link library The process is similar to the compilation and invocation process of Lib static link library under Windows.
Method Two: The HELLO1.O and HELLO2.O groups are synthesized into dynamic link libraries. So [[email protected] main5] #gcc-C-fpic hello1.c hello2.c//compile hello1.c and hello2.c into hell O1.O and hello2.o,-c mean to compile only the non-link,-fpic is the location of the independent code, indicating that the compiler generated code to fit the contents of the shared library code can be loaded into memory based on the location of the internal address calculation. [[email protected] main5] #gcc-shared hello1.o hello2.o-o hello.so//Combine HELLO1.O and HELLO2.O as shared object, i.e. dynamic link library [email Protected] Main5] #cp hello.so/usr/lib//copy hello.so to/usr/lib directory [[email protected] main5] #gcc-o Hello hello.c hello.s O//The HELLO.C compiler is linked to the executable program of Hello, this process uses the dynamic link library hello.so
Here to say a few words, in fact, all the binary information has its operating mechanism, as long as the understanding of its mechanism, and can achieve it, any time at this moment can not imagine things will become a reality. Of course, the huge chasm between the two requires top-notch design ideas and top-notch code to span.

Linux static library and dynamic library compilation

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.