Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.
You can subscribe to this list here.
Showing
5
results of 5
Dear all,
I'm in the process of building a Ruby SWIG wrapper for the mxflib
() library. And I have
encountered the following issue that has left me rather puzzled. I've
constructed a similar example to illustrate the issue.
Given the swig input (see below), this warning is produced:
Warning 401: Nothing known about base class 'ItemList'. Ignored.
In fact, no functionality of the std::list is inherited by the MyTest
class wrapper. However, removing the bla namespace declaration and all
"bla::" prefixes completely solves this issue and a complete wrapper is
successfully generated. Unfortunately, leaving out namespaces is not an
option in the case of the library I'm trying to wrap. I have pretty much
tried all combinations of leaving out or putting in the "bla::" prefix
(including "::bla::") but to no avail, the ItemList remains unknown for
SWIG.
I've tested this with both SWIG 2.0.4 and 1.3.40, using command:
swig -v -c++ -ruby swigtest.i
It seems that SWIG, when namespaces are involved, in this case fails to
recursively resolve the Item and ItemList typedefs (ItemList ->
list<Item> -> pair<string,MyTest>) despite the given %template directives?
Is this expected behavior? And can anything be done to solve this?
Thanks!
Kind regards,
Dieter Van Rijsselbergen.
-----------------------------swigtest.i-----------------------------
%module swigtest
%include "std_string.i"
%include "std_list.i"
%include "std_pair.i"
%{
#include <string>
#include <list>
%}
namespace bla {
class MyTest; // forward declaration
typedef std::pair<std::string, MyTest> Item;
%template(Item) std::pair<std::string, bla::MyTest>;
typedef std::list<Item> ItemList;
%template(ItemList) std::list<bla::Item>;
class MyTest : public ItemList // MyTest is a list of other MyTests
{
public:
MyTest() {}
size_type length() { return size(); } // call superclass size()
};
}
-----------------------------swigtest.i-----------------------------
--
Limecraft - "Creativity Freshly Connected" -
Gaston Crommenlaan 10/101
B-9050 Gent.
On Sunday June 12 2011 11:41:42 Teto wrote:
> I dunno why but I've touched some things and I' ve been able to create
> the shared library.
>
> However when launching my test.py
>
> ======================
> File "test.py", line 3, in <module>
> import pypbc
> File "/home/teto/pfe/pypbc.py", line 80, in <module>
> class __mpz_struct(_object):
> File "/home/teto/pfe/pypbc.py", line 86, in __mpz_struct
> __swig_setmethods__["_mp_alloc"] = _pypbc.__mpz_struct__mp_alloc_set
> AttributeError: 'module' object has no attribute
> '_mpz_struct__mpz_struct__mp_alloc_set'
> ======================
>
> Here is the result of the command " nm _pypbc.so |grep
> __mpz_struct__mp_alloc_set":
> 0000000000018b09 t _wrap___mpz_struct__mp_alloc_set
>
> Btw, when doing "nm _pypbc.so" I have some undefined symbols, should
> they all be defined ? Here are some of them
> U pbc_realloc
> U pbc_set_memory_functions
> U pbc_strdup
> U printf
> U strncpy
> U strstr
> on the contrary some are correctly defined:
> 0000000000017fb6 t pbc_param_clear
>
>
> Any idea ?
Is libpbc.so (or what it is called) included in your link path? On Linux that
is the LD_LIBRARY_PATH.
Johan
> On Sun, Jun 12, 2011 at 12:01 PM, Teto <mattator@...> wrote:
> > You are right that made perfect sense. In fact, when I modified the file,
> > I misreplaced paths. So now swig correctly generates my pybc_wrap.c
> > file. Thank you very much.
> >
> > I have new problems during the next step. I solved the first one (problem
> > with va_list) by ignoring it with #ifndef SWIG ... #endif as told on the
> > manual but the 2nd problem When trying to compile, gcc throws error
> > because of a typedef of a struct. (the original C library compiles just
> > fine):
> >
> > " pypbc_wrap.c:16208:12:error: incompatible types when assigning to type
> > ‘element_t’ from type ‘struct element_s *’
> >
> >
> >
> > Here is the orginial library declaration:
> > =================================
> > struct field_s;
> >
> > struct element_s {
> > struct field_s *field;
> > void *data;
> > };
> > typedef struct element_s *element_ptr;
> > typedef struct element_s element_t[1];
> > =================================
> >
> >
> > Here is some code around the swig generated c wrapping file:
> > =================================
> > // declaration at the beginning of the function
> > element_t arg2 ;
> > void *argp2 ;
> >
> > /// Swig generated code
> > ....
> > ....
> >
> > /// the line 16208 which triggers the error
> > arg2 = *((element_t *)(argp2));
> > =================================
> >
> > Is there a way out ?
> >
> > Thx again for your patience.
> >
> > On Sat, Jun 11, 2011 at 8:46 PM, Johan Hake <hake@...> wrote:
> >> On Saturday June 11 2011 12:32:17 Teto wrote:
> >> > thx for the quick answer.
> >> >
> >> > pbc/pbc.h only includes other headers so that's why nothing was bound.
> >> > I
> >>
> >> > changed my "pypbc.i" as you said (at least I think):
> >> Looks good.
> >>
> >> > ===============================
> >> > %module pypbc
> >> > %{
> >> > /* includes header*/
> >> > #include <pbc/pbc.h>
> >> > %}
> >> >
> >> > // Copy of <pbc/pbc.h> content
> >> > %include <pbc/utils.h>
> >> > %include <pbc/param.h>
> >> > // To make it shorter I don't paste everything
> >> > % .....
> >> > %include <pbc/memory.h>
> >> >
> >> > ===============================
> >> >
> >> > Then I launch
> >> > swig -Wall -I/usr/include -python pypbc.i
> >> > which answers:
> >> > pypbc.i:7: Error: Unable to find 'pbc/utils.h'
> >> >
> >> > The file exists I can see it. Is the problem due to the brackets
> >> > <pbc/utils.h> not supported ?
> >>
> >> Yes it is, but where are your header files situated? You need to add a
> >> -I/where/it/is to your swig command. But it is strange that it would not
> >> find it this time when it found it the first time...
> >>
> >> Johan
> >>
> >> > On Sat, Jun 11, 2011 at 8:00 PM, Johan Hake <hake@...> wrote:
> >> > > On Saturday June 11 2011 11:13:32 Teto wrote:
> >> > > > Hi,
> >> > > > t's my first time with Swig and I want to bind all functions of
> >> > > > the pbc library () from C to
> >> > > > python (2.7). All
> >> > >
> >> > > the
> >> > >
> >> > > > prototypes are declared in pbc/pbc.h so I do:
> >> > > >
> >> > > > Here is my pypbc.
> >> > > > %module pypbc
> >> > > > %{
> >> > > > /* includes header*/
> >> > > > #include <pbc/pbc.h>
> >> > > > %}
> >> > > > %include <pbc/pbc.h>
> >> > > >
> >> > > > I don't understand why I need to repeat twice the include of a
> >> > > > same file, once with '#', the other one with '%'.
> >> > >
> >> > > What is included inside a %{ %} clause will be directly inserted in
> >> > > the generated wrapper file, so all types declared in your library
> >> > > get available.
> >> > >
> >> > > All types declared outside any clause will be parsed by SWIG and
> >> > > wrapper code
> >> > > with types and functions will be generated. Instead of writing the
> >> > > declarations directly in the .i file you can include a .h file. This
> >> > > is what
> >> > > you do with the %include directive. Note however that files which
> >> > > are #included in pbc.h will not be parsed by SWIG.
> >> > >
> >> > > I suggest you either create a pbh.i file which is almost a copy of
> >> > > your pbc.h
> >> > > file with %includes instead of #includes and use this instead. Or
> >> > > you can %include the actuall .h files directly in your pbc.i file
> >> > >
> >> > > Johan
> >> > >
> >> > > > Then I run :
> >> > > > swig -Wall -I/usr/include -python pypbc.i
> >> > > > gcc -fPIC -c pypbc_wrap.c -I/usr/include/python2.7 -L/usr/lib
> >> > > > -lpbc ld -shared pypbc_wrap.o -o _pypbc.so
> >> > > >
> >> > > > There are no errors but when I launch python and import pypbc,
> >> > > > then type dir( pypbc ), there is no pbc function or structure
> >> > > > available , only Swig stuff. What I am doing wrong. Doesn't
> >> > > > %include <pbc/pbc.h> tell Swig to bind all the functions it would
> >> > > > find in those files ?
> >> > > >
> >> > > > Thx in advance.
> >> > > >
> >> > > > Matt
>
> ---------------------------------------------------------------------------
> --- EditLive Enterprise is the world's most technically advanced content
> authoring tool. Experience the power of Track Changes, Inline Image
> Editing and ensure content is compliant with Accessibility Checking.
>
> _______________________________________________
> Swig-user mailing list
> Swig-user@...
>
Thanks Ashish. That worked.
Willis
From: Ashish Sharma [mailto:ashishs99@...]
Sent: Friday, June 10, 2011 3:33 PM
To: Willis Cheung
Cc: swig-user@...
Subject: Re: [Swig-user] Objective C wrappers enum errors
Foo.h:
Class Test {
enum {foo1};
};
You are using an unnamed enum here and all unnamed enums are treated as
constants. Currently the default implementation with constants is buggy. You
can instead use a named enum and that is expected to work fine.
Or, you can also use the directive %objcconst(1) to wrap the unnamed enums
as const integers. So your module file would look something like:
%module Foo
%{
#include "Foo"
%}
%objcconst(1);
%include "Foo.h"
You can look at Examples/objc/enum for an example.
Ashish
William,
Thanks for the pointer. I am not sure if this is a GCC extension, though,
given the following quote from ISO C spec the author provided in that email:
--8<---8<---
7 An object shall have its stored value accessed only by an lvalue
expression that has one of the following types: {footnote 73}
a type compatible with the effective type of the object,
...
an aggregate or union type that includes one of the aforementioned
types among its members (including, recursively, a member of a
subaggregate or contained union), or
--8<---8<---
I am reading this requirement as permitting such "casts via union", but I may
be wrong.
Regards,
Alexey.
On Sunday, June 12, 2011 03:49:15 am William S Fulton wrote:
> The Java chapter talks about this and refers to a posting
> which
> mentions that this is a gcc extension. There seems to be no portable way
> to solve this problem.
>
> William
>
> On 12/06/11 07:44, Alexey Neyman wrote:
> > Well, the issue is that the code SWIG generates for JAVA bindings is not
> > safe to compile at -O2; I missed that warning in SWIG docs. But, why
> > does SWIG
> >
> > generate code like this:
> > jlong jresult = 0 ;
> > struct x *result = 0 ;
> > ...
> > result = (struct x *)x_create();
> > *(struct x **)&jresult = result;
> > return jresult;
> >
> > instead of something like this?
> >
> > union { jlong j; struct x *c; } result;
> > result.c = (struct x *)x_create();
> > return result.j;
> >
> > That would be compliant with strict aliasing rules and passes through
> > this test case.
> >
> > Regards,
> > Alexey.
> >
> > On Saturday, June 11, 2011 08:51:38 pm Alexey Neyman wrote:
> >> Hi all,
> >>
> >> I am experiencing a strange issue with Java bindings in SWIG. I have
> >> reduced it to the following test case. It works in Python and Perl just
> >> fine, but aborts in Java in x_destroy() when it tries to dereference
> >> x->y - somehow, x is NULL. I tried SWIG 1.3.40 and 2.0.4, both fail in
> >> the same way. Am I doing something wrong?
> >>
> >> --8<---8<--[ x1.c ]--8<--
> >> #include<stdlib.h>
> >> #include<x1.h>
> >>
> >> struct x *
> >> x_create(void)
> >> {
> >>
> >> struct x *x;
> >>
> >> x = malloc(sizeof(*x));
> >> x->y = malloc(sizeof(*x->y));
> >> x->y->dummy = 5;
> >> return x;
> >>
> >> }
> >>
> >> void
> >> x_destroy(struct x *x)
> >> {
> >>
> >> free(x->y);
> >> free(x);
> >>
> >> }
> >>
> >> --8<---8<--[ x1.h ]--8<--
> >> struct y { int dummy; };
> >> struct x { struct y *y; };
> >>
> >> struct x *x_create(void);
> >> void x_destroy(struct x *x);
> >>
> >> --8<---8<--[ x1.i ]--8<--
> >> %module x1
> >> %{
> >> #include<x1.h>
> >> %}
> >> %include<x1.h>
> >>
> >> --8<---8<--[ runme.java ]--8<--
> >> public class runme {
> >>
> >> static {
> >>
> >> System.loadLibrary("x1");
> >>
> >> }
> >> public static void main(String argv[]) {
> >>
> >> x1.x_destroy(x1.x_create());
> >>
> >> }
> >>
> >> }
> >> --8<---8<---8<---8<---8<--
> >>
> >> Compiled/run as follows:
> >>
> >> $ swig -java x1.i
> >> $ gcc -O2 -fPIC -shared -o libx1.so
> >> -I/usr/lib/jvm/java-6-openjdk/include \
> >>
> >> -I. x1_wrap.c x1.c
> >>
> >> $ javac *.java
> >> $ java -Djava.library.path=. runme
> >> ...
> >> # Problematic frame:
> >> # C [libx1.so+0x92a] x_destroy+0x1a
> >> ...
> >> Aborted
> >> $ objdump -d libx1.so | grep -B 9 92a
> >>
> >> 00000910<x_destroy>:
> >> 910: 55 push %ebp
> >> 911: 89 e5 mov %esp,%ebp
> >> 913: 83 ec 18 sub $0x18,%esp
> >> 916: 89 75 fc mov %esi,-0x4(%ebp)
> >> 919: 8b 75 08 mov 0x8(%ebp),%esi
> >> 91c: 89 5d f8 mov %ebx,-0x8(%ebp)
> >> 91f: e8 63 fe ff ff call 787<__i686.get_pc_thunk.bx>
> >> 924: 81 c3 d0 16 00 00 add $0x16d0,%ebx
> >> 92a: 8b 06 mov (%esi),%eax
> >>
> >> $ grep ESI hs_err_pid18038.log
> >> ESI=0x00000000
> >>
> >> Thanks in advance,
> >> Alexey.
> >>
> >> ------------------------------------------------------------------------
> >> --- --- EditLive Enterprise is the world's most technically advanced
> >> content authoring tool. Experience the power of Track Changes, Inline
> >> Image Editing and ensure content is compliant with Accessibility
> >> Checking.
> >> _______________________________________________
> >> Swig-user mailing list
> >> Swig-user@...
> >>
> >
> > -------------------------------------------------------------------------
> > ----- EditLive Enterprise is the world's most technically advanced
> > content authoring tool. Experience the power of Track Changes, Inline
> > Image Editing and ensure content is compliant with Accessibility
> > Checking.
> > _______________________________________________
> > Swig-user mailing list
> > Swig-user@...
> >
>From Makefile:
test_aquad3.out: aquad3.c aquad3.i test_aquad3.py
output:
Traceback (most recent call last):
File "test_aquad3.py", line 26, in ?
area=aquad3.aquad(f, xmin, xmax, eps)
TypeError: in method 'aquad', argument 1 of type 'double (*)(double)'
make: *** [test_aquad3.out] Error 1
/* aquad3.i SWIG interface file*/
%module aquad3
%{
/* Put header files here or function declarations like below */
/* aquad3.h adaptive quadrature numerical integration */
/* for a desired accuracy on irregular functions. */
/* define the function to be integrated as: */
/* double f(double x) */
/* { */
/* // compute value */
/* return value; */
/* } */
/* */
/* integrate from xmin to xmax */
/* approximate desired absolute error in all intervals, eps */
/* accuracy absolutely not guaranteed */
double aquad3(double f(double x), double xmin, double xmax, double eps);
%}
extern double aquad(double f(double x), double xmin, double xmax, double eps);
# test_aquad3.py using swig to interface to aquad3.c
import aquad3
xmin = 1.0
xmax = 2.0
eps = 0.001
def f(x):
return x*x
def quad(f, xmin, xmax, nstep): # integrate f(x) from xmin to xmax
area=(f(xmin)+f(xmax))/2.0
h = (xmax-xmin)/nstep
for i in range(1,nstep):
x = xmin+i*h
area = area + f(x)
return area*h # trapezoidal method
print "test_passing_function, not via swig"
n = 10
area = quad(f, xmin, xmax, n) # WORKS
print "area under x*x from ",xmin," to ",xmax," = ",area
print "test_aquad3.py using swig to interface to aquad3.c"
area=aquad3.aquad(f, xmin, xmax, eps) # FAILS
print "aquad3.aquad(f, xmin, xmax, eps) =", area
Probably not interesting, aquad3.c so anyone can run test
/* aquad3.c from book page 301, with modifications */
#include <stdio.h>
#undef abs
#define abs(a) ((a)<0.0?(-(a)):(a))
static double stack[100][7]; /* a place to store/retrieve */
static int top, maxtop; /* top points to where stored */
void store(double s0, double s1, double s2, double s3, double s4,
double s5, double s6)
{
stack[top][0] = s0;
stack[top][1] = s1;
stack[top][2] = s2;
stack[top][3] = s3;
stack[top][4] = s4;
stack[top][5] = s5;
stack[top][6] = s6;
}
void retrieve(double* s0, double* s1, double* s2, double* s3, double* s4,
double* s5, double* s6)
{
*s0 = stack[top][0];
*s1 = stack[top][1];
*s2 = stack[top][2];
*s3 = stack[top][3];
*s4 = stack[top][4];
*s5 = stack[top][5];
*s6 = stack[top][6];
} /* end retrieve */
double Sn(double F0, double F1, double F2, double h)
{
return h*(F0 + 4.0*F1 + F2)/3.0; /* 2h/6 */
} /* end Sn */
double RS(double F0, double F1, double F2, double F3,
double F4, double h) /* 4h/16 */
{
return h*(14.0*F0 +64.0*F1 + 24.0*F2 + 64.0*F3 + 14.0*F4)/45.0;
/* error term 8/945 h^7 f^(8)(c) */
} /* end RS */
/* name changed to plain aquad */
double aquad(double f(double x), double xmin, double xmax, double eps)
{
double a, b, c, d, e;
double Fa, Fb, Fc, Fd, Fe;
double h1, h2, hs, hmin;
double Sab, Sac, Scb, S2ab;
double tol; /* eps */
double val, value;
int i, ns; /* starting intervals */
maxtop = 99;
top = 0;
value = 0.0;
tol = eps;
ns = 9;
a = xmin;
hs = (xmax-xmin)/(double)(ns);
b = a + hs;
for(i=0; i<ns; i++) /* hueristic starter set */
{
h1 = (b-a)/2.0;
c = a + h1;
Fa = f(a);
Fc = f(c);
Fb = f(b);
Sab = Sn(Fa, Fc, Fb, h1);
store(a, Fa, Fc, Fb, h1, tol, Sab);
top++;
a = b;
b = a + hs;
}
while(top > 0)
{
top--;
retrieve(&a, &Fa, &Fc, &Fb, &h1, &tol, &Sab);
c = a + h1;
b = a + 2.0*h1;
h2 = h1/2;
d = a + h2;
e = a + 3.0*h2;
Fd = f(d);
Fe = f(e);
Sac = Sn(Fa, Fd, Fc, h2);
Scb = Sn(Fc, Fe, Fb, h2);
S2ab = Sac + Scb;
if(abs(S2ab-Sab) < tol || h2 < 1.0e-13)
{
val = RS(Fa, Fd, Fc, Fe, Fb, h2);
value = value + val;
}
else
{
h1 = h2;
tol = tol/2.0;
store(a, Fa, Fd, Fc, h1, tol, Sac);
top++;
store(c, Fc, Fe, Fb, h1, tol, Scb);
top++;
}
if(top>=maxtop)
{
printf("aquad3 failed a=%g, c=%g \n", a, c);
break;
}
} /* end while */
return value;
} /* end aquad3.c */
Jon Squire squire@... or squire@...