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
Ïà¹ØÎĵµ£º
1.cµ÷ÓÃpython:
ʵÀý´úÂ룺
main.cµ÷ÓÃtest.pyµÄ
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//main.c
#include <windows.h>
......
Windows C ³ÌÐòÉè¼ÆÈëÃÅÓëÌá¸ß
http://download.chinaitlab.com/program/files/13246.html
µ¥Æ¬»úCÓïÑÔÈëÃÅ
http://download.chinaitlab.com/program/files/12907.html
C++ ÈëÃÅ»ù´¡½Ì³Ì
http://download.chinaitlab.com/program/files/7617.html
CÓïÑÔ³£ÓÃËã·¨Ô´´úÂë
http://download.chinaitlab.com/program/files ......
Ò»¸ö¿ØÖÆ̨ϵÄÊý×Ö±í´ïʽÇóÖµ³ÌÐò (c/c++)
Ô´´úÂë¼ûÏ£º
#include <stdio.h>
#include <string>
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <stack>
using namespace std;
//ÉèÖÃÔËËã·ûÓÅÏȼ¶µÄËã·¨
int Priority(const string opera) // ÔËËã·û ......
Ò»Sun Studio ¼ò½é
Sun Studio ÊÇ Solaris/OpenSolaris ²Ù×÷ϵͳÉϵÄÊ×Ñ¡¿ª·¢»·¾³¡£Ëü°üº¬²¢ÓÅ»¯ÁË C ¡¢ C++ ¼° Fortran ±àÒëÆ÷£¬¶øÇÒÈÚÈëÁËÒµ½çÁìÏ鵀 IDE ºÍÐÔÄܵ÷ÊÔ¼¼Êõ¡£Ëæ×ÅÈíÓ²¼þ¼¼Êõ·¢Õ¹¹ØϵµÄµ÷Õû£¬¿ª·¢¶à´¦ÀíÆ÷ºÍ¶àÏß³ÌÓ¦ÓõÄÖØÒªÐÔÕýÈÕÒæÍ»³ö£¬¶ø Sun StudioÔÚ¿ª·¢¶àÏ̳߳ÌÐò£¨Ó ......