Linux gcc compilation (dynamic library, static library), linuxgcc

Source: Internet
Author: User

Linux gcc compilation (dynamic library, static library), linuxgcc

1. linux library path:/lib,/usr/lib,/usr/local/lib
2. Compile a static library in linux

A. Compile the source file
Vi pr1.c

Void print1 (){
Printf ("static print1 () callback \ n ");
}

Vi pr2.c
Void print2 (){
Printf ("static print2 () callback \ n ");
}

Vi main. c
Int main (){
Print1 ();
Print2 ();
Return 0;
}

B. Compile the library file into obj
Cc-c pr1.c pr2.c

View obj files
Ls-l pr *. o

C. link the static library (Multiple File packaging process-r append mode)
Ar-r libpr. a pr1.o pr2.o

View packaging results
Ar-t libpr.

D. compile main, use static library,-L to specify the library directory,-l to specify the library file name, and the source file libpr. a needs to be converted by default, with the header removed (lib) and tail removed (including. later), for pr. multiple libraries and multiple file names can be written into multiple-L,-l, and-o files to specify the name of the compiled output file.
Gcc-o staticdll main. c-L./-lpr

E. Run./staticdll

3. Compile the dynamic library
A. Compile the source file
Vi pr1dll. c
Void print (){
Printf ("this is dll src \ n ");
}

Vi maindll. c
Int main (){
Print ();
Return 0;
}

B. Compile the dynamic library-fPIC specifies that the dll is not related to the location,-o output path,-shared specifies the Sharing Mode
Gcc-fPIC-shared-o dll. so pr1dll. c

C. Compile the main program
Gcc-o exedll maindll. c./dll. so

D. Run:./exedll




References:
Http://www.cnblogs.com/feisky/archive/2010/03/09/1681996.html

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.