Reading comments (0) Add to favorites edit Delete
We usually make some common functions into function libraries for use by other programs. The function library is divided into two types: static library and dynamic library.
Type. 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. Dynamic
The library is not connected to the target code during program compilation, but is loaded only when the program is running.
Dynamic Inventory is also required for the row. This document uses examples to illustrate how to create static and dynamic libraries in Linux.
And 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 be output on the screen"
Hello XXX! ". Hello. H (see program 1) is the header file of the function library. Main. C (see Program 3) is
The main program calls the public function hello in the main program.
# Ifndef hello_h
# Define hello_h
Void Hello (const char * Name );
# Endif // hello_h
Program 1: Hello. h
# Include <stdio. h>
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 pass the source program Hello. C through G
CC is first compiled into a. o file.
Enter the following command at the system prompt to get the hello. o file.
# Gcc-C hello. c
#
Run the LS command to check whether the hello. o file exists.
# Ls
Hello. c hello. h hello. O main. c
#
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 created static library name is myhello, the static library file name is libmyhello.. When creating and using static databases,
Pay attention to this. 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 CRV 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 containing these public functions, and then specify the static library name when the target file is generated using the GCC command. GCC will
Connect the public functions to the target file in the static library. Note: GCC will add the prefix lib before the static library name, and then
Add the static library file name obtained by extension. A to find the static library file.
In Program 3: Main. C, we include the header file hello. H of the static library, and then directly call the Public
Use the hello function. Run the hello program to check the result.
(# Gcc-O hello main. C-L.-lmyhello ??)
# GCC main. c libmyhello. A-O main
#./Hello
Hello everyone!
#
Let's Delete the static library file and try whether the public function hello is actually connected to the target file hello.
# Rm libmyhello.
RM: Remove regular file 'libmyhello. '? Y
#./Hello
Hello everyone!
#
The program runs as usual, and the public functions in the static library are connected to the target file.
Let's continue to see how to create a dynamic library in Linux. We start with the. o file.
Step 2: create a dynamic library file from the. o file;
The naming rules for dynamic library file names are similar to those for static library file names. The prefix Lib is also added for dynamic library names, but its
The file name extension is. So. For example, if the dynamic library name is myhello, the dynamic library file name is libmyh.
Ello. So. Use GCC to create a dynamic library.
Enter the following command at the system prompt to obtain the dynamic library file libmyhello. So.
# Gcc-shared-fpci-O libmyhello. So hello. o
#
We still use the LS command to check whether the dynamic library file is generated.
# Ls
Hello. c hello. h hello. O libmyhello. So main. c
#
Step 2: Use the dynamic library in the program;
Using a dynamic library in a program is exactly the same as using a static library. The source program that uses these public functions also contains
The prototype declaration of these public functions, and then specify the dynamic library name for compilation when the target file is generated using the GCC command. We
Run the GCC command to generate the target file, and then run it to check the result.
# Gcc-O hello main. C-L.-lmyhello
#./Hello
./Hello: Error while loading shared libraries: libmyhello. So: cannot open shar
Ed object file: no such file or directory
#
Oh! Error. Check the error message. The dynamic library file libmyhello. So is not found. When the program is running,
Find the required dynamic library files in the/usr/lib and/lib directories. If found, load the dynamic library; otherwise
Indicates that the program is terminated due to a similar error. Copy the file libmyhello. So to the/usr/lib directory and try again.
Try.
# Mv libmyhello. So/usr/lib
#./Hello
Hello everyone!
#
Succeeded. This further demonstrates that the dynamic library is required when the program is running.
Let's look back and find that the GCC commands used to compile a static library and a dynamic library into a target program are exactly the same,
Which library file will the GCC command use when the static library and dynamic library have the same name? With the feeling that the question must be solved,
Let's try it.
Delete all files except. C and. h and restore them to the example program State after editing.
# Rm-F hello. O/usr/lib/libmyhello. So
# Ls
Hello. c hello. h main. c
#
Create the static library file libmyhello. A and dynamic library file libmyhello. So.
# Gcc-C hello. c
# Ar Cr libmyhello. A hello. o
# Gcc-shared-fpci-O libmyhello. So hello. o
# Ls
Hello. c hello. h hello. O libmyhello. A libmyhello. So main. c
#
Through the last LS command, we can find the static library file libmyhello. A and dynamic library file libmyhello. S.
O has been generated and all are in the current directory. Then, run the GCC command to use the function library myhello to generate the target.
File hello, and run the program Hello.
# Gcc-O hello main. C-L.-lmyhello
#./Hello
./Hello: Error while loading shared libraries: libmyhello. So: cannot open shar
Ed object file: no such file or directory
#
It is easy to know from the results of program Hello running that when the static library and dynamic library have the same name, the GCC command will give priority to the dynamic library
Status library.
Note:
Compile Parameter Parsing
The most important option is the GCC command line:
-Shared this option specifies to generate a dynamic Connection Library (let the connector generate a T-type export symbol table, and sometimes generate a weak connection W-type export symbol). External programs cannot connect without this sign. Equivalent to an executable file
L-FPIC: indicates the code compiled into a location independent, without this option, the compiled code is location-related. Therefore, during dynamic loading, the code is copied to meet the needs of different processes, rather than truly sharing code segments.
L-L.: indicates that the database to be connected is in the current directory.
L-ltest: an implicit naming rule is used by the compiler to search for a dynamically connected database. That is, add lib before the given name and. So to determine the library name.
L LD_LIBRARY_PATH: The environment variable indicates the path where the dynamic connector can load the dynamic library.
L of course, if you have the root permission, you can modify/etc/lD. so. CONF file, and then call/sbin/ldconfig to achieve the same purpose. However, if you do not have the root permission, you can only use the LD_LIBRARY_PATH output method.
When calling a dynamic library, there are several problems that may occur frequently. Sometimes, the directory where the library header files are already included through "-I, the file where the library is located is guided by the "-l" parameter and the database name of "-l" is specified. However, when you run the LDD command to view the file, you cannot find the so file with the specified link, in this case, you need to modify LD_LIBRARY_PATH or/etc/lD. so. CONF file to specify the directory of the dynamic library. This usually solves the problem that the database cannot be linked.
We usually make some common functions into function libraries for use by other programs. The function library is divided into two types: static library and dynamic library.
Type. 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. Dynamic
The library is not connected to the target code during program compilation, but is loaded only when the program is running.
Dynamic Inventory is also required for the row. This document uses examples to illustrate how to create static and dynamic libraries in Linux.
And 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 be output on the screen"
Hello XXX! ". Hello. H (see program 1) is the header file of the function library. Main. C (see Program 3) is
The main program calls the public function hello in the main program.
# Ifndef hello_h
# Define hello_h
Void Hello (const char * Name );
# Endif // hello_h
Program 1: Hello. h
# Include <stdio. h>
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 pass the source program Hello. C through G
CC is first compiled into a. o file.
Enter the following command at the system prompt to get the hello. o file.
# Gcc-C hello. c
#
Run the LS command to check whether the hello. o file exists.
# Ls
Hello. c hello. h hello. O main. c
#
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 created static library name is myhello, the static library file name is libmyhello.. When creating and using static databases,
Pay attention to this. 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 CRV 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 containing these public functions, and then specify the static library name when the target file is generated using the GCC command. GCC will
Connect the public functions to the target file in the static library. Note: GCC will add the prefix lib before the static library name, and then
Add the static library file name obtained by extension. A to find the static library file.
In Program 3: Main. C, we include the header file hello. H of the static library, and then directly call the Public
Use the hello function. Run the hello program to check the result.
(# Gcc-O hello main. C-L.-lmyhello ??)
# GCC main. c libmyhello. A-O main
#./Hello
Hello everyone!
#
Let's Delete the static library file and try whether the public function hello is actually connected to the target file hello.
# Rm libmyhello.
RM: Remove regular file 'libmyhello. '? Y
#./Hello
Hello everyone!
#
The program runs as usual, and the public functions in the static library are connected to the target file.
Let's continue to see how to create a dynamic library in Linux. We start with the. o file.
Step 2: create a dynamic library file from the. o file;
The naming rules for dynamic library file names are similar to those for static library file names. The prefix Lib is also added for dynamic library names, but its
The file name extension is. So. For example, if the dynamic library name is myhello, the dynamic library file name is libmyh.
Ello. So. Use GCC to create a dynamic library.
Enter the following command at the system prompt to obtain the dynamic library file libmyhello. So.
# Gcc-shared-fpci-O libmyhello. So hello. o
#
We still use the LS command to check whether the dynamic library file is generated.
# Ls
Hello. c hello. h hello. O libmyhello. So main. c
#
Step 2: Use the dynamic library in the program;
Using a dynamic library in a program is exactly the same as using a static library. The source program that uses these public functions also contains
The prototype declaration of these public functions, and then specify the dynamic library name for compilation when the target file is generated using the GCC command. We
Run the GCC command to generate the target file, and then run it to check the result.
# Gcc-O hello main. C-L.-lmyhello
#./Hello
./Hello: Error while loading shared libraries: libmyhello. So: cannot open shar
Ed object file: no such file or directory
#
Oh! Error. Check the error message. The dynamic library file libmyhello. So is not found. When the program is running,
Find the required dynamic library files in the/usr/lib and/lib directories. If found, load the dynamic library; otherwise
Indicates that the program is terminated due to a similar error. Copy the file libmyhello. So to the/usr/lib directory and try again.
Try.
# Mv libmyhello. So/usr/lib
#./Hello
Hello everyone!
#
Succeeded. This further demonstrates that the dynamic library is required when the program is running.
Let's look back and find that the GCC commands used to compile a static library and a dynamic library into a target program are exactly the same,
Which library file will the GCC command use when the static library and dynamic library have the same name? With the feeling that the question must be solved,
Let's try it.
Delete all files except. C and. h and restore them to the example program State after editing.
# Rm-F hello. O/usr/lib/libmyhello. So
# Ls
Hello. c hello. h main. c
#
Create the static library file libmyhello. A and dynamic library file libmyhello. So.
# Gcc-C hello. c
# Ar Cr libmyhello. A hello. o
# Gcc-shared-fpci-O libmyhello. So hello. o
# Ls
Hello. c hello. h hello. O libmyhello. A libmyhello. So main. c
#
Through the last LS command, we can find the static library file libmyhello. A and dynamic library file libmyhello. S.
O has been generated and all are in the current directory. Then, run the GCC command to use the function library myhello to generate the target.
File hello, and run the program Hello.
# Gcc-O hello main. C-L.-lmyhello
#./Hello
./Hello: Error while loading shared libraries: libmyhello. So: cannot open shar
Ed object file: no such file or directory
#
It is easy to know from the results of program Hello running that when the static library and dynamic library have the same name, the GCC command will give priority to the dynamic library
Status library.
Note:
Compile Parameter Parsing
The most important option is the GCC command line:
-Shared this option specifies to generate a dynamic Connection Library (let the connector generate a T-type export symbol table, and sometimes generate a weak connection W-type export symbol). External programs cannot connect without this sign. Equivalent to an executable file
L-FPIC: indicates the code compiled into a location independent, without this option, the compiled code is location-related. Therefore, during dynamic loading, the code is copied to meet the needs of different processes, rather than truly sharing code segments.
L-L.: indicates that the database to be connected is in the current directory.
L-ltest: an implicit naming rule is used by the compiler to search for a dynamically connected database. That is, add lib before the given name and. So to determine the library name.
L LD_LIBRARY_PATH: The environment variable indicates the path where the dynamic connector can load the dynamic library.
L of course, if you have the root permission, you can modify/etc/lD. so. CONF file, and then call/sbin/ldconfig to achieve the same purpose. However, if you do not have the root permission, you can only use the LD_LIBRARY_PATH output method.
When calling a dynamic library, there are several problems that may occur frequently. Sometimes, the directory where the library header files are already included through "-I, the file where the library is located is guided by the "-l" parameter and the database name of "-l" is specified. However, when you run the LDD command to view the file, you cannot find the so file with the specified link, in this case, you need to modify LD_LIBRARY_PATH or/etc/lD. so. CONF file to specify the directory of the dynamic library. This usually solves the problem that the database cannot be linked.