Linux GCC compiles dynamic class libraries (. So) and static class libraries (. a)

Source: Internet
Author: User
Tags static class

Linux GCC compiles dynamic class libraries (. So) and static class libraries (. a)

My compilation environment for Ubuntu desktop 16.04

One: Test code

The test has 3 files: ab.h,ab.c,test.c

//AB.hvoidhello ();//AB.C#include <stdio.h>voidHello () {printf ("Hello from AB.C \ n");}//test.c#include <stdio.h>#include"AB.h"voidMainvoid) {printf ("It is main\n"); Hello ();}
View Code

Using GCC, compile and run to display the results:

[Email protected]:~/desktop/demo$ gcc AB.C test.c
[Email protected]:~/desktop/demo$./a.out
It is main
Hello from AB.C

Second: GCC compiles the static class library. A

//compile point o file[Email protected]:~/desktop/demo$GCC-C AB.C//compile to AB.O file[Email protected]:~/desktop/demo$lsab.c AB.h ab.o a.out test.c//package as. A file[Email protected]:~/desktop/demo$ar-CRV LIBAB.A AB.O a-AB.O//Compile test program, test LIBAB.A[Email protected]:~/desktop/demo$GCC-o testliba test.c libab.a//Test procedure Testliba[Email protected]:~/desktop/demo$lsab.c AB.h ab.o a.out libab.a test.c Testliba//run the test and the output is consistent with the above[Email protected]:~/desktop/demo$./Testliba It is mainhello from AB.C [email protected]:~/desktop/demo$

Three: GCC compiles the dynamic class library. So

//compile AB.C as a dynamic class library libab.so[Email protected]:~/desktop/demo$GCC-shared-o libab.so-FPIC AB.C//View[Email protected]:~/desktop/demo$lsab.c AB.h ab.o a.out libab.a libab.so test.c Testliba//Compile the test program Testso and link the libab.so in the current directory[Email protected]:~/desktop/demo$GCC-O Testso Test.c-lab-L.//set the dynamic class library path[Email protected]:~/desktop/demo$ Export ld_library_path=.: $LD _library_path//run the test program with the results consistent with the above[Email protected]:~/desktop/demo$./Testso It is mainhello from AB.C [email protected]:~/desktop/demo$

External supply, only need to put AB.h and libab.a or libab.so can;

Reference: http://www.cnblogs.com/ymy124/archive/2012/04/13/2446434.html

Linux GCC compiles dynamic class libraries (. So) and static class libraries (. a)

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.