[Linux] - Generation and dynamic libraries and static libraries

Generate dynamic libraries and static libraries

First we understand at first why use the library? For chestnuts, when we want to use your own code to others when they do not want to give people the source code, so it will be packaged into your own code library, then others will only use your method from the library, but can not see your source.

1, the concept of

DLL: is the dynamic link library. To .so file extension. In the program compiled and it will not be linked to the object code, but is loaded only when the program runs , the program runs in when needed dynamic inventory.
Static library: The .a suffix name, the library code into an executable program which, without relying on the presence of runtime libraries . When the program runs more will form a code redundancy.
Libraries: a lot of code to implement packaged into a file, library code can not have a main function

2, a dynamic library of

Use tools: gcc
gcc -o child.o -fPIC -c child.c
gcc -o Child --share child.o
presentation:
first of all generate three files are child.c child.h main.c and
now you want to it is packaged as a dynamic library, enter the following command at the command line:

Here Insert Picture Description
The first line is to be compiled, converting .c files into .o object files.
Share of the second line means is to generate a dynamic library, -fpic is to generate position independent code, because the code is loaded when the dynamic library dynamic run, the dynamic library is to be mapped to each their own independent virtual address space of the program can not guarantee that every program will load his dynamic library to the same location, so easy to -fpic mapped to each address.
libchild.so mean: lib prefix, child is the name for ourselves from the dynamic library, .so suffix dynamic library is generated.

3, generate static library

Tools: gcc / ar
command:
GCC -C child.c object files -o child.o //
ar -cr libchild.a child.o ...
Here Insert Picture Description
directly using the code in the command line ar -cr libchild.a child .o, you will find two files are generated libchild.a and libchild.so, which generates a static library. libchild the lib prefix, child we played a static library name

4. How to use?

It is to use the words of our libraries and .o files into one to produce an executable program.
Use of the library, two cases are
linked when using the library (three ways):
When we write command: gcc -o main main.o -lchild shows that we link this library is a child, you will find error: can not find -lchild
then we have doubts, Ku Mingming in that way, why you can not find it? Find the library because there is a default path, but this default path does not contain the current path, the root directory has a lot of dynamic and static libraries, so there is a first approach, we will link the library into the specified the path:
(1) a library file into the specified pathHere Insert Picture Description
the first line of said generated file to the root directory of the library to which lib64 found successful compilation is re-executed command to compile the second row. But under normal circumstances we will not put the library under the specified path, it will contaminate the system root directory, so there will be a second way, put our own specified directory.
(2) LIBRARY_PATH set environment variables
as shown below, first we have just deleted the lib64 library, recompile find a compilation error (a method to prevent interference on)
Here Insert Picture Description
this time we will be able to set your own environment variables
Here Insert Picture Description
./ current directory If the environment variable is directly equal to the current directory, then the value will cover before, so we add $ LIBRARY_PATH, again compiled can be found success.
(3) set the link library by gcc -L option is the default search path
command: gcc -o main main.o -L ./-lchild this method is the most commonly
following is a third way: which is designated -lchild the name of the link library, -l is specified library name of the child to the library from our own name, -L is specified library search path is the current directory ./
Here Insert Picture Description
After executing the command instruction compiles successfully found

When we use the command ./main runtime, found an error, it is necessary to use the following methods to solve the
operation when using the library : only a dynamic library will be loaded at runtime library, the following two methods
(1) place the library file to a specified path
(2) to set the LD_LIBRARY_PATH environment variable
here is the second demonstration, again compiled main.c file compiled successfully found
Here is the second demonstration
more than is generated with the use of dynamic and static libraries.
Today to share here, please continue to focus on follow-up to the author, inter-process communication -

Published 33 original articles · won praise 13 · views 1066

Guess you like

Origin blog.csdn.net/Vicky_Cr/article/details/103093210