python how to insert C language runtime?

https://www.cnblogs.com/si-lei/p/10748612.html

 

1. python in how to insert C language runtime?

 (1) written in C language code:

Copy the code
#include <stdio.h> 

void CFUN () 
{ 
    printf ( "--------- I c language: ----------- \ the n-"); 
    printf ( "the Hello world ! \ the n-"); 
    printf (" ------------------------------ \ the n-"); 
}
Copy the code

(2) the c language compiler for the dynamic library:

  Use the command: gcc cfun.c -fPIC -shared -o lib_cfun.so

  Which - Shared parameter indicates generate dynamic link library.

  -fPIC parameter indicates compiled as position-independent code, the code is compiled without this option if the location is relevant; so when dynamically loaded by way of a copy of the code to meet the different calls, but can not achieve a real sharing of code snippets purpose.

  - O parameter indicates rename lib_cfun.so.

(3) write python code:

Copy the code
* Import ctypes from 
from the Thread Threading Import 

to load dynamic library # 
lib = cdll.LoadLibrary ( "./ lib_cfun.so") 

# create a child thread, let it perform c language function 
t = Thread (target = lib.CFun) 
t.start () 

# main thread 
print ( "---------------- I'm a python language! ------------------ --- ") 

Print (" ---------------- the Hello Python! --------------------- ")
Copy the code

(4) perform python code:

Guess you like

Origin www.cnblogs.com/fyly/p/11266308.html