Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

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(&ltime);
     return ctime(&ltime);
 }
 ½Ó¿ÚÎļþ
ÏÖÔÚ£¬ÎªÁËÔö¼ÓÕâЩÎļþµ½Äãϲ»¶µÄÓïÑÔÖУ¬ÄãÐèҪдһ¸ö½Ó¿ÚÎļþ£¨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. -


Ïà¹ØÎĵµ£º

¹ØÓÚpython unicodeµÄʵÑé

ʵÑé»·¾³£ºwindows xp + vim
Îļþ£ºtest.py¡£±àÂ룺ansi
ÎÒÃǵÄÄ¿±ê²Ù×÷test.pyÖб£´æµÄ·ÇÓ¢ÎÄ×Öĸ¡£
ÎļþÍ·µÄ#encoding=utf8/gbk£¬Õâ¸öÊÇÓÃÀ´ËµÃ÷Ô´ÎļþµÄÓ²Å̱àÂëÒÔ±ãpythonʶ±ð[4]¡£
----------------------------------------------
ÊäÈë:
x = 'ÖÐÎÄ'
Êä³ö: ±àÒëʧ°Ü
±àÒëʱÐèÒªÖªµÀ‘ÖÐÎÄ’µÄÓ²Å̱à ......

python×Ö·û´®Ê¹ÓÃ×ܽá



À´Ô´:
×÷Õß:
Áé½£
1.python ×Ö·û´®Í¨³£Óе¥ÒýºÅ£¨'...'£©¡¢Ë«ÒýºÅ£¨...£©¡¢ÈýÒýºÅ£¨...£©»ò£¨'''...'''£©°üΧ£¬ÈýÒýºÅ°üº¬µÄ×Ö·û´®¿ÉÓɶàÐÐ×é³É£¬Ò»°ã¿É±íʾ´ó¶ÎµÄÐðÊöÐÔ×Ö·û´®¡£ÔÚʹÓÃʱ»ù±¾Ã»Óвî±ð£¬

1.python
×Ö·û´®Í¨³£Óе¥ÒýºÅ£¨'...'£©¡¢Ë«ÒýºÅ£¨"..."£©¡¢ÈýÒýºÅ£¨"""... ......

c/c++µ½µ×ÄÜ×öʲô£¿

    ÕâÁ½ÌìѧϰC++ѧÀÛÁË£¬¿´Á˺ܶàµÄÍøÕ¾ÂÛ̳£¬Í»È»¸Ð¾õÃÔãÁË£¬c/c++µ½µ×ÄÜ×öʲôÄØ£¿ÏÖÔÚJAVAºÜÈÈ£¬Ò²ºÜºÃÕÒ¹¤×÷£¬¶øÇÒѧÆðÀ´»¹Ìý˵ºÜÈÝÒ×ÈëÃÅ¡£²»ÓÃѧ¼ÆËã»ú»ù´¡ÀàµÄ֪ʶ£¬¿ÉC/C++¾Í²»Í¬ÁË£¬Ö»Ñ§±à³Ì»¹²»ÐУ¬»¹µÃѧʲôÊý¾Ý½á¹¹£¬Ëã·¨£¬¼ÆËã»úÔ­Àí£¬²Ù×÷ϵͳ£¬»ã±àÓïÑÔ£¬±à³ÌÓþߵȵȣ¬ÐèÒªºÃ¶à£¬¸Ð¾õÒ» ......

extern c×÷ÓÃ

Á´½Óָʾ·ûextern C
Èç¹û³ÌÐòԱϣÍûµ÷ÓÃÆäËû³ÌÐòÉè¼ÆÓïÑÔÓÈÆäÊÇC дµÄº¯ÊýÄÇôµ÷Óú¯Êýʱ±ØÐë
¸æËß±àÒëÆ÷ʹÓò»Í¬µÄÒªÇóÀýÈçµ±ÕâÑùµÄº¯Êý±»µ÷ÓÃʱº¯ÊýÃû»ò²ÎÊýÅÅÁеÄ˳Ðò¿ÉÄÜ
²»Í¬ÎÞÂÛÊÇC++º¯Êýµ÷ÓÃËü»¹ÊÇÓÃÆäËûÓïÑÔдµÄº¯Êýµ÷ÓÃËü
³ÌÐòÔ±ÓÃÁ´½Óָʾ·ûlinkage directive ¸æËß±àÒëÆ÷¸Ãº¯ÊýÊÇÓÃÆäËûµÄ³ÌÐòÉè¼ÆÓïÑÔ
±àдµ ......

C±ä²Îº¯ÊýµÄʵÏÖ»úÖÆ

CµÄ±ä²ÎÎÊÌâÓëprintº¯ÊýµÄʵÏÖ
ÎÒÃÇÔÚCÓïÑÔ±à³ÌÖлáÓöµ½Ò»Ð©²ÎÊý¸öÊý¿É±äµÄº¯Êý,ÀýÈçprintf() Õâ¸öº¯Êý,ËüµÄ¶¨ÒåÊÇÕâÑùµÄ: int printf( const char* format, ...); 
Ëü³ýÁËÓÐÒ»¸ö²ÎÊýformat¹Ì¶¨ÒÔÍâ,ºóÃæ¸úµÄ²ÎÊýµÄ¸öÊýºÍÀàÐÍÊǿɱäµÄ,ÀýÈçÎÒÃÇ¿ÉÒÔÓÐÒÔϲ»Í¬µÄµ÷Ó÷½·¨:
   printf("%d",i);
&nb ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