swig-python

1. SWIG介绍(来自于wiki)

SWIG (Simplified Wrapper and Interface Generator) is anopen source software tool used to connectcomputer programs or libraries written in C or C++ with scripting languages such as LuaPerlPHPPythonR,RubyTcl, and other languages likeC#JavaGoModula-3,OCamlOctave, and Scheme. Output can also be in the form of XML or Lisp S-expressions.

2.SWIG的安装

2.1 下载地址: http://www.swig.org/download.html

2.2 将下载的文件如swig-2.0.8.tar.gz解压

2.3 进入swig-2.0.8目录,执行一下命令

[plain]  view plain copy
  1. ./configure --prefix=/usr/program/swig/ #或者其他安装目录  
  2. make  
  3. make install  
在./configure 时遇到Cannot find pcre-config script from PCRE (Perl Compatible Regular Expressions)的错误,使用

./configure --prefix=/usr/progam/swig --without-pcre, 因为我使用swig的目的是python

3.SWIG的使用

SWIG支持多种语言(脚本),这里介绍python的一个例子,这个例子是使用c语言模拟位数组,然后交由python扩展

最后有一个使用c扩展python和直接使用python的效率比较

1. 编写c文件

[cpp]  view plain copy
  1. //Bit.h  
  2. #ifndef BIT_H  
  3. #define BIT_H  
  4.   
  5. //create: 2012-10-19  
  6. //version: 1.0  
  7. #define CHAR_LENGTH sizeof(unsigned char)   
  8.   
  9. //Bit模拟位操作  
  10. typedef struct  
  11. {  
  12.     unsigned char *pArray;//指向一个字符型数组,用以存储bit位  
  13.     unsigned long length;//记录pArray的长度, 字符个数  
  14.     unsigned long used;//记录用户使用的bit位  
  15. }Bit;  
  16.   
  17. //创建Bit对象  
  18. //@len: 初始化bit位数  
  19. //@return: 返回一个指向Bit对象的指针, 该指针需要使用freeBit销毁  
  20. Bit* createBit(unsigned long len);  
  21.   
  22. //设置bit位  
  23. //@pb:指向Bit的一个对象, 如果为NULL, 返回1  
  24. //@index: 需要设置的bit位, 范围应当是 (0~used)  
  25. //@value: bit位的值, (0,1)  
  26. //@return: 成功返回0, 如果出现pb==NULL, index out of range, value!=0 and value!=1, 返回1  
  27. int setBit(Bit *pb,unsigned long index, int value);  
  28.   
  29. //获取bit位  
  30. //@pb:指向Bit的一个对象, 如果为NULL, 返回1  
  31. //@index: 需要设置的bit位, 范围应当是 (0~used)  
  32. //@return: 成功返回0, 如果出现pb==NULL, index out of range, 返回1  
  33. int getBit(Bit *pb,unsigned long index);  
  34.   
  35. //返回使用的长度  
  36. //@pb: 如果pb==NULL, return -1  
  37. int bitLength(Bit *pb);  
  38.   
  39. //销毁Bit对象  
  40. void freeBit(Bit *pb);  
  41. #endif  
