When writing a program in a Linux environment, APIs in the glibc library are often called. How are these libraries implemented? Many beginners have been confused about this. Next we will work with you to learn how to create static and shared libraries.
Author: canghai hunter Source: http://blog.csdn.net/embedded_hunter reproduced please indicate the source of embedded technology exchange QQ group: 179012822
First, we write code to implement the task of "entering the radius to find the area and perimeter of the circle. Let's take a look at the content of my other articles:
- C language programming practices in Linux _ 01
In this article, http://blog.csdn.net/embedded_hunter/article/details/6838338 is directly implemented in the main function. Very simple.
- Linux environment C language programming practice _ 02 http://blog.csdn.net/embedded_hunter/article/details/6838352 in this article, through the function method to achieve the task requirements, the main function and common functions written in the same C source file.
- C language programming in Linux _ 03
Http://blog.csdn.net/embedded_hunter/article/details/6838372 in this article, or through the function of implementation of the task requirements, but the main function and common functions are placed in different C source files.
Next we want to make the area function of the circle and the circumference function of the circle into a database. The main function calls our custom database directly.
The following describes how to create a static library and a shared library. Before introducing the production method, let's take a look at the concepts and differences between static and shared libraries.
First, both static and shared libraries are binary code. In Linux, these libraries and executable files are all elf files. (For ELF format files, I will not introduce them in detail here. Please read other materials on your own. If you have the opportunity, I will write an article about ELF format files separately)
A source program is converted to an executable file and must go through the compilation and link phases. Compiling is to convert the source file into the target code. The link is to "combine" the target code with other target files or libraries to generate executable programs.
The main difference between a static library and a shared library lies in the Link process. When a static library is connected, the code is statically inserted into the executable file, so the executable file is large, in addition, the executable file does not depend on the static library at runtime, while the shared library does not insert the code into the executable file. Therefore, the executable file will depend on the shared library when running.
I. Static Library Creation Method
"C language programming practice _ 03 in Linux" contains two source files and one header file: myprograme. C, mylib. H, and mylib. C. Make mylib. C as a static library.
1. Generate the target code, which will be used later when creating a static library.
# Gcc-C mylib. c
The mylib. O target file is generated.
2. Create a static library
# Ar-Cr libmylib. A mylib. o
Libmylib. A is the file name of the static library and must start with Lib. The *** in lib ***. A is the library name. The static library must be named according to the Lib [name]. A rule.
You can use the AR command to generate a static library.
3. Use static libraries when linking programs
# Gcc-O main. C-L.-lmylib
The mylib static library is linked during the main. c file compilation link. The Library name is specified with-l, for example,-lmylib. The static library location is specified with-l, for example,-l .. indicates the current path. This option cannot be omitted.
Note: The compiler searches for shared libraries by default. If a shared library with the same name exists in this directory, the compiler uses the shared library for link by default. If you want to use a static library, you can use the-static option to specify it.
4. Run the program
#./Main to run
In this case, the program can run even if libmylib. A is deleted. A shared database cannot be deleted.
Ii. How to create a shared library
1. Create a shared library
# Gcc-shared-FPIC-O libmylib. So mylib. c
Libmylib. So is the file name of the static library. It must start with Lib. The *** in lib ***. So is the library name.
-Shared Library generation
-The FPIC generation location-independent code. Consider what is irrelevant to the location.
2. Use a shared library
# Gcc-O main1 main. C-L.-lmylib
3. Run the program
In this case, you cannot directly run the program because the program uses a shared library and you need to specify the location of the shared library. By default, the paths for searching shared libraries are/usr/lib and/lib.
There are multiple ways to solve this problem:
(1) copy the Shared Library to the/usr/lib or/lib directory.
(2) set the environment variable LD_LIBRARY_PATH
(3) modify the configuration file/etc/lD. So. conf.
(4) Specify the shared library path during compilation. Option-rpath
Here we use the second method:
# Export LD_LIBRARY_PATH =/home/Linux/: $ LD_LIBRARY_PATH
#./Main1
4. Check which libraries the executable files depend on.
# LDD main1