Static Link Library and dynamic link library

Source: Internet
Author: User

The final executable code of the C source program must be preprocessed, assembled, compiled, linked, and linked to the compiled target code (. o file) and other target code (such as a few target files linked to an executable program) and library files linked to form an executable program.

Generally, the first line of code for writing a C program is # include <stdio. h> is used to include the header file of standard input and output. The header file is used to define some constants and declare some functions. The simplest program is.

# Include <stdio. h> <br/> int main () <br/> {<br/> printf ("Hello world! /N "); <br/> exit (0); <br/>}

In this case, we often use the command gcc-O helloworld. C during compilation to generate the executable program hellowolrd. During processing, the compiler searches the system library files (usually stored in/lib and/usr/lib) to find and stdio. h corresponding library file, complete the link to generate executable programs.

Therefore, I guess that if you understand how to link the file, you can execute the link file, and even the header file is not included.

Experiment process:

Part1: first, I will not change the source file, but specify the link to the libc. A static library file when linking.

Type the command gcc-O helloworld. c-LC (or GCC-O helloworld. c/usr/lib/libc. a,-l is short, because in Linux, static library files always start with Lib ,. at the end of a, search for the default location in short form. If not, specify the path and use the-l parameter.

-L usage: gcc-O helloworld-L/usr/lib-lC. GCC-O helloworld-L/usr/lib libc is used in the experiment. although a expresses the same meaning, it is notified to libc. A does not exist,
Gcc-O helloworld. c/usr/lib/libc. A is acceptable. It is possible that-L and-l can be used together, while-l can also be used separately.
. In fact, the former specifies the path, as long as the library file can be found, OK)

Note: The-l parameter adds the path to the search library file for the compiler. The-LC method preferentially links to the shared library, that is, the dynamic library, when there is a shared library.

The same is true for experiments:

If you use the command gcc-O helloworld. C or GCC-O helloworld. C (. l/usr/lib)-LC only obtains the executable file 4841b. If you use the command gcc-O helloworld. c/usr/lib/libc. A executable program will be 617725b, because it contains the code added when the static link is included.

 

Part2: Delete the include statement in the original file.

Run the GCC-O helloworld. c/usr/lib/libc. A or GCC-O helloworld. C-LC command to get the executable file, with only a warning.

 

Lab 2:

We all know that there are two standard methods to call a user-defined function in the main function:

1. Define or declare at least before the main function.

2. declare in a new header file, define it in a new. c file, and include the header file in the main program file. If the function is made into a library file, it can be used frequently in the future. If the function is not made into a library file, it is not necessary to create a header file, you only need to manually link the target file of the function to the main program. However, the use of header files and libraries is a standardized approach to simplify programming. Three experiments will be made into library files.

For example, the custom myprint () function

 

File 1: myprint. c

# Include <stdio. h> <br/> void myprint () <br/>{< br/> printf ("Hello myprint! /N "); <br/>}

File 3: program. c

Int main () <br/>{< br/> myprint (); <br/> exit (0); <br/>}

Gcc-O program myprint. C program. C still has a warning, but it can run. In fact, the warning is caused by the incompatibility of exit (0 ).

Therefore, for the sake of standardization: to use the include mechanism, you must first include the header file. Or you can define it directly in the main function.

 

Experiment 3: static and dynamic libraries

Tutorial objective: to create a library file for frequently used functions. You can directly include the header file when using the file later.

Part1: Create a static Link Library File

File 1: Lib. H (header file, usually placed in

/Usr/include directory, some of which may be in other directories)

Void Bill (void );

File 2: Bill. c

# Include <stdio. h> <br/> void Bill () <br/> {<br/> printf ("Hello bill! /N "); <br/>}

File 3: Main. c

# Include "Lib. H "<br/> int main () <br/>{< br/> Bill (); <br/> exit (0); <br/>}

Note: At this time, we put Lib. h and other files in the same directory, so double quotation marks are used for include. If you use angle brackets, Lib. h cannot be found (it may be the default search path)

Step 1: Compile bill. c gcc-C bill. c

Step 2: Archive ar CRV libfoo. A bill. o

Step 3: Compile main. c gcc-C main. c

Step 4: link GCC-O main. O libfoo. A (34 can be merged into: gcc-O main. c libfoo.)

You can also use-L to access the user-defined function library, but because the standard location is not saved, you must use-L to specify the path gcc-O main. O-L.-lfoo.

Now you can see the situation of the dynamic library it references:

LDD main

Continue. What if I put libfoo. A and Lib. h In the default path? Gcc-O main. c

 

For Linux programs, the program (Dynamic loader) responsible for loading the shared library and parsing the client program function reference is lD. so, or it may be a ld-linux.so.2 or ld-lsb.so.1. You can configure other locations for searching shared libraries in the file/etc/lD. So. conf.

 

Part2: create a dynamic link library file


The link of a dynamic library is like this: it does not contain function code, but references accessible shared code at runtime. When a compiled program is loaded into the memory for execution, function references are parsed and shared libraries are called. If necessary, shared libraries are loaded into the memory (for example, many useless header files are sometimes included ).

Two major advantages: 1. saving valuable memory and disk space. (Think about why ?)

2. Updates to the shared library are independent of the applications that depend on it.

 

Create a Dynamic Link Library:

Step 1: gcc-C-FPIC bill. when C is running, the called Dynamic Linked Library Function is placed somewhere in the memory, and all programs that call it point to this code segment, therefore, these codes must use relative addresses. during compilation, PIC (position independent code) is used to tell the compiler that these codes are used for dynamic link libraries.

Step 2: gcc-shared-O libfoo. So Bill. o

You can also put the FPIC parameter below: gcc-shared-FPIC-O libfoo. So Bill. o

 

Below: gcc-O main. c. /libfoo. so Note: libfoo must be specified. so location, otherwise the link may pass, but there will be a runtime error: cannot find the dynamic link library.

 

Reference: http://liuleijsjx.javaeye.com/blog/428687





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.