[cpp]  view plain copy
  1. //Bit.c  
  2. #include "Bit.h"  
  3. #include <stdio.h>  
  4. #include <stdlib.h>  
  5. #include <limits.h>  
  6.   
  7. //创建Bit对象  
  8. //@len: 初始化bit位数, len>=0  
  9. //@return: 返回一个指向Bit对象的指针, 该指针需要使用freeBit销毁  
  10. Bit* createBit(unsigned long len)  
  11. {  
  12.     if(len<0)  
  13.         return NULL;  
  14.   
  15.     Bit *pb=(Bit*)malloc(sizeof(Bit));  
  16.     if(len>0)  
  17.     {  
  18.         pb->length=(len-1)/CHAR_LENGTH+1;  
  19.         pb->pArray=(char*)malloc(sizeof(char)*pb->length);  
  20.         pb->used=len;  
  21.     }else  
  22.     {  
  23.         pb->length=0;  
  24.         pb->pArray=NULL;  
  25.         pb->used=len;  
  26.     }  
  27.     return pb;  
  28. }  
  29.   
  30. //设置bit位  
  31. //@pb:指向Bit的一个对象, 如果为NULL, 返回1  
  32. //@index: 需要设置的bit位, 范围应当是 [0~used)  
  33. //@value: bit位的值, (0,1)  
  34. //@return: 成功返回0, 如果出现pb==NULL, index out of range, value!=0 and value!=1, 返回1  
  35. int setBit(Bit *pb,unsigned long index, int value)  
  36. {  
  37.     if(pb==NULL || pb->pArray==NULL || index<0 || index>=pb->used || (value!=0 && value!=1))  
  38.         return 1;  
  39.   
  40.     unsigned long a=index/CHAR_LENGTH;  
  41.     unsigned long b=index%CHAR_LENGTH;  
  42.     if(value==0)  
  43.     {  
  44.         pb->pArray[a]&=(UCHAR_MAX^(1<<b));  
  45.         //printf("%d\n",pb->pArray[a]);  
  46.     }else  
  47.     {  
  48.         pb->pArray[a]|=(1<<b);  
  49.         //printf("%d\n",pb->pArray[a]);  
  50.     }  
  51.     return 0;  
  52. }  
  53.   
  54. //获取bit位  
  55. //@pb:指向Bit的一个对象, 如果为NULL, 返回1  
  56. //@index: 需要设置的bit位, 范围应当是 [0~used)  
  57. //@return: 成功返回0, 如果出现pb==NULL, index out of range, 返回1  
  58. int getBit(Bit *pb,unsigned long index)  
  59. {  
  60.     if(pb==NULL || pb->pArray==NULL || index<0 || index>=pb->used)  
  61.         return 1;  
  62.     unsigned long a=index/CHAR_LENGTH;  
  63.     unsigned long b=index%CHAR_LENGTH;  
  64.     //printf("%d\n",pb->pArray[a]&(1<<b));  
  65.     if((pb->pArray[a]&(1<<b))==0)  
  66.         return 0;  
  67.     else  
  68.         return 1;  
  69. }  
  70.   
  71. //返回使用的长度  
  72. //@pb: 如果pb==NULL, return -1  
  73. int bitLength(Bit *pb)  
  74. {  
  75.     if(pb==NULL || pb->pArray==NULL)  
  76.         return -1;  
  77.     return pb->used;  
  78. }  
  79.   
  80. //销毁Bit对象  
  81. void freeBit(Bit *pb)  
  82. {  
  83.     if(pb==NULL)  
  84.         return;  
  85.     if(pb->pArray!=NULL)  
  86.     {  
  87.         free(pb->pArray);  
  88.         pb->pArray=NULL;  
  89.     }  
  90.     free(pb);  
  91. }  

2. 编写SWIG使用的swg文件

//Bit.i

[plain]  view plain copy
  1. %module Bit #module name  
  2. %{  
  3. #include "Bit.h" #加入Bit_wrap.c文件  
  4. %}  
  5.   
  6. #需要导出到python的函数  
  7. extern Bit* createBit(unsigned long len);  
  8. extern int setBit(Bit *pb,unsigned long index, int value);  
  9. extern int getBit(Bit *pb,unsigned long index);  
  10. extern int bitLength(Bit *pb);  
  11. extern void freeBit(Bit *pb);  

3. 编译

3.1 使用Bit.i生成wrap文件,该命令得到Bit_wrap.c Bit.py

[plain]  view plain copy
  1. [username]$ swig -python Bit.i  
3.2 编译Bit.c Bit_wrap.c文件,其中PYTHON_INCLUDE为python安装目录下的include文件夹, 如/usr/program/python/include/python.2.7username]$ gcc -c

[plain]  view plain copy
  1. [username]$ gcc -c -fpic Bit.c Bit_wrap.c -I${PYTHON_INCLUDE}  
3.3 连接得到动态库

[plain]  view plain copy
  1. [username]$ gcc -shared Bit.o Bit_wrap.o -o _Bit.so  
注意输出库文件为_Bit.so, 有下划线和模块名称组成
4.使用,这里以计算一亿一下的素数个数来演示

将Bit.py和_Bit.so文件复制到需要的位置

编写prime_count.py

[python]  view plain copy
  1. #coding=utf-8  
  2. #create-2012-10-17  
  3. #version: 1.0  
  4. #计算小于1亿的素数个数  
  5.   
  6. import time  
  7. from Bit  import *  
  8.   
  9. MAX=100000000  
  10. count=1  
  11.   
  12. #0 表示素数  
  13. #1 表示已经去除  
  14.   
  15. start_time=time.time()  
  16.   
  17. len=MAX/2-1  
  18. b=createBit(len)  
  19.   
  20. def remove(idx):  
  21.     i=idx  
  22.     global b  
  23.     while i<len:  
  24.         #b[i]=1  
  25.         setBit(b,i,1)  
  26.         i+=2*idx+3  
  27.   
  28. for i in range(3,MAX,2):  
  29.     if getBit(b,(i-3)/2)==0:  
  30.         #素数  
  31.         count+=1  
  32.         remove((i-3)/2)  
  33.   
  34. end_time=time.time()  
  35. print count  
  36. print end_time-start_time  
