Python论坛  - 讨论区

标题:Re: [python-chinese] 请问swig的一个简单问题

2005年07月17日 星期日 00:58

Hou Ming Yuan houmingyuan at gmail.com
Sun Jul 17 00:58:38 HKT 2005

这个问题还是链接库的原因。我在windows平台下没有用mingw试过,只有用VC试过是可以的。
你可以先试试在链接这步加-lpython2.4试试。我也不清楚mingw是否与linux的库文件时兼容的。你可以先用nm
libpython.2.4.a| grep _imp__PyString_FromFormat
看看这个.a文件是否有需要的符号。
希望能有所帮助。
 如何写setup.py,你最好看python manual -- Distributing Python Modules.
我还没写过,要请达人介绍一下。
 在05-7-16,shhgs <shhgs.efhilt at gmail.com> 写道: 
> 
> 以下是在Windows下编译时报的错。不是Lib的原因,我设了lib变量,而且把libpython2.4.a拷贝到编译器的lib目录里了。
> 
> ---------------------------------------------------------------------------------------------------------------
> swig -c++ -python cppobj.i
> g++ -c cppobj_wrap.cxx -fpic -IC:/Python24/include
> cppobj_wrap.cxx:1: warning: -fpic ignored for target (all code is
> position independent)
> g++ -o _cppobj.so cppobj_wrap.o -shared -fpic
> cppobj_wrap.o(.text+0x9f7):cppobj_wrap.cxx: undefined reference to
> `_imp__PyString_FromFormat'
> cppobj_wrap.o(.text+0xa59):cppobj_wrap.cxx: undefined reference to
> `_imp__PyString_FromString'
> cppobj_wrap.o(.text+0xa8a):cppobj_wrap.cxx: undefined reference to
> `_imp__PyLong_FromUnsignedLong'
> cppobj_wrap.o(.text+0xadd):cppobj_wrap.cxx: undefined reference to
> `_imp__PyOS_snprintf'
> cppobj_wrap.o(.text+0xaea):cppobj_wrap.cxx: undefined reference to
> `_imp__PyString_FromString'
> cppobj_wrap.o(.text+0xb1c):cppobj_wrap.cxx: undefined reference to
> `_imp__PyOS_snprintf'
> cppobj_wrap.o(.text+0xb29):cppobj_wrap.cxx: undefined reference to
> `_imp__PyString_FromString'
> cppobj_wrap.o(.text+0xbab):cppobj_wrap.cxx: undefined reference to
> `_imp__PyObject_Free'
> cppobj_wrap.o(.text+0xbf4):cppobj_wrap.cxx: undefined reference to
> `_imp__PyType_Type'
> cppobj_wrap.o(.text+0xcc4):cppobj_wrap.cxx: undefined reference to
> `_imp__PyObject_Malloc'
> cppobj_wrap.o(.text+0xcd2):cppobj_wrap.cxx: undefined reference to
> `_imp__PyObject_Init'
> cppobj_wrap.o(.text+0xe11):cppobj_wrap.cxx: undefined reference to
> `_imp__PyString_FromFormat'
> cppobj_wrap.o(.text+0xe31):cppobj_wrap.cxx: undefined reference to
> `_imp__PyString_FromFormat'
> cppobj_wrap.o(.text+0xea1):cppobj_wrap.cxx: undefined reference to
> `_imp__PyString_FromFormat'
> cppobj_wrap.o(.text+0xec1):cppobj_wrap.cxx: undefined reference to
> `_imp__PyString_FromFormat'
> cppobj_wrap.o(.text+0xf97):cppobj_wrap.cxx: undefined reference to
> `_imp__PyObject_Free'
> cppobj_wrap.o(.text+0xfe0):cppobj_wrap.cxx: undefined reference to
> `_imp__PyType_Type'
> cppobj_wrap.o(.text+0x10a6):cppobj_wrap.cxx: undefined reference to
> `_imp__PyObject_Malloc'
> cppobj_wrap.o(.text+0x10b4):cppobj_wrap.cxx: undefined reference to
> `_imp__PyObject_Init'
> cppobj_wrap.o(.text+0x116c):cppobj_wrap.cxx: undefined reference to
> `_imp__PyExc_TypeError'
> cppobj_wrap.o(.text+0x1176):cppobj_wrap.cxx: undefined reference to
> `_imp__PyErr_Format'
> cppobj_wrap.o(.text+0x11b3):cppobj_wrap.cxx: undefined reference to
> `_imp__PyObject_Str'
> cppobj_wrap.o(.text+0x11c9):cppobj_wrap.cxx: undefined reference to
> `_imp__PyString_AsString'
> cppobj_wrap.o(.text+0x1205):cppobj_wrap.cxx: undefined reference to
> `_imp__PyExc_TypeError'
> cppobj_wrap.o(.text+0x120f):cppobj_wrap.cxx: undefined reference to
> `_imp__PyErr_Format'
> cppobj_wrap.o(.text+0x122e):cppobj_wrap.cxx: undefined reference to
> `_imp__PyExc_TypeError'
> cppobj_wrap.o(.text+0x1238):cppobj_wrap.cxx: undefined reference to
> `_imp__PyErr_Format'
> cppobj_wrap.o(.text+0x126d):cppobj_wrap.cxx: undefined reference to
> `_imp__PyExc_TypeError'
> cppobj_wrap.o(.text+0x1277):cppobj_wrap.cxx: undefined reference to
> `_imp__PyErr_Format'
> cppobj_wrap.o(.text+0x1288):cppobj_wrap.cxx: undefined reference to
> `_imp__PyExc_TypeError'
> cppobj_wrap.o(.text+0x1292):cppobj_wrap.cxx: undefined reference to
> `_imp__PyErr_Format'
> cppobj_wrap.o(.text+0x12f5):cppobj_wrap.cxx: undefined reference to
> `_imp__PyErr_Occurred'
> cppobj_wrap.o(.text+0x132d):cppobj_wrap.cxx: undefined reference to
> `_imp__PyErr_Fetch'
> cppobj_wrap.o(.text+0x1344):cppobj_wrap.cxx: undefined reference to
> `_imp__PyObject_Str'
> cppobj_wrap.o(.text+0x135b):cppobj_wrap.cxx: undefined reference to
> `_imp__PyErr_Clear'
> cppobj_wrap.o(.text+0x136e):cppobj_wrap.cxx: undefined reference to
> `_imp__PyString_AsString'
> cppobj_wrap.o(.text+0x138e):cppobj_wrap.cxx: undefined reference to
> `_imp__PyErr_Format'
> cppobj_wrap.o(.text+0x139d):cppobj_wrap.cxx: undefined reference to
> `_imp__PyString_AsString'
> cppobj_wrap.o(.text+0x13bf):cppobj_wrap.cxx: undefined reference to
> `_imp__PyErr_Format'
> cppobj_wrap.o(.text+0x1402):cppobj_wrap.cxx: undefined reference to
> `_imp__PyErr_Occurred'
> cppobj_wrap.o(.text+0x1488):cppobj_wrap.cxx: undefined reference to
> `_imp___Py_NoneStruct'
> cppobj_wrap.o(.text+0x14ca):cppobj_wrap.cxx: undefined reference to
> `_imp__PyString_FromString'
> cppobj_wrap.o(.text+0x14eb):cppobj_wrap.cxx: undefined reference to
> `_imp__PyObject_GetAttr'
> cppobj_wrap.o(.text+0x15d2):cppobj_wrap.cxx: undefined reference to
> `_imp___Py_ZeroStruct'
> cppobj_wrap.o(.text+0x15e9):cppobj_wrap.cxx: undefined reference to
> `_imp__PyObject_SetAttrString'
> cppobj_wrap.o(.text+0x15fc):cppobj_wrap.cxx: undefined reference to
> `_imp__PyErr_Clear'
> cppobj_wrap.o(.text+0x1624):cppobj_wrap.cxx: undefined reference to
> `_imp__PyCFunction_Type'
> cppobj_wrap.o(.text+0x1750):cppobj_wrap.cxx: undefined reference to
> `_imp__PyErr_Clear'
> cppobj_wrap.o(.text+0x17ea):cppobj_wrap.cxx: undefined reference to
> `_imp__PyErr_Clear'
> cppobj_wrap.o(.text+0x189c):cppobj_wrap.cxx: undefined reference to
> `_imp___Py_NoneStruct'
> cppobj_wrap.o(.text+0x18a3):cppobj_wrap.cxx: undefined reference to
> `_imp___Py_NoneStruct'
> cppobj_wrap.o(.text+0x18cd):cppobj_wrap.cxx: undefined reference to
> `_imp___Py_NoneStruct'
> cppobj_wrap.o(.text+0x18ff):cppobj_wrap.cxx: undefined reference to
> `_imp__Py_BuildValue'
> cppobj_wrap.o(.text+0x1936):cppobj_wrap.cxx: undefined reference to
> `_imp__PyObject_CallObject'
> cppobj_wrap.o(.text+0x1969):cppobj_wrap.cxx: undefined reference to
> `_imp___Py_TrueStruct'
> cppobj_wrap.o(.text+0x1980):cppobj_wrap.cxx: undefined reference to
> `_imp__PyObject_SetAttrString'
> cppobj_wrap.o(.text+0x19ac):cppobj_wrap.cxx: undefined reference to
> `_imp___Py_NoneStruct'
> cppobj_wrap.o(.text+0x19b3):cppobj_wrap.cxx: undefined reference to
> `_imp___Py_NoneStruct'
> cppobj_wrap.o(.text+0x1a05):cppobj_wrap.cxx: undefined reference to
> `_imp__PyCObject_Import'
> cppobj_wrap.o(.text+0x1a11):cppobj_wrap.cxx: undefined reference to
> `_imp__PyErr_Occurred'
> cppobj_wrap.o(.text+0x1a1c):cppobj_wrap.cxx: undefined reference to
> `_imp__PyErr_Clear'
> cppobj_wrap.o(.text+0x1bd2):cppobj_wrap.cxx: undefined reference to
> `_imp__PyErr_Clear'
> cppobj_wrap.o(.text+0x1be0):cppobj_wrap.cxx: undefined reference to
> `_imp__PyString_Type'
> cppobj_wrap.o(.text+0x1be7):cppobj_wrap.cxx: undefined reference to
> `_imp__PyString_Type'
> cppobj_wrap.o(.text+0x1bf9):cppobj_wrap.cxx: undefined reference to
> `_imp__PyType_IsSubtype'
> cppobj_wrap.o(.text+0x1cf9):cppobj_wrap.cxx: undefined reference to
> `_imp__PyErr_Clear'
> cppobj_wrap.o(.text+0x1e2a):cppobj_wrap.cxx: undefined reference to
> `_imp__PyErr_Clear'
> cppobj_wrap.o(.text+0x1e46):cppobj_wrap.cxx: undefined reference to
> `_imp__PyExc_TypeError'
> cppobj_wrap.o(.text+0x1e50):cppobj_wrap.cxx: undefined reference to
> `_imp__PyErr_SetString'
> cppobj_wrap.o(.text+0x1eeb):cppobj_wrap.cxx: undefined reference to
> `_imp__PyArg_ParseTuple'
> cppobj_wrap.o(.text+0x1f31):cppobj_wrap.cxx: undefined reference to
> `_imp__PyErr_Occurred'
> cppobj_wrap.o(.text+0x21d8):cppobj_wrap.cxx: undefined reference to
> `_imp__PyExc_TypeError'
> cppobj_wrap.o(.text+0x21e2):cppobj_wrap.cxx: undefined reference to
> `_imp__PyErr_Format'
> cppobj_wrap.o(.text+0x21f3):cppobj_wrap.cxx: undefined reference to
> `_imp__PyExc_TypeError'
> cppobj_wrap.o(.text+0x21fd):cppobj_wrap.cxx: undefined reference to
> `_imp__PyErr_Format'
> cppobj_wrap.o(.text+0x227a):cppobj_wrap.cxx: undefined reference to
> `_imp__PyArg_ParseTuple'
> cppobj_wrap.o(.text+0x24eb):cppobj_wrap.cxx: undefined reference to
> `_imp___Py_NoneStruct'
> cppobj_wrap.o(.text+0x2502):cppobj_wrap.cxx: undefined reference to
> `_imp__PyString_FromStringAndSize
> 
> cppobj_wrap.o(.text+0x253a):cppobj_wrap.cxx: undefined reference to
> `_imp__PyArg_ParseTuple'
> cppobj_wrap.o(.text+0x259e):cppobj_wrap.cxx: undefined reference to
> `_imp___Py_NoneStruct'
> cppobj_wrap.o(.text+0x25a5):cppobj_wrap.cxx: undefined reference to
> `_imp___Py_NoneStruct'
> cppobj_wrap.o(.text+0x25dc):cppobj_wrap.cxx: undefined reference to
> `_imp__PyArg_ParseTuple'
> cppobj_wrap.o(.text+0x2610):cppobj_wrap.cxx: undefined reference to
> `_imp__Py_BuildValue'
> cppobj_wrap.o(.text+0x262c):cppobj_wrap.cxx: undefined reference to
> `_imp__PyString_FromString'
> cppobj_wrap.o(.text+0x2708):cppobj_wrap.cxx: undefined reference to
> `_imp__PyExc_NameError'
> cppobj_wrap.o(.text+0x2712):cppobj_wrap.cxx: undefined reference to
> `_imp__PyErr_SetString'
> cppobj_wrap.o(.text+0x2778):cppobj_wrap.cxx: undefined reference to
> `_imp__PyExc_NameError'
> cppobj_wrap.o(.text+0x2782):cppobj_wrap.cxx: undefined reference to
> `_imp__PyErr_SetString'
> cppobj_wrap.o(.text+0x27b1):cppobj_wrap.cxx: undefined reference to
> `_imp__PyType_Type'
> cppobj_wrap.o(.text+0x28cf):cppobj_wrap.cxx: undefined reference to
> `_imp__PyInt_FromLong'
> cppobj_wrap.o(.text+0x28f0):cppobj_wrap.cxx: undefined reference to
> `_imp__PyFloat_FromDouble'
> cppobj_wrap.o(.text+0x2923):cppobj_wrap.cxx: undefined reference to
> `_imp__PyString_FromString'
> cppobj_wrap.o(.text+0x2932):cppobj_wrap.cxx: undefined reference to
> `_imp___Py_NoneStruct'
> cppobj_wrap.o(.text+0x2939):cppobj_wrap.cxx: undefined reference to
> `_imp___Py_NoneStruct'
> cppobj_wrap.o(.text+0x29f0):cppobj_wrap.cxx: undefined reference to
> `_imp__PyDict_SetItemString'
> cppobj_wrap.o(.text+0x2c50):cppobj_wrap.cxx: undefined reference to
> `_imp__Py_InitModule4'
> cppobj_wrap.o(.text+0x2c68):cppobj_wrap.cxx: undefined reference to
> `_imp__PyCObject_FromVoidPtr'
> cppobj_wrap.o(.text+0x2c93):cppobj_wrap.cxx: undefined reference to
> `_imp__PyModule_AddObject'
> cppobj_wrap.o(.text+0x2d34):cppobj_wrap.cxx: undefined reference to
> `_imp__Py_InitModule4'
> cppobj_wrap.o(.text+0x2d44):cppobj_wrap.cxx: undefined reference to
> `_imp__PyModule_GetDict'
> collect2: ld returned 1 exit status
> mingw32-make: *** [_cppobj.so] Error 1
> 
> ---------------------------------------------------------------------------------------------------------------
> 
> On 7/16/05, Hou Ming Yuan <houmingyuan at gmail.com> wrote:
> >
> >
> > 1)List-1: cppobj.h
> > //----------------------------------------------------
> > // cppobj.h
> >
> > #include 
> > using namespace std;
> > class cppobj {
> > private :
> > string s;
> > public:
> > cppobj(string a): s(a) {};
> > string show() {return s;}
> > };
> > //-----------------------------------------------------
> > 2) List-2 cppobj.i
> > %module cppobj
> > %include "std_string.i"
> >
> > %{
> > #include "cppobj.h"
> > %}
> >
> > %include "cppobj.h"
> > //-----------------------------------------------------
> > 3) List-3 Makefile
> >
> >
> > all: _cppobj.so
> >
> > _cppobj.so: cppobj_wrap.o
> > g++ -o $@ $< -shared -fpic
> >
> > cppobj_wrap.cxx: cppobj.i cppobj.h
> > swig -c++ -python cppobj.i
> >
> > cppobj_wrap.o: cppobj.h cppobj_wrap.cxx
> > g++ -c cppobj_wrap.cxx -fpic -I/usr/include/python2.4
> >
> > clean:
> > rm -rf cppobj_wrap.cxx _cppobj.so *.o cppobj.py cppobj.pyc
> >
> > 4) 一下使我如何使用的
> > [houmingyuan at mycomputer swig]$ python
> > Python 2.4.1 (#1, Jun 19 2005, 15:23:32)
> > [GCC 3.4.3 20041212 (Red Hat 3.4.3-9.EL4)] linux2
> > Type "help", "copyright", "credits" or "license" for more information.
> >
> >
> > >>> import cppobj
> > >>> c = cppobj.cppobj("hello")
> > >>> c.show()
> > 'hello'
> > >>>
> >
> >
> > 在05-7-16,shhgs <shhgs.efhilt at gmail.com> 写道:
> > > 怎样让Python使用C++的类?
> > > //----------------------------------------------------
> > > // cppobject.cc
> > > #include 
> > > using namespace std;
> > >
> > > class cppobj {
> > > private :
> > > string s;
> > > public :
> > > cppobj(string a): s(a) {};
> > > string show() {return s;}
> > > };
> > > //-----------------------------------------------------
> > >
> > > 比方说我有上面这个类,现在我想让python使用cppobject的对象
> > > >>> c = cppobj("hello")
> > > >>> print c.show()
> > > hello
> > >
> > > 请问怎样用swig实现?
> > >
> > >
> > >
> > >
> > > On 7/15/05, Hou Ming Yuan < houmingyuan at gmail.com> wrote:
> > > > 个人觉得还是用swig比较简单一点。可以直接使用swig对C++头文件进行装换。主要分成四步
> > > > 1)准备好C++的源文件
> > > > 如下
> > > > car.cpp
> > > > car.h
> > > > 2) 编写interface文件
> > > > car.i
> > > > %module car
> > > > %{
> > > > #include "car.h"
> > > > %}
> > > >
> > > > %include car.h
> > > >
> > > > 3) 使用SWIG生成warpper文件
> > > > #swig -c++ -python car.i
> > > > 生成两个文件 car_wrapper.cxx car.py
> > > >
> > > > 4) 将源文件和car_wrapper.cxx一起编译成一个动态库_car.so
> > > >
> > > > 之后就可以在python 中使用
> > > > #python
> > > > #import car
> > > >
> > > >
> > 这些步骤说起来多,如果写在makefile里面就很简单了,以后即使C++的类更改只要重新用swig生成,不需要额外手工劳动了。
> > > >
> > > > 在05-7-15,kassarar < kassarar at 126.com> 写道:
> > > > >
> > > >
> > 请问,用swig怎么把class或者struct转为python的class啊,好像我每一次编出来的模块都没有原来头文件里面定义过的类
> > > > >
> > > > >
> > > > >
> > > > >
> > > > >
> > > > >
> > > > >
> > > > > 需要一个2000兆的免费邮箱吗?
> > > > > 网易免费邮箱是 中国最多人使用的电子邮箱。
> > > > > _______________________________________________
> > > > > python-chinese list
> > > > > python-chinese at lists.python.cn
> > > > > http://python.cn/mailman/listinfo/python-chinese
> > > > >
> > > > >
> > > > >
> > > >
> > > >
> > > > _______________________________________________
> > > > python-chinese list
> > > > python-chinese at lists.python.cn
> > > > http://python.cn/mailman/listinfo/python-chinese
> > > >
> > > >
> > > >
> > >
> >
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20050717/6aea0b46/attachment-0001.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号