You can also use Cython to implement
1 Download Cython, install with Python setup.py installation
21 instances
① Creating the HelloWorld directory
Create a Helloworld.pyx with the following content:
cdef extern from"Stdio.h":
extern int printf (const char *format, ...)
def SayHello ():
printf ("hello,world\n")
② compiled, the most convenient is the use of Python distutils,
Create setup.py under the HelloWorld directory, as follows:
from Distutils.core import Setup
from distutils.extension import Extension
from Cython.build Import cythonize
Setup
Name = 'helloworld',
Ext_modules=cythonize ([
Extension ("HelloWorld", ["Helloworld.pyx"]),
]),
)
Compile:
Python setup.py Build
Installation:
Python setup.py Install
After installation, it will be in Build/lib.??? Helloworld.pyd copies generated in the directory to Lib/site-packages
Note:
Sometimes we just want to test, do not want to install, then we can put build/lib.??? Helloworld.pyd copy of the directory to the current directory
or execute the script before Importhelloworld: Import sys;sys.path.append (Pathof helloworld.pyd)
③ Test:
>>>import HelloWorld
>>>helloworld. SayHello ()
Hello,world
Python and c| C + + (ii): Using Cython for mixed