使用方法和其他python提供的模块一样

5.附计算结果

使用以上方法(c扩展python)得到结果为:

[html]  view plain copy
  1. 5761455(素数个数)  
  2. 113.465720892(second)  
另外,如果直接使用python模拟bit,并同样计算一亿一下的素数个数,结果为

[plain]  view plain copy
  1. 5761455(素数个数)  
  2. 293.473055124(second)  
可以看到,性能提升非常明显(接近3倍),当然这只是一个例子,并不具有太多代表性

另附,同样的方法,如果是c语言,调用我们实现的Bit的话结果为:

[plain]  view plain copy
  1. 5761455(素数个数)  
  2. 5(second)  

计算这种事情还是交给c做比较恰当

关于更多素数计算的信息见我的博客-数的计算

<think>我们正在解决用户安装lgpio包时出现的错误:command 'swig' failed: No such file or directory 根据引用[1]和[2]可知,出现swig命令找不到的问题是因为系统中没有安装swig或者swig依赖的共享库(如libpcre)缺失。 引用[3]表明,尝试通过pip安装swig是行不通的,因为swig并不是一个Python包,它是一个独立的工具。 因此,我们需要在系统级别安装swig。 解决步骤: 1. 安装swig:根据操作系统的不同,安装方法不同。 - 对于Ubuntu/Debian系统: sudo apt update sudo apt install swig - 对于CentOS/RHEL系统: sudo yum install swig - 对于Arch Linux: sudo pacman -S swig 2. 如果安装swig后仍然出现关于共享库的错误(如引用[1]中提到的libpcre.so.1缺失),则需要安装相应的库。 - 在Ubuntu/Debian上,可以安装libpcre3: sudo apt install libpcre3 - 或者创建符号链接(如果库已安装但版本不对,例如有libpcre.so.3但没有libpcre.so.1): sudo ln -s /usr/lib/x86_64-linux-gnu/libpcre.so.3 /usr/lib/x86_64-linux-gnu/libpcre.so.1 3. 重新安装lgpio包。 注意:引用[4]提到了cl.exe的错误,这是Windows上的编译工具,与当前问题无关,可以忽略。 因此,针对用户的问题,我们给出如下解决方案:</think>### 解决安装lgpio时出现"command 'swig' failed: No such file or directory"错误 此错误表明系统缺少**SWIG**(Simplified Wrapper and Interface Generator)工具,该工具是编译C/C++扩展模块(如lgpio)的必要依赖。以下是完整解决方案: #### 1. **安装SWIG核心工具** 根据操作系统选择安装命令: ```bash # Ubuntu/Debian sudo apt update && sudo apt install swig # CentOS/RHEL sudo yum install swig # Arch/Manjaro sudo pacman -S swig # macOS (Homebrew) brew install swig ``` #### 2. **补充依赖库(解决libpcre问题)** 若出现`libpcre.so.1`缺失错误(如引用[1]所示),需安装PCRE库: ```bash # Ubuntu/Debian sudo apt install libpcre3 libpcre3-dev # CentOS/RHEL sudo yum install pcre pcre-devel ``` #### 3. **验证SWIG安装** ```bash swig -version ``` 正常输出应显示类似: ``` SWIG Version 4.1.1 ... ``` #### 4. **安装构建工具链** 补充开发工具集: ```bash # Ubuntu/Debian sudo apt install build-essential python3-dev # CentOS/RHEL sudo yum groupinstall "Development Tools" ``` #### 5. **重新安装lgpio** ```bash # 清除旧缓存 pip cache purge # 重新安装 pip install --no-cache-dir lgpio ``` #### 替代方案:预编译二进制包 若仍失败,可尝试免编译安装: ```bash pip install lgpio --only-binary :all: --prefer-binary ``` > **原理说明**:SWIG用于将C/C++代码转换为Python扩展模块[^1]。lgpio依赖此工具生成`_lgpio.c`接口文件,缺失会导致编译失败[^2]。安装开发工具链确保拥有完整的编译环境。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值