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. -
Ïà¹ØÎĵµ£º
CºÍC++ÓïÑÔѧϰ×ܽá(Ò»)
֪ʶ½á¹¹:
1¡¢if,for,switch,goto
2¡¢#define,const
3¡¢Îļþ¿½±´µÄ´úÂë,¶¯Ì¬Éú³ÉÄÚ´æ,¸´ºÏ±í´ïʽ,strcpy,memcpy,sizeof
4¡¢º¯Êý²ÎÊý´«µÝ,ÄÚ´æ·ÖÅ䷽ʽ,ÄÚ´æ´íÎó±íÏÖ,mallocÓënewÇø±ð
5¡¢ÀàÖØÔØ¡¢Òþ²ØÓ븲¸ÇÇø±ð,externÎÊÌâ,º¯Êý²ÎÊýµÄȱʡ
ÖµÎÊÌâ,ºê´úÂëÓëÄÚÁªº¯ÊýÇø±ð
6¡¢¹¹ÔìºÍÎö¹ ......
ÕâÁ½ÌìѧϰC++ѧÀÛÁË£¬¿´Á˺ܶàµÄÍøÕ¾ÂÛ̳£¬Í»È»¸Ð¾õÃÔãÁË£¬c/c++µ½µ×ÄÜ×öʲôÄØ£¿ÏÖÔÚJAVAºÜÈÈ£¬Ò²ºÜºÃÕÒ¹¤×÷£¬¶øÇÒѧÆðÀ´»¹Ìý˵ºÜÈÝÒ×ÈëÃÅ¡£²»ÓÃѧ¼ÆËã»ú»ù´¡ÀàµÄ֪ʶ£¬¿ÉC/C++¾Í²»Í¬ÁË£¬Ö»Ñ§±à³Ì»¹²»ÐУ¬»¹µÃѧʲôÊý¾Ý½á¹¹£¬Ëã·¨£¬¼ÆËã»úÔÀí£¬²Ù×÷ϵͳ£¬»ã±àÓïÑÔ£¬±à³ÌÓþߵȵȣ¬ÐèÒªºÃ¶à£¬¸Ð¾õÒ» ......
<!--
/* Font Definitions */
@font-face
{font-family:ËÎÌå;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:SimSun;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:"\@ËÎÌå" ......
/*
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 ......
CµÄ±ä²ÎÎÊÌâÓëprintº¯ÊýµÄʵÏÖ
ÎÒÃÇÔÚCÓïÑÔ±à³ÌÖлáÓöµ½Ò»Ð©²ÎÊý¸öÊý¿É±äµÄº¯Êý,ÀýÈçprintf() Õâ¸öº¯Êý,ËüµÄ¶¨ÒåÊÇÕâÑùµÄ: int printf( const char* format, ...);
Ëü³ýÁËÓÐÒ»¸ö²ÎÊýformat¹Ì¶¨ÒÔÍâ,ºóÃæ¸úµÄ²ÎÊýµÄ¸öÊýºÍÀàÐÍÊǿɱäµÄ,ÀýÈçÎÒÃÇ¿ÉÒÔÓÐÒÔϲ»Í¬µÄµ÷Ó÷½·¨:
printf("%d",i);
&nb ......