PythonµÄC/C++À©Õ¹
Python
µÄ
C/C++
À©Õ¹
By phidoit@gmail.com
¿ÉÀ©Õ¹ÐÔÊÇ
Python
µÄÒ»´óÌØÉ«£¬Ò»·½Ã棬ÓÉÓÚ
Python
ÊǽâÊÍÖ´Ðеģ¬Õâµ¼ÖÂÔËÐÐËٶȻá±È±àÒëÐÍÓïÑÔÂý£¬Òò´Ë¿ÉÒÔͨ¹ýʹÓÃ
C/C++
ÖØдºËÐIJ¿·Ö´úÂëÒÔ½â¾öÐÔÄÜÉϵÄÆ¿¾±
(
³ÌÐò
90%
µÄʱ¼äÔÙÔËÐÐ
10%
µÄ´úÂë
)
£»ÁíÒ»·½Ã棬¿ÉÒÔͨ¹ýÀ©Õ¹£¬´ïµ½Ìí¼ÓÕûºÏһЩ¶îÍâµÄ¹¦ÄÜÒÔ¼°±£³ÖרÓÐÔ´´úÂëµÄÄ¿µÄ¡£ÔÚ±¾ÎĽÓÏÂÀ´µÄ²¿·ÖÖÐÎÒÃǽ«ÌÖÂÛÈçºÎ±àд
C/C++
À©Õ¹´úÂ룬²¢Ê¹ÓÃËüÃǵŦÄÜ¡£
ÎÒÃÇÒª½¨Á¢µÄÊÇÒ»¸ö¿ÉÒÔÔÚ
Python
ÄÚÔËÐеÄ
C/C++
Ä£¿é£¬Òò´ËÐèÒª½â¾öÈçºÎʹ
C
´úÂëºÍ
Python
´úÂëÄܹ»½øÐн»»¥ÒÔ¼°Êý¾Ý¹²Ïí¡£À©Õ¹ÊÇͨ¹ýΪ
C
´úÂë±àд°ü×°º¯Êý
(
ÀàËÆÊÊÅäÆ÷
)
ʵÏÖË«Ïò½»»¥ºÍÊý¾Ý¹²ÏíµÄ¡£
Ò»£®Ò»°ãµÄ°üװģʽ
ÿһ¸ö°ü×°º¯ÊýÖ÷Òª×öÈý¼þÊ£º
1.
°ÑÊäÈëµÄ
Python
¶ÔÏóת»»Îª
C/C++
¶ÔÏó£»
2.
µ÷ÓÃ
C/C++
º¯Êý£»
3.
ת»»
C/C++
º¯Êý´¦ÀíµÄÊä³ö½á¹ûΪ
Python
¶ÔÏ󣬲¢·µ»Ø£»
ÏÈÓÃÒ»¸ö¼òµ¥ÑùÀýÃèÊöÄ£¿é´óÌåµÄ¹ý³Ì£º
//wrap.cpp
//1.C´úÂë
#include "Python.h"
int add(int arg1, int arg2)
{
return arg1 + arg2;
}
//2.addµÄ°ü×°º¯Êý£º
static PyObject* wrap_add(PyObject *self, PyObject *args)
{
//°ÑÊäÈëµÄPython¶ÔÏóת»»ÎªC/C++ÄÜʶ±ðµÄÊý¾Ý
int arg1, arg2;
if(!PyArg_ParseTuple(args, "ii", &arg1, &arg2))
return NULL;
//µ÷ÓÃC/C++º¯Êý£¬µÃµ½½á¹û
int result = add(arg1,arg2);
//°ÑµÃµ½µÄ½á¹û°ü×°³ÉPython¶ÔÏ󣬲¢·µ»Ø
return (PyObject*)Py_BuildValue("i", result);
}
//3.Ϊģ¿éÌí¼ÓPyMethodDef·½·¨Êý×é
static PyMethodDef wrap_methods[] ={
{"add", wrap_add, METH_VARARGS},
{NULL, NULL}
};
//4.Ôö¼ÓÄ£¿é³õʼ»¯º¯ÊýInitModule
PyMODINIT_FUNC initwrap (void)
{
Py_InitModule("wrap ", wrap_methods);
}
°ÑÉÏÃæµÄ´úÂë±àÒ룬Éú³É
wrap.pyd
¡£
Visual Studio 2005
±àÒë·½·¨²Î¿¼£º
http://blog.csdn.net/solo_lxy/archive/2007/07/20/1700515.aspx
&
Ïà¹ØÎĵµ£º
C++ Boost¿â¼ò½é
boostÖ÷Òª°üº¬Ò»Ï¼¸¸ö´óÀࣺ×Ö·û´®¼°Îı¾´¦Àí¡¢ÈÝÆ÷¡¢µü´ú×Ó(Iterator)¡¢Ëã·¨¡¢º¯Êý¶ÔÏóºÍ¸ß½×±à³Ì¡¢·ºÐͱà³Ì¡¢Ä£°åÔª±à³Ì¡¢Ô¤´¦ÀíÔª±à³Ì¡¢²¢·¢±à³Ì¡¢ÊýѧÏà¹Ø¡¢¾À´íºÍ²âÊÔ¡¢Êý¾Ý½á¹¹¡¢ÊäÈë/Êä³ö¡¢¿çÓïÑÔÖ§³Ö¡¢ÄÚ´æÏà¹Ø¡¢Óï·¨·ÖÎö¡¢ÔÓÏî¡£ ÓÐһЩ¿âÊÇ¿çÀà±ð°üº¬µÄ£¬¾ÍÊǼÈÊôÓÚÕâ ......
½üÀ´ÓÖÓÐÈËÔÚȺÀïÎÊÈçºÎÓÃcÓïÑÔ±àÖÆdllÎļþ£¨¶¯Ì¬Á´½Ó¿â£©¡£
ÔÀ´Ã»ÓжÔÕâ¸öÎÊÌâÌ«ÔÚÒâ¹ý£¬Ò²Ã»Óг¢ÊÔ¹ýÈκνâ¾ö·½°¸£¬±Ï¾¹ÔÀ´ÎÒÊÇÓÃvbµÄ£¨ÏÖÔÚÓÃ.net£©£¬×ö¸ödllÖ»²»¹ýÊǵãѡһϽ¨Á¢activeX dll¹¤³ÌµÄͼ±ê¶øÒÑ¡£½ñÌìÔÚÍøÉÏÓëÅóÓÑÁÄÌ죬¿´ÁËËûÖ¸¸øÎҵļ¸¸ö¼¸¸öÎļþ£¬ÓÃMingW½«C³ÌÐò±àÒë³ÉdllÎļþµÄÀý×Ó£¬ÎÒ»ÐÈ»´óÎò£¬ÔÀ ......
תÔØ×Ô
http://blog.163.com/skynjl/blog/static/603053922009127101558356/
½ñÌìͬʽÐÎÒ±àдһ¸öshell½Å±¾¡£»°Ëµ£¬ËäÈ»ÎÒÊÜ*nixµÄÓ°ÏìÉõÉµ«ÊǶÔÓÚ*nix ÀïËæ´¦¿É¼ûµÄsh½Å±¾È´ÊÇÌÖÑáÖ®¼«¡£ÎªÊ²Ã´ÌÖÑáÄØ£¿Ê×ÏÈÊÇÒòΪsh½Å±¾ÄÇĪÃûÆäÃîµÄÓï·¨£¬¸Ð¾õ¾ÍÏñËæдËæÈӵijÌÐò£¬ÍêȫûÓÐÈκÎÃÀ¸Ð¿ÉÑÔ¡£Æä´ÎÊÇsh½Å ......
Ê×ÏÈÏÂÔØ×îа汾µÄPython£¬ÔÚÕâÀïÎҵİ汾ÊÇPython3.1.1¡£È»ºó¾ÍÊÇ°²×°£¬Çл»µ½PythonµÄÎļþĿ¼Ï£º
./configure
make
make test
sudo make install
ÔÚÕâÀï¿ÉÄÜ»áÓöµ½¼¸¸öÎÊÌ⣬µÚÒ»¸öÊÇÔÚ./configureʱ£¬¿ÉÄÜ»á³öÏÖC compiler cannot create ......