Static library in C

Source: Internet
Author: User
3. Static Library

Sometimes you needCodeCompiled into a library, which is used in many projects, suchLibcIs such a library, we have differentProgramWill be used in bothLibcLibrary Function (for examplePrintf).Libc(For exampleEnvironVariable ). This section describes how to create such a library.

We continue to useStack. c. For ease of understandingStack. cSplit the file into four program files (although not necessary ),Main. cIt is simpler to change the header file.Stack. hThe code used in this section is as follows:

 
/* Stack. C */Char stack [512]; int Top =-1;
 
/* Push. C */extern char stack [512]; extern int top; void push (char c) {stack [++ top] = C ;}
/* Pop. C */extern char stack [512]; extern int top; char POP (void) {return stack [top --];}
 
/* Is_empty.c */extern int top; int is_empty (void) {return Top =-1 ;}
 
/* Stack. H */# ifndef stack_h # define stack_hextern void push (char); extern char POP (void); extern int is_empty (void); # endif
 
/* Main. C */# include <stdio. h> # include "Stack. H" int main (void) {push ('A'); Return 0 ;}

The directory structure of these files is:

 
$ Tree. | -- Main. c' -- stack | -- is_empty.c | -- pop. c | -- push. c | -- stack. c' -- stack. H1 directory, 6 files

We setStack. c,Push. c,Pop. c,Is_empty.cCompile to the target file:

 
$ Gcc-C stack/stack. c stack/push. c stack/pop. c stack/is_empty.c

Then package it into a static libraryLibstack.:

 
$ Ar Rs libstack. A stack. O push. O pop. O is_empty.oar: Creating libstack.

All library file names areLibThe static library starts.As the suffix, it indicates archive.ArCommands are similarTarCommand, but the target file can only be packaged into a static libraryArCommand but notTarCommand. OptionRAdd the following file list to the package. If the package does not exist, create it. If the package already contains a file with the same name, replace it with the new one.SIt is used to generate a static database, indicating to create an index for the static database, which is used by the linker.RanlibThe command can also create an index for a static database. The preceding command is equivalent:

 
$ Ar R libstack. A stack. O push. O pop. O is_empty.o $ ranlib libstack.

Then weLibstack.AndMain. cCompilation links:

 
$ GCC main. C-L.-lstack-istack-O main

-L indicates the compiler where to find the required library file. -L. indicates searching in the current directory. -lstack tells the compiler to link the libstack library, the -I Option tells the compiler where to find the header file. Note that the compiler will not find the library files even if they are in the current directory by default, so the -L. Option cannot be fewer. The default directory that the compiler will find can be viewed using the -print-search-dirs Option:

$ Gcc-print-search-dirsinstall:/usr/lib/GCC/i486-linux-gnu/4.3.2/programs: =/usr/lib/GCC/i486-linux-gnu/4.3.2 /: /usr/lib/GCC/i486-linux-gnu/4.3.2/:/usr/lib/GCC/i486-linux-gnu/:/usr/lib/GCC/i486-linux-gnu/4.3.2 /: /usr/lib/GCC/i486-linux-gnu/:/usr/libexec/GCC/i486-linux-gnu/4.3.2/:/usr/libexec/GCC/i486-linux-gnu /: /usr/lib/GCC/i486-linux-gnu/4.3.2/: // usr/lib/GCC/i486-linux-gnu /: /usr/lib/GCC/i486-linux-gnu/4.3.2/modules =/usr/lib/GCC/i486-linux-gnu/4.3.2/:/usr/lib/GCC/i486-linux-gnu/4.3.2 /: /usr/lib/GCC/i486-linux-gnu/4.3.2/http://www.cnblogs.com/http://www.cnblogs.com/i486-linux-gnu/lib/i486-linux-gnu/4.3.2/:/usr/lib/gcc/i486-linux-gnu/4.3.2/http://www.cnblogs.com/http://www.cnblogs.com/i486-linux-gnu/lib/../lib/:/usr/lib/gcc/i486-linux-gnu/4.3.2/http://www.cnblogs.com/../i486-linux-gnu/4.3.2/:/usr/lib/gcc/i486-linux-gnu/4.3.2/http://www.cnblogs.com/http://www.cnblogs.com/lib/:/lib/i486-linux-gnu/4.3.2/:/lib/../lib/:/usr/lib/i486-linux-gnu/4.3.2/:/usr/lib/../lib/:/usr/lib/gcc/i486-linux-gnu/4.3.2/http://www.cnblogs.com/http://www.cnblogs.com/i486-linux-gnu/lib/:/usr/lib/gcc/i486-linux-gnu/4.3.2/http://www.cnblogs.com/../:/lib/:/usr/lib/

TheLibrariesIs the search path list of the library file.:. The compiler will-LOption-LThe library specified by the option, such-LstackThe compiler will first find the Shared LibraryLibstack. SoIf yes, link it. If no, find the static library.Libstack.And link it if any. Therefore, the compiler gives priority to the shared library. If you want the compiler to only link to the static library, you can specify-Static.

What is the difference between a linked shared library and a linked static library? In section 2nd,MainFunction and startup routine ", in the LinkLibcThe Shared Library only specifies the dynamic linker and the library files required by the program, and there is no real link, executable filesMainCalled inLibcLibrary functions are still undefined symbols and need to be dynamically linked at runtime. When a static library is linked, the linker extracts the target file from the static library and truly links the executable file. We can use disassembly to view the executable file generated in the previous step.Main:

$ Objdump-D main... 08048394 <main >:8048394: 8d 4C 24 04 Lea 0x4 (% ESP), % ECx 8048398: 83 E4 F0 and $0xfffffff0, % ESP 804839b: FF 71 FC pushl-0x4 (% ECx )... 080483c0 <push>: 80483c0: 55 push % EBP 80483c1: 89 E5 mov % ESP, % EBP 80483c3: 83 EC 04 Sub $0x4, % ESP

Interestingly,Main. cOnly calledPushThis function, so only the executable files generated by links are available.PushWithoutPopAndIs_empty. This is a benefit of using a static library. The linker can extract only the required parts from the static library for link. If the target file andMain. cCompilation links:

 
$ GCC main. c stack. O push. O pop. O is_empty.o-istack-O main

the unused functions are also linked. Of course, another advantage is that you only need to write a library file name for a static library, instead of a long string of target file names.

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.