Dynamic library authoring under Linux and how to invoke the C function library in Python
Dynamic libraries:
Dynamic library also known as Dynamic link library English as DLL, is the short form of dynamic link library, DLL is a library containing code and data that can be used by multiple programs simultaneously, DLL is not an executable file. Dynamic linking provides a way for a process to invoke a function that is not part of its executable code. The executable code of the function is in a DLL that contains one or more functions that have been compiled, linked, and stored separately from the processes that use them. DLLs also help to share data and resources. Multiple applications can access the contents of a single copy of a DLL in memory at the same time. A DLL is a library that contains code and data that can be used by multiple programs at the same time. The dynamic library under Windows is a. dll suffix in Linux on the. so suffix.
Dynamic Library Production:
Gcc-fpic-g-c-wall A.C
Gcc-fpic-g-c-wall B.C
Gcc-shared-wl,-soname,liblusterstuff.so.1-o liblusterstuff.so.1.0.1 A.O B.O-LC
Note: can be abbreviated as: gcc-fpic-o2-shared mydll.c-o mydll.so
3. How to invoke the C language-generated dynamic library in Python:
(1): Sir into a dynamic library: as
gcc-fpic-o2-shared Print_lib.c-o print_lib.so
===================================================
struct STU
{
int num;
char name [30];
Float score;
}
void print0 (int a)
{
printf ("%d\n", a);
}
void print 2 (struct data)
{
printf ("%d \ n", data num);
printf ("%s\n", data. Name);
printf ("%f\n", data. Score);
}
void Print 3 (struct * p)
{
Print ("%d\n", p->num);
Print ("%s\n", p->name);
Print ("%f\n", p->float);
}
====================================================
(2): Python set a data type to be passed to the dynamic library
From ctypes Import *
Class Py_st (Strutcture):
_fields =
[("Num", C_int), ("name", String), ("Score", C_float)]
(3) Python uses dynamic libraries:
1.//Load Dynamic Library
FileName = "Home/zyh/tmp/print_dll.so"
Lib = cdll. LoadLibrary (FileName)
2.//define Variables
param = Py_st ()
3.//Assign a value to the structure
Param. num = 1001
Param. name = "Hello word"
Param. Score = 95.5
Lib. Print 0 (10)
Lib. Print 1 (param)
Lib. Print 2 (pointer (param))
This article from the "12690807" blog, reproduced please contact the author!
Dynamic library authoring under Linux and how to invoke the C function library in Python