Dynamic library authoring under Linux and how to invoke the C function library in Python

Source: Internet
Author: User

Dynamic library authoring under Linux and how to invoke the C function library in Python

    1. 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.

    2. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.