In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to generate dynamic libraries under the Linux system". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
What is a dynamic library?
Dynamic library is also called dynamic link library (DLL), which is the abbreviation of Dynamic Link Library. DLL is a library that contains 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 executable code for a function is located in a DLL that contains one or more functions that have been compiled, linked, and stored separately from the processes that use them. DLL also helps to share data and resources. Multiple applications can simultaneously access the contents of a single copy of DLL in memory. DLL is a library that contains code and data that can be used by multiple programs simultaneously.
I. brief introduction
The extension of the dynamic library file under Linux is ".so" (Shared Object). By convention, all dynamic library file names are in the form of libname.so (version number may be added to the name). In this way, the thread function library is called libthread.so. The file name of the static library is libname.a. The file name of the shared archive is libname.sa. Shared archive is just a transitional form that helps people move from static libraries to dynamic libraries.
The editor synthesizes his own learning experience and the better content on the network, and introduces the generation and linking method of dynamic library files with simple examples.
Operating system: Ubuntu 14.0.4
GCC version: 4.1.3
II. Library file and test file code
The directory where the library files and test files are located: / home/test/program/
1. Kuwen file name: myLibSrc.c
The code is as follows:
/ *
Filename: myLibSrc.c
* /
# include
# include "myLibInclude.h"
Int
MyLibSrcFun () {
Printf ("There is myLibSrcFun ()\ n")
Return 0
}
2. Test file: main.c
The code is as follows:
/ *
Filename: main.c
* /
# include
# include "myLibInclude.h"
Int main (intargc, char** argv) {
Printf ("Main function!\ n")
/ / call the functions in the loaded dynamic library
MyLibSrcFun ()
Return 0
}
3. Header file: myLibInclude.h
/ *
Filename: myLibInclude.h
* /
IntmyLibSrcFun (); / / declare the function
Third, the compilation method of dynamic library
The myLibSrc.c command for compiling library files is 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, a dynamic library file is generated under 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 the permission of a super user, you can use sudo or su root to enter the root user password to switch. In view of the fact that when individuals learn to use it, at the same time, many operations need to use root users, so compile directly under root users.
B. the meaning of parameters when compiling and generating dynamic library
-fpic: generated so that the output object module can be relocatable.
-shared: specifies that the corresponding source file is generated into the corresponding dynamic link library file.
4. Testing method of dynamic library
Compile test file: main.c
The code is as follows:
$gcc-o app main.c / home/test/program/ libmyLib.so
Run. / app after successful compilation:
The code is as follows:
Main function!
There is myLibSrcFun ()
It is important to note that:
1. The command $gcc-o app main.c / home/test/program/ libmyLib.so compiled above
Is to specify the absolute path to a specific linked library file The absolute path to the library file in this example is / home/test/program/ libmyLib.so
Of course, if you want to link dynamic libraries from the system's library file path (usually the system function libraries are located in the / usr/lib directory), you can first 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 for the linking method: for the last argument in $gcc-o app main.c-lmyLib-lmyLib, it can be seen that the command-line argument passed to the C compiler does not mention the full path to the library, or even the full name of the file in the library directory! In fact, the compiler is told that according to the option-lmyLib to link to the corresponding function library (under / usr/lib), the name of the function library is libmyLib.so, that is, the extension of the "lib" section and the file are omitted, but preceded by an'l '.
This is the end of the content of "how to generate dynamic libraries under the Linux system". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.