Login

MOVONET

Home Music Blog Group Champion Forum Club

Login | Sign Up| Help

Category:business circle/entertainment circle   Owner python    Members' Amount:11People   Group Status: Public    Create Date:2006-12-31  
     PYTHON and C alternately transfer the example to analyze before mutually the

use the tool:

Vc++ compiler

Python interpreter

like has not installed VC, may go to Microsoft website next C++ the compiler, the address is as follows: After

http://download.microsoft.com/download/3/9/b/39bac755-0a1e-4d0b-b72c-3a158b7444c4/VCToolkitSetup.exe

installs, joins separately in the environment variable PYTHON INCLUDE and LIBS in following 2 great

INCLUDE

LIB

1, C transfers PYTHON

#include <Python.h>

int main (int argc, char *argv[]

{

Py_Initialize(;

PyRun_SimpleString (“from time import time, ctime n”

“print 'Today is', ctime(time( n”;

Py_Finalize(;

return 0;

}

with the CL filename translation is directly



2, produces DLL with C, transfers the

C code with PYTHON: If FOO.C

#include <Python.h>

/* Define the method table. */

static PyObject *foo_bar (PyObject *self, PyObject *args;

static PyMethodDef FooMethods[] = {

{“bar”,foo_bar,METH_VARARGS},

{NULL,NULL}

};

/* Here's the initialization function. We don't need to do anything

for our own needs, but Python needs that method table. */

void initfoo(

{

(void Py_InitModule (“foo”, FooMethods;

}

/* Finally, let's do something… involved… as an example function. */

static PyObject *foo_bar (PyObject *self, PyObject *args

{

char *string;

int len;

if (! PyArg_ParseTuple (args, “s”, &string

return NULL;

len = strlen(string;

return Py_BuildValue (“i”, len;

}
C definition document: foo.def
EXPORTS
initfoo the
translation produces foo.dll
Cl - c foo.c;
link foo.obj /dll /def: foo.def /OUT: foo.dll
transfers in PYTHON:
Import foo
Dir(foo


namely may see the result:
>>> import foo
>>> dir(foo
['__doc__', '__file__', '__name__', 'bar']
>>> ^Z

MOKI-COUNT :0 | Views479 | Comments2
  Comments
  • IP :
  • \ '\' \ '\' \   
  • 2009-11-11 09:20:32
  • IP :
  • Rubbish SHIT   
  • 2009-03-31 17:10:41

Verification: 7+0=