Dev-CPP/mingw32Environment Introduction(14)
Review above:
In the previous article, we learned how to use the Assembly Language in GCC. Later, due to the personal living environment, new content was not released. Of course, I don't want to talk about unpleasant things here. However, I didn't expect that there would still be so unmoral people in China. Because a person from Wenzhou in my dormitory invited n students to watch movies all night, which affected my sleep and resulted in serious sleep insufficiency. What's worse is the violation of human rights against me! Therefore, there is no time to calm down and write things. Today, I just calmed down some things and thought of you, my friend. So I will present my latest article immediately.
Chapter 2GccWritePythonExtension
In this chapter, I will introduce you to some basic knowledge about Python programming extensions.
If you understand python. You can skip this section and read the following content. If not, please take a look at Python with patience. Python is an explanatory language with a long history. It is cross-platform, object-oriented, written in C, and scalable. He was designed for Object-Oriented systems from the very beginning of design. Therefore, python is a good language. For more information about Python, see another article I wrote about Python, "a little bit about Python".
Python can be expanded using the portable Language C. Besides, it is written in C. Therefore, the expansion speed can be compared with that of static languages. Therefore, Python extensions can be written to greatly improve the project running efficiency. Of course there are other benefits.
However, this article does not introduce the Python language, nor specifically introduces Python extension using C. So, as I once said, my articles in this series just open a door for you.
Next I will start my topic.
Before you start everything, download the latest interpreter on your http://www.python.org and install it. When I write this article, the latest version is Python 2.4.2. I will not go into details about Python installation step by step.
Start writing programs!
First, let me give you a simple set of Python code.
The first file is the Add. py file, which will be imported.
# Add. py Def add (x, y ): Return (x + y) |
The second file is the main. py file, which will be executed.
# Main. py Import add Print Add. Add (256,512) |
Write the above two codes to the same directory using the specified file name (case-insensitive, though case-insensitive. Then, input main. py in the directory of the DOS terminal, and you will see that its output is 768.
Next I will briefly introduce the meaning of the two codes.
In the first file, an add function is defined and two parameters are input, named X and Y respectively. The function returns the value of X + Y.
In the second file, we first imported the Add. py file and put its namespace in the add space. Then, we call the Add function in add and input 256 and 512. In addition, the return value of the add function is displayed on the terminal.
Maybe you will ask, isn't this about C? How to introduce Python again. In fact, this is to lay the foundation for the future.
Next, let's try to use C to implement the Add. py function in Python.
// Add. c # Include <python. h> Static pyobject * Add (pyobject * Self, pyobject * ARGs ); Static pyobject * Add (pyobject * Self, pyobject * ARGs) { Int x = 0; Int y = 0; Int z = 0; If (! Pyarg_parsetuple (ARGs, "I | I", & X, & Y )) { Return NULL; } Z = x + y; Return py_buildvalue ("I", Z ); } Static pymethoddef addmethods [] = { {"Add", add, meth_varargs, "execute a shell command ."}, {Null, null, 0, null} }; Pymodinit_func initadd () { Py_initmodule ("add", addmethods ); } |
You may feel confused about this code. What is this? I will introduce it to you one by one.
The # include <python. h> must be imported. This is included in the include directory of the python installation directory. Secondly, we define a function named add. Because everything in python is an object. Therefore, in C, for python communication, C is passed to Python. All data types transmitted by python to C are pyobject. The following statement pyarg_parsetuple converts the input data to the C type. The py_buildvalue function converts the C data type to the python data type and returns it.
Next, we want to tell Python that the python functions in C have those. Therefore, the addmethods data group is used to store the data. This is a list. The last initadd function is used for initialization in the python import module. Must exist. In addition, for example, in the form of init Module name. The py_initmodule function tells python to add the content defined in the addmethods list to the namespace "add.
Finally, I wrote the code again. But this time, I will mark the code part of the routine with the red part, that is, the necessary part.
# Include <python. h> Static pyobject * Add (pyobject * Self, pyobject * ARGs ); Static pyobject * Add (pyobject * Self, pyobject * ARGs) { Int x = 0; Int y = 0; Int z = 0; If (! Pyarg_parsetuple (ARGs, "I | I", & X, & Y )) { Return NULL; } Z = x + y; Return py_buildvalue ("I", Z ); } Static pymethoddef addmethods [] = { {"Add", add, meth_varargs, "execute a shell command ."}, {Null, null, 0, null} }; Pymodinit_func initadd () { Py_initmodule ("add", addmethods ); } |
Finally, let's compile this code. You may ask whether different compilation methods are required for different operating systems since they are cross-platform. The answer is no. The Python compilation script can complete all the compilation processes for you. However, you need to set your mingw to the environment variable at the beginning. For details, refer to Chapter 1 to Chapter 3 of this series.
The following is a python compilation and installation script file.
#! /Usr/bin/Python # Setup. py From distutils. Core import setup, extension Module1 = extension (name = 'add, sources = ['add. c']) Setup (name = 'packagename ', version = '1. 0', description = 'this is a demo package', ext_modules = [module1]) |
This code tells Python how to make up your package. First, the first sentence #! /Usr/bin/Python tells the system what interpreter is used in BSD/Linux/Unix to run the code in Win32.
Module1 = extension ('mytest', sources = ['add. c'] defines your first extension to be compiled. The setup function is used to define the package. In ext_modules, the module information we just defined is added to the list. In this way, python will give you more information, and the operating system will automatically compile appropriate extension files on different platforms.
Enter setup. if there is no error in Py build-cmingw32, after compilation, you will find add in the subdirectory In the build directory. PYD file. This is the python C extension file. Add. PY removed from the directory and deleted add. pyC file (this file will be automatically generated during Python import, so that the next import can be executed and imported quickly ). Then put the compiled Add. PYD in this directory. What do you see during the same operation? Same as the running result just now. Yes. The C extension here implements the functions defined in Python just now. Do you think there are still many questions you don't understand? If you want to go deep into Python extension programming, you can refer to Python reference manual Python/c api. The meanings of some parameters in extension are listed below.
Include_dirs defines the directory address of the header file. By default, the directory address of the python header file has been defined.
Library_dirs defines the directory address of the library file. By default, the python library file directory is also included.
Libraries defines the library file name to be used during compilation. This is a bit like the parameter added after-L in GCC.
Of course, there are many other parameters. Here we only list several common basic parameter names and meanings. I hope the door I opened for you is spacious. If you are interested, you can view the python reference manual. I believe it will be of great help to you.
Good luck. In addition, please try to raise any questions.
VOICEOVER:
As I said at the beginning, I have not written any new articles due to my living environment. I owe you a lot of money. However, this is also the reason why no articles have been published in the last month. I also said in the previous stage that there is no idea. Later, I found the direction of intermediate articles by accident, so I had a new idea. I hope this article will satisfy you. Thank you for your long-term support. With you, I will have unremitting motivation to write it down. Thank you. If you have any questions, please ask your questions and suggestions on the http://blog.csdn.net/visioncat.
Studio software development group (SDT)
Studio development team
Dipper (Huang yaokui)