Static library in C

Source: Internet
Author: User

Sometimes you need to compile a set of code into a library, which is used in many projects. For example, libc is such a library, we use library functions in libc (for example, printf) in different programs, and variables in libc (for example, environ variables to be discussed later ). This section describes how to create such a library.

We continue to use the example of stack. c. For ease of understanding, we set the stack. c. c is simpler. the header file stack. h remains unchanged. 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_H
Extern 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. h

1 directory, 6 files we compile stack. c, push. c, pop. c, is_empty.c into the target file:

$ Gcc-c stack/stack. c stack/push. c stack/pop. c stack/is_empty.c and package it into a static library libstack.:

$ Ar rs libstack. a stack. o push. o pop. o is_empty.o
Ar: creating libstack. a library names all start with lib, and static libraries use. a as the suffix, indicating Archive. The ar command is similar to the tar command and serves as a package. However, you can only use the ar command instead of the tar command to package the target file into a static library. Option r: 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 is used to generate a static database, which indicates creating an index for the static database. This index is used by the linker. The ranlib command can also create an index for a static library. The preceding command is equivalent:

$ Ar r libstack. a stack. o push. o pop. o is_empty.o
$ Ranlib libstack. a then we link libstack. a and main. c together:

$ Gcc main. c-L.-lstack-Istack-o main-L option tells the compiler where to find the required library file.-L indicates to find the file in the current directory. -Lstack tells the compiler to connect to the libstack library, and the-I option tells the compiler where to find the header file. Note: even if the library file is in the current directory, the compiler will not find it 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-dirs
Install:/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 /.. /.. /.. /.. /i486-linux-gnu/bin/i486-linux-gnu/4.3.2/:/usr/lib/gcc/i486-linux-gnu/4.3.2 /.. /.. /.. /.. /i486-linux-gnu/bin/
Libraries: =/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 /.. /.. /.. /.. /i486-linux-gnu/lib/i486-linux-gnu/4.3.2/:/usr/lib/gcc/i486-linux-gnu/4.3.2 /.. /.. /.. /.. /i486-linux-gnu/lib /.. /lib/:/usr/lib/gcc/i486-linux-gnu/4.3.2 /.. /.. /.. /i486-linux-gnu/4.3.2/:/usr/lib/gcc/i486-linux-gnu/4.3.2 /.. /.. /.. /.. /lib/:/lib/i486-linux-gnu/4.3.2/:/lib /.. /lib/:/us R/lib/i486-linux-gnu/4.3.2/:/usr/lib /.. /lib/:/usr/lib/gcc/i486-linux-gnu/4.3.2 /.. /.. /.. /.. /i486-linux-gnu/lib/:/usr/lib/gcc/i486-linux-gnu/4.3.2 /.. /.. /.. //:/lib/:/usr/lib/among which libraries is the search path list of the library file, separated by the ":" symbol. The compiler searches for libraries specified by the-L option in these search paths and the path specified by the-l option, for example,-lstack. The Compiler first checks whether the shared library libstack exists. so, if there is a link to it, if not, find whether there is a static library libstack. a. link it if any. Therefore, the compiler gives priority to shared libraries. If you want the compiler to only link to static libraries, you can specify the-static option.

What is the difference between a linked shared library and a linked static library? As mentioned in section 2nd "main function and startup routine", when linking the libc shared library, the dynamic linker and the library files required by the program are specified, and there is no real link, the libc library function called in the main of the executable file is still undefined. Dynamic Links should be made 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 is interesting, main. c Only calls the push function, so only push and is_empty are available in the executable file generated by the link. 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 files and main. c files are directly linked together:

$ Gcc main. c stack. o push. o pop. o is_empty.o-Istack-o main, functions that are not used will also be 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.

 

Related Article

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.