In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
1. Related knowledge
(1) whether it is making a static library or a dynamic library, the .o files must be packaged.
(2) in the / lib64 directory, static library file .a, dynamic library file .so
2. Static library
(1), gcc-c add.c-> generate an add.o file, where the parameter (- c)
(2) static library is created by command ar; ar-cr add.an add.o
(3) there are two ways to compile static libraries:
A:gcc test.c-o test-L. Add.a / / must add-L at compile time every time. Static library file
B, put the .a file (at this time, the .a file must start with lib) under the / lib64 directory, and do not add-L. Parameter, but add a static library file (- lxxxx) at run time
Sample implementation
Step 1: package it into a .o file
Step 2: create a .a file
Step 3 1: pass-L under the current directory. .a file for compilation
Step 4: run the result
Step 3 2: do not add-L below the current directory. When the .a file is compiled, you must rename it to libshow.a; and move it to the / lib64 directory, adding the parameter-lshow at compile time
Step 4: run the result
The above is the production of static libraries. Follow the prescribed process, and it is best to make them all as libxxxxx.a:
A, Mr. Cheng. O document
B, packaged into libxxxx.a file
C, there are two ways to compile at this time, and the current directory uses-L. Compile with libxxx.an or-lxxxx
D, as long as the compilation is successful, it can be executed all over the world-> because the files compiled using static function libraries are relatively large.
3. The making of dynamic library
.so: it has an advantage in version upgrade; core parameter:-shared-fpic
Gcc-shared-fpic- o libshow.so show.c:-fpic- > generate location-independent code-shared:- > generate shared libraries
Sample implementation
Step 1: implement the creation of libxxxx.so files
Step 2: move the libxxxx.so file to / lib64
Step 3: run the compilation
Note:
(1) the dynamic library can only be compiled and run in this way. The .so file must be placed in the / lib64 directory and the file name must be libxxx.so.
(2) you can compile and run successfully without putting the libxxx.a file of the static library.
4. Dynamic loading
It is very efficient when it is loaded into memory at run time, and an option should be added at the end of compilation:-ldl
Done through a series of API
It is realized in C language:
Step 1: create a libxxx.so file first
Step 2: write the loaded program
Test.c
# include "show.h" # includetypedef void (* pFun) (char *); / / define the function pointer int main (void) {void * D1 = dlopen ("libshow.so", RTLD_LAZY) that implements the method; / / Open the dynamic library file if (D1 = = NULL) {perror ("dlopen"); return-1;} pFun pfun = (pFun) dlsym (D1, "show") / / find the function pfun ("abcd") named show; / / once found, the function pointer can be called dlclose (D1); return 0;}
Step 3: compile and run directly, adding the parameter-ldl at the end
In this way, the method of dynamically loading .so files is realized.
Use C++ to achieve:
(1) step 1: create a libxxx.so file
(2) step 2: move to / lib64
(3) step 3: compile and run
A segment error was sent at this time
Analysis:
(1) there is no problem in finding function names in C language.
(2) C++ can not find the function name in the dynamic link library; because C++ is overloaded, its function name is no longer the function name we see.
Solution:
(1). Find the corresponding function name through assembly.
(2), extern "C", extend C, use the characteristics of C
The improved code is as follows:
/ / show.h#ifndef _ SHOW_H_#define _ SHOW_H_#include#includeusing namespace std;extern "C" void show (char * str) / / extend C to have the properties of C (interpret this function as C), and the function name is what we see. To find the function name # endif////show.cpp#include "show.h" extern "C" void show (char * str) {cout
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.