跪求:使用swig转化C++为python代码中的数组问题

songqiangqiang 2009-12-14 03:02:03
最近在学习swig,网上的例子可以通过,但是试着转化数组的时候出了问题下面贴出各个文件
请教打印数组内容的方法:
使用的是linux系统库的生成方法如下:
swig -c++ -python md52.i
g++ -fpic -c -I/usr/include/python2.4 -I/usr/lib/python2.4/config md52.cpp md52_wrap.cxx
g++ -shared -o _md52.so md52.o md52_wrap.o -L/usr/lib/python2.4 -lpython2.4

具体使用的时候
[@66.223 md5]# python
Python 2.4.3 (#1, Sep 17 2008, 16:07:08)
[GCC 4.1.2 20071124 (Red Hat 4.1.2-41)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import md52
>>> a=md52.md5_long_128()
>>> a.intData[0]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unsubscriptable object
>>>



md52.i
//////////////////////////////////////////////
%module md52
%{
/* Includes the header in the wrapper code */
#include "md52.h"
%}

/* Parse the header file to generate wrappers */
%include "md52.h"


md52.h
////////////////////////////////////////////

struct md5_long_128{

unsigned int intData [4];

// union{
// char byteData[16]; /*data stored in md5-byte format*/
// unsigned int intData [4]; /*data stored in int format*/
// }data; /*output vector union of Md5 Algorithm*/

/*-------------------------------------
compare fuction definition of
output vector of length 128
--------------------------------------*/
bool
operator ==(
const md5_long_128 & a /*the other md5 output vector of length 128*/
)const
{
return (intData[0]==a.intData[0])&&
(intData[1]==a.intData[1])&&
(intData[2]==a.intData[2])&&
(intData[3]==a.intData[3]);
}

};

md5_long_128 getSign128(const char * inputStr, int inputLen=-1);

md5.cpp
//////////////////////////////////////////////////////////////////////////
#include "md52.h"
#include <string.h>
#include <stdio.h>


md5_long_128 getSign128(const char * inputStr , int inputLen)
{
md5_long_128 digest; /*output vector of the md5 procedure */

printf("a");

return digest;

}
...全文
343 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
thy38 2009-12-15
  • 打赏
  • 举报
回复
刚刚去SWIG官网的文档看了一下:Unfortunately, our Python version suffers a number of drawbacks. Most notably, there is no way for us to access any of the grid data (which is easily accomplished in C++). However, there are ways to fix this :

也就是说,直接访问不行,可以问其它方法包装一下,加一层Wrapper函数:
%module pde
%{
#include "pde.h"
%}

%include pde.h

// Add a few "helper" functions to extract grid data
%inline %{
double Grid2d_get(Grid2d *g, int i, int j) {
return g->data[i][j];
}
void Grid2d_set(Grid2d *g, int i, int j, double val) {
g->data[i][j] = val;
}
%}

在Python中:
# An example using our set/get functions

from pde import *

# Set up an initial condition
def initcond(h):
h.set_temp(0.0)
nx = h.grid.xpoints
for i in range(0,nx):
Grid2d_set(h.grid,i,0,1.0) # Set grid values

# Dump out to a file
def dump(h,filename):
f = open(filename,"w")
nx = h.grid.xpoints
ny = h.grid.ypoints
for i in range(0,nx):
for j in range(0,ny):
f.write(str(Grid2d_get(h.grid,i,j))+"\n") # Get grid value
f.close()

# Set up a problem and run it

h = Heat2d(50,50)
initcond(h)
fileno = 1
for i in range(0,25):
h.solve(100)
dump(h,"Dat"+str(fileno))
print "time = ", h.time
fileno = fileno+1
thy38 2009-12-15
  • 打赏
  • 举报
回复
前一阵子看过SWIG,一个月不用又忘了不少。回头我看看

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