Dynamic library can save disk and memory space, make program upgrade easier, increase CPU cache hit rate, etc. The following small series shows you how to generate Linux dynamic library through examples. Let's take a look.
What is the dynamic library?
Dynamic library, also known as dynamic link library, is a short form of Dynamic Link Library. DLL is a library containing code and data that can be used by multiple programs at the same time. DLL is not an executable file. Dynamic linking provides a way for a process to call functions that are not part of its executable code. The function's executable code is located in a DLL that contains one or more functions that have been compiled, linked, and stored separately from the process that uses them. DLLs also help to share data and resources. Multiple applications can simultaneously access the contents of a single DLL copy in memory. A DLL is a library of code and data that can be used simultaneously by multiple programs.
I. Brief description
The extension of the dynamic library file under Linux is “.so”(Shared Object). By convention, all dynamic library file names are of the form libname.so (possibly adding a version number to the name). Thus, the thread function library is called libthread.so. The file name of the static library is libname.a. The file name form of the shared archive is libname.sa. Shared archive is just a form of transition that helps people move from static to dynamic.
Xiaobian integrates his own learning experience and better content on the network, and introduces the method of generating and linking dynamic library files with simple examples.
Operating System: Ubuntu 14.0.4
GCC Version: 4.1.3
Second, Library File and Test File Code
Library File and Test File Directory: /home/test/program/
1. Library file name: myLibSrc.c
The code is as follows:
/*
filename : myLibSrc.c
*/
#include"stdio.h"
#include“myLibInclude.h”
int
myLibSrcFun(){
printf(“There is myLibSrcFun()\ ”);
return 0;
}
2 File: main.c
The code is as follows:
/*
filename: main.c
*/
#include Stdio.h"
#include“myLibInclude.h”
int main(intargc, char** argv){
printf(“Main function !\ ” );
//Call the function in the loaded dynamic library
myLibSrcFun();
Return 0;
}
"strong" 3, header file: myLibInclude.h "/strong"
/*
filename: myLibInclude.h
*/
intmyLibSrcFun();//Declare the function
Third, the dynamic library compilation method
Compile the library file myLibSrc.c command as follows :
The code is as follows:
$ gcc myLibSrc.c -shared -o libmyLib.so
or: $ gcc myLibSrc.c -fpic-shared -g -DDEBUG -o libmyLib.so
If the compilation is successful, the dynamic library file will be generated in the directory /home/test/program/: libmyLib.so
There are two points to add:
A. For Linux operations, it is generally recommended to use the normal user mode. If you need superuser privileges, you can use sudo or su root to enter the root user password switch. In view of the individual learning and use, many operations need to use the root user, so compile directly under the root user.
B, the meaning of the parameters when compiling the dynamic library
-fpic: The output object module can be relocated by address method.
-shared: Specifies to generate the corresponding dynamic link library file for the corresponding source file.
Fourth, the dynamic library test method
Compile the test file: main.c
The code is as follows:
$gcc–o app main.c /Home/test/program/libmyLib.so
Run after successful compilation. /app :
The code is as follows:
Main function !
There is myLibSrcFun()
Note:
1. The above compiled command $gcc –o app main.c /home/test/program/The last parameter of libmyLib.so
is the absolute path that specifies the particular connected library file. The absolute path of the library file in this example is /home/test/program/libmyLib.so
Of course, if you want to access the library file path from the system (usually the system function library is located in the /usr/lib directory) If you link the dynamic library, you can copy the generated library file to /usr/lib and then link:
The code is as follows:
$cp libmyLib.so /usr/lib/libmyLib.so
$gcc –o app main.c -lmyLib
Here, a simple explanation of the link method: for $gcc –o app main.c -lmyLib The last parameter in the –lmyLib, visible command line arguments passed to C compiler does not mention the full path of the function library, not even mention the full name of the file in the function library directory! In fact, the compiler is told to link to the corresponding function library (under /usr/lib) according to the option -lmyLib. The name of the function library is libmyLib.so, that is, the extensions of the <quo;lib” sections and files are Omitted, but added a ‘l’ in front.
The above is an example of the dynamic library generation under Linux. Through these examples, you can learn more about how the Linux dynamic library is generated, and hope to help beginners.
in CentOS6.5 system Docker is an advanced capacity engine based on LXC. It can be used in the curre
Rescue mode, as its name suggests, is to maintain the Linux system mode. When the /
Oracle often has garbled characters under Linux system, no matter whether it is ins
In a Linux system, opening a file is as simple as a startup icon, but how to open a file in the term
Linux vim comment color can't see how to change?
How can I use email to send emails under Linux lnmp?
CentOS uses the split command to split the file instance
Ubuntu system remote boot operation example
How to use the sync command on Linux
How to use dd command in Linux system
How to install and use SoundCloud on Linux
How to configure the permissions of the Linux system sudo command?
Naruto OL landlord ninja exam 130 layer how to play customs clearance Raiders
How does Win10 prohibit users from manually searching for updates?
How does the Win8 system modify the time format?
Ubuntu 14.04 install java method
The use of the Linux primary command useradd and detailed
How to install Haier one-click recovery?
How to turn off the web accelerator under Windows 7
OpenIPMP test environment installation basic tutorial under Windows
View win10 computer configuration and system basic information method