pythonµÄC¡¢c++À©Õ¹
pythonµÄC¡¢c++À©Õ¹
http://blog.chinaunix.net/u3/110228/showart_2148725.html
pythonµÄÇ¿´ó²»½ö±íÏÖÔÚÆ书ÄÜÉÏ£¬¶øÇÒ»¹±íÏÖÔÚÆäÀ©Õ¹ÄÜÁ¦ÉÏ¡£
ʹÓÃC/C++ºÜÈÝÒ×±àдpythonµÄÄ£¿é£¬À©Õ¹pythonµÄ¹¦ÄÜ¡£
ͬʱ½«ÐÔÄÜÒªÇó±È½Ï¸ßµÄ´úÂëʹÓÃC/C++±àд£¬ÄܸüºÃµÄÃÖ²¹
½Å±¾ÓïÑÔÖ´ÐÐËÙ¶ÈÂýµÄȱÏÝ¡£
1. pythonµÄCÓïÑÔÀ©Õ¹
1.1 TestCLib.c: ÌṩpythonµÄÄ£¿é½Ó¿Ú
#include "Python.h"
#include <stdlib.h>
#include <stdio.h>
long fac(long);
// ------------------------------------------------------
// Make C code usable in Python
// ------------------------------------------------------
PyObject* TestCLib_fac(PyObject * self, PyObject *args)
{
long num;
if ( ! PyArg_ParseTuple(args,"l",&num)){
return NULL;
}
return (PyObject*)Py_BuildValue("l",fac(num));
}
static PyMethodDef TestCLibMethods[] = {
{"fac",TestCLib_fac,METH_VARARGS},
{NULL,NULL}
};
void initTestCLib()
{
Py_InitModule("TestCLib",TestCLibMethods);
}
1.2 test.c: ¾ßÌåµÄCÓïÑÔʵÏÖ
#include <stdlib.h>
#include <stdio.h>
// ------------------------------------------------------
// Pure C code
// ------------------------------------------------------
long fac(long n)
{
if ( n < 0){
return fac(-n);
} else if ( n < 2 ){
return 1;
} else {
return n * fac(n-1);
}
}
1.3 test.py: ²âÊԽű¾
#!/usr/bin/env python
import TestCLib as TestLib
for i in range(10,20) :
f1 = TestLib.fac(i)
print "%d! = %d"%(i,f1)
1.4 ±àÒëÓëÔËÐÐ
±àÒëÃüÁ
gcc -fPIC -shared -o TestCLib.so TestCLib.c test.c -I /usr/local/python/include/python2.6/
½«Éú³ÉµÄ¶¯Ì¬Á´½Ó¿â T
Ïà¹ØÎĵµ£º
×ÜÊDZ»Í¬Ñ§ÃÇÎʵ½£¬ÈçºÎѧϰCºÍC++²Å²»Ã£È»£¬²Å²»ÊÇÂÒѧ£¬ÏëÁËһϣ¬ÕâÀï¸ø³öÒ»¸ö×ܵĻظ´¡£
¡¡¡¡Ò»¼ÒÖ®ÑÔ£¬»¶ÓÅÄש¹þ¡£
¡¡¡¡1¡¢¿ÉÒÔ¿¼ÂÇÏÈѧϰC.
¡¡¡¡´ó¶àÊýʱºò£¬ÎÒÃÇѧϰÓïÑÔµÄÄ¿µÄ£¬²»ÊÇΪÁ˳ÉΪһ¸öÓïÑÔר¼Ò£¬¶øÊÇÏ£Íû³ÉΪһ¸ö½â¾öÎÊÌâµÄר¼Ò¡£×öÒ»¸öÓÐÓõijÌÐòÔ±£¬×öÒ»¸ö׬ǮµÄ³ÌÐòÔ±¡£ÎÒÃǵļ ......
1 ±à³Ì»ù´¡
1.1 »ù±¾¸ÅÄî
1. µÄÀí½â£ºconst char*, char const*, char*constµÄÇø±ðÎÊÌ⼸ºõÊÇC++ÃæÊÔÖÐÿ´Î ¶¼»áÓеÄÌâÄ¿¡£ ÊÂʵÉÏÕâ¸ö¸ÅÄî˶¼ÓÐÖ»ÊÇÈýÖÖÉùÃ÷·½Ê½·Ç³£ÏàËƺÜÈÝÒ׼ǻ졣 Bja ......
£¨×¢£¬±¾ÎÄÊÇ·ÒëµÄhttp://www.cprogramming.com/
ÉϵÄÎÄÕ Where C and C++ Differ
£©
C++»ùÓÚC£¬Ò²±£ÁôÁËCµÄ´ó²¿·ÖÌØÐÔ¡£µ«ÊÇÔÚÔ´Â뼶ÉÏÓÐЩµØ·½ÊÇÓëC²»¼æÈݵġ£
C³ÌÐòԱʹÓÃC++ʱµÄÏÝÚå
´Ó void* µÄÒþʽ·ÖÅä
²»ÄÜ´Ó void* ÒþʽµØ·ÖÅäµ½ÆäËûÈκÎÀàÐÍ¡£ÀýÈ磬ÏÂÃæµÄ´úÂëÔÚCÖÐÊǷdz£ÓÐЧµÄ¡£
in ......
Ò»Sun Studio ¼ò½é
Sun Studio ÊÇ Solaris/OpenSolaris ²Ù×÷ϵͳÉϵÄÊ×Ñ¡¿ª·¢»·¾³¡£Ëü°üº¬²¢ÓÅ»¯ÁË C ¡¢ C++ ¼° Fortran ±àÒëÆ÷£¬¶øÇÒÈÚÈëÁËÒµ½çÁìÏ鵀 IDE ºÍÐÔÄܵ÷ÊÔ¼¼Êõ¡£Ëæ×ÅÈíÓ²¼þ¼¼Êõ·¢Õ¹¹ØϵµÄµ÷Õû£¬¿ª·¢¶à´¦ÀíÆ÷ºÍ¶àÏß³ÌÓ¦ÓõÄÖØÒªÐÔÕýÈÕÒæÍ»³ö£¬¶ø Sun StudioÔÚ¿ª·¢¶àÏ̳߳ÌÐò£¨Ó ......
B/S½á¹¹£¬¼´Browser/Server£¨ä¯ÀÀÆ÷/·þÎñÆ÷£©½á¹¹£¬ÊÇËæ×ÅInternet¼¼ÊõµÄÐËÆ𣬶ÔC/S½á¹¹µÄÒ»Öֱ仯»òÕ߸ĽøµÄ½á¹¹¡£ÔÚÕâÖÖ
½á¹¹Ï£¬Óû§½çÃæÍêȫͨ¹ýWWWä¯ÀÀÆ÷ʵÏÖ£¬Ò»²¿·ÖÊÂÎñÂß¼ÔÚÇ°¶ËʵÏÖ£¬µ«ÊÇÖ÷ÒªÊÂÎñÂß¼ÔÚ·þÎñÆ÷¶ËʵÏÖ£¬ÐγÉËùν3-tier½á¹¹¡£B/S½á¹¹£¬Ö÷ÒªÊÇ
ÀûÓÃÁ˲»¶Ï³ÉÊìµÄWWWä¯ÀÀÆ÷¼¼Êõ£¬½áºÏä¯ÀÀÆ÷µÄ¶àÖ ......