3. Static Library
Sometimes you needCodeCompiled into a library, which is used in many projects, suchLibc
Is such a library, we have differentProgramWill be used in bothLibc
Library Function (for examplePrintf
).Libc
(For exampleEnviron
Variable ). This section describes how to create such a library.
We continue to useStack. c
. For ease of understandingStack. c
Split the file into four program files (although not necessary ),Main. c
It is simpler to change the header file.Stack. h
The 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.c
Compile 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 areLib
The static library starts.
As the suffix, it indicates archive.Ar
Commands are similarTar
Command, but the target file can only be packaged into a static libraryAr
Command but notTar
Command. OptionR
Add 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.S
It is used to generate a static database, indicating to create an index for the static database, which is used by the linker.Ranlib
The 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. c
Compilation 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/
TheLibraries
Is the search path list of the library file.:
. The compiler will-L
Option-L
The library specified by the option, such-Lstack
The compiler will first find the Shared LibraryLibstack. So
If 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,Main
Function and startup routine ", in the LinkLibc
The Shared Library only specifies the dynamic linker and the library files required by the program, and there is no real link, executable filesMain
Called inLibc
Library 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. c
Only calledPush
This function, so only the executable files generated by links are available.Push
WithoutPop
AndIs_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. c
Compilation 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.