Article title: create static and dynamic libraries in Linux. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
We usually make some common functions into function libraries for use by other programs. Function libraries can be divided into static libraries and dynamic libraries. The static library will be connected to the target code during program compilation. this static library is no longer needed when the program is running. The dynamic library is not connected to the target code during program compilation, but is loaded only when the program runs. Therefore, dynamic inventory is required when the program runs. This document provides examples to illustrate how to create static and dynamic libraries in Linux and how to use them.
Before creating a function library, we first prepare the source program for example and compile the source program of the function library into a. o file.
Step 2: Edit the program named hello. h, hello. c, and main. c;
Hello. c (see program 2) is the source program of the function library, which contains the public function hello, which will output "Hello XXX!" on the screen! ". Hello. h (see Program 1) is the header file of the function library. Main. c (see program 3) is the main program of the test library file, and the public function hello is called in the main program.
# Ifndef HELLO_H
# Define HELLO_H
Void hello (const char * name );
# Endif // HELLO_H
Program 1: hello. h
# Include
Void hello (const char * name)
{
Printf ("Hello % s! \ N ", name );
}
Program 2: hello. c
# Include "hello. h"
Int main ()
{
Hello ("everyone ");
Return 0;
}
Program 3: main. c
Step 2: compile hello. c into a. o file;
Both static and dynamic libraries are created by the. o file. Therefore, we must first compile the source program hello. c into a. o file through gcc.
Enter the following command at the system prompt to get the hello. o file.
# Gcc-c hello. c
#
(Note 1: This article does not introduce the usage of each command and its parameter functions. if you want to learn more about them, see other documents .)
(Note 2: The first character "#" is a system prompt and does not need to be typed. The following is the same .)
Run the ls command to check whether the hello. o file exists.
# Ls
Hello. c hello. h hello. o main. c
#
(Note 3: The first character "#" is the system running result, which is the same as the following .)
In the ls command result, we can see the hello. o file. This step is complete.
Next, let's take a look at how to create a static library and use it.
Step 2: Create a static library from the. o file;
The naming rules for static library file names are prefixed with lib, followed by the static library name and the extension is.. For example, if the static library name is myhello, the static library file name is libmyhello.. Pay attention to this when creating and using static databases. Use the ar command to create a static library.
Enter the following command at the system prompt to create the static library file libmyhello..
# Ar cr libmyhello. a hello. o
#
Run the ls command to view the result:
# Ls
Hello. c hello. h hello. o libmyhello. a main. c
#
The ls command result contains libmyhello..
Step 2: Use static libraries in programs;
After the static library is created, how can I use its internal functions? You only need to include the prototype declaration of these public functions in the source program using these public functions, and then specify the static library name when generating the target file using the gcc command, gcc will connect the public functions to the target file from the static library. Note: gcc adds the prefix lib to the static library name, and appends the static library file name with the extension. a to find the static library file.
[1] [2] Next page