Python ʹÓÃC´úÂë——swig
Ref : http://www.swig.org/translations/chinese/tutorial.html
¼ÙÉèÄãÓÐһЩcÄãÏëÔÙ¼ÓPython.¡£¾ÙÀýÀ´ËµÓÐÕâôһ¸öÎļþexample.c
/* File : example.c */
#include <time.h>
double My_variable = 3.0;
int fact(int n) {
if (n <= 1) return 1;
else return n*fact(n-1);
}
int my_mod(int x, int y) {
return (x%y);
}
char *get_time()
{
time_t ltime;
time(<ime);
return ctime(<ime);
}
½Ó¿ÚÎļþ
ÏÖÔÚ£¬ÎªÁËÔö¼ÓÕâЩÎļþµ½Äãϲ»¶µÄÓïÑÔÖУ¬ÄãÐèҪдһ¸ö½Ó¿ÚÎļþ£¨interface file£©Í¶Èëµ½swigÖС£ÕâЩC functionsµÄ½Ó¿ÚÎļþ¿ÉÄÜÈçÏÂËùʾ£º
/* example.i */
%module example
%{
/* Put header files here or function declarations like below */
extern double My_variable;
extern int fact(int n);
extern int my_mod(int x, int y);
extern char *get_time();
%}
extern double My_variable;
extern int fact(int n);
extern int my_mod(int x, int y);
extern char *get_time();
½¨Á¢PythonÄ£¿é
ת»»±àÂëC³ÉPythonÄ£¿éºÜ¼òµ¥£¬Ö»ÐèÒª°´ÈçÏÂ×ö¼´¿É£¨Çë¼ûÆäËû²Ù×÷ϵͳµÄSWIG ¹²Ïí¿â°ïÖúÊֲᣩ:
unix % swig -python example.i
unix % gcc -c -fPIC
-m32
example.c example_wrap.c \
-I/usr/(local/include/python2.1)include/python2.4
unix % (ld)gcc
-shared -m32
example.o example_wrap.o -o _example.so
¹ØÓÚÒÔÉϺìÀ¶ÂÌÑÕÉ«×Ö½âÊÍ£º
1. -
Ïà¹ØÎĵµ£º
ʵÑé»·¾³£ºwindows xp + vim
Îļþ£ºtest.py¡£±àÂ룺ansi
ÎÒÃǵÄÄ¿±ê²Ù×÷test.pyÖб£´æµÄ·ÇÓ¢ÎÄ×Öĸ¡£
ÎļþÍ·µÄ#encoding=utf8/gbk£¬Õâ¸öÊÇÓÃÀ´ËµÃ÷Ô´ÎļþµÄÓ²Å̱àÂëÒÔ±ãpythonʶ±ð[4]¡£
----------------------------------------------
ÊäÈë:
x = 'ÖÐÎÄ'
Êä³ö: ±àÒëʧ°Ü
±àÒëʱÐèÒªÖªµÀ‘ÖÐÎÄ’µÄÓ²Å̱à ......
/*
coder: ACboy
date: 2010-3-14
result: 1A
description: UVa 327 Evaluating Simple C Expressions
*/
#include <iostream>
#include <algorithm>
using namespace std;
struct Node {
char name;
int value;
int lastValue;
int pos;
};
int cmp(const Node & a, const Node &a ......
Á´½Óָʾ·ûextern C
Èç¹û³ÌÐòԱϣÍûµ÷ÓÃÆäËû³ÌÐòÉè¼ÆÓïÑÔÓÈÆäÊÇC дµÄº¯ÊýÄÇôµ÷Óú¯Êýʱ±ØÐë
¸æËß±àÒëÆ÷ʹÓò»Í¬µÄÒªÇóÀýÈçµ±ÕâÑùµÄº¯Êý±»µ÷ÓÃʱº¯ÊýÃû»ò²ÎÊýÅÅÁеÄ˳Ðò¿ÉÄÜ
²»Í¬ÎÞÂÛÊÇC++º¯Êýµ÷ÓÃËü»¹ÊÇÓÃÆäËûÓïÑÔдµÄº¯Êýµ÷ÓÃËü
³ÌÐòÔ±ÓÃÁ´½Óָʾ·ûlinkage directive ¸æËß±àÒëÆ÷¸Ãº¯ÊýÊÇÓÃÆäËûµÄ³ÌÐòÉè¼ÆÓïÑÔ
±àдµ ......
1.Ê×ÏÈÊÇ»ñµÃlinuxÄÚºËÔ´Â룬ºÃÏñÊÇ·Ï»°£¬ÏÂÔصØÖ·ÈçÏ£ºftp://ftp.kernel.org/pub/linux/kernel/v2.6/ÏÂÔØ£º
linux-2.6.16.22.tar.bz2 patch-2.6.22.6.bz2
ÉÏÃæÒ»²½ÐèҪ˵Ã÷µÄÊÇÒ»°ã¶øÑÔ£¬linuxÄں˵ĸ÷¸ö²¹¶¡ÎļþÊǸù¾Ýij¸ölinuxÄں˵İ汾ºÅÀ´×÷µÄpatch¡£
½«ÉÏÃæµÄÁ½¸öѹËõÎļþ½âѹ:
tar jxvf linux-2.6.22.ta ......
1¡¢C/C++³ÌÐòÔ±Çë×¢Ò⣬²»ÄÜÔÚcaseÓï¾ä²»Îª¿Õʱ“ÏòÏÂÖ´ÐД¡£
¡¡¡¡2¡¢ÖµÀàÐͺÍÒýÓÃÀàÐÍÖ®¼äµÄÇø±ð£ºC#µÄ»ù±¾ÀàÐÍ(int£¬charµÈ)¶¼ÊÇÖµÀàÐÍ£¬ÊÇÔÚÕ»Öд´½¨µÄ¡£¶ø¶ÔÏóÊÇÒýÓÃÀàÐÍ£¬´´½¨ÓÚ¶ÑÖУ¬ÐèҪʹÓùؼü×Önew¡£
¡¡¡¡3¡¢ÔÚC#ÖÐͨ¹ýʵÀý·ÃÎʾ²Ì¬·½·¨»ò³ÉÔ±±äÁ¿ÊDz»ºÏ·¨µÄ£¬»áÉú³É±àÒëÆ÷´íÎó¡£µ«ÊÇÎÒÃÇ¿ÉÒÔͨ¹ýÉù ......