E
Esteban Manchado Velázquez
Hi,
I'm doing some experiments with SWIG (trying to make a Ruby interface =
for
MuSE low-level sound classes), and I'm having some problems.
One of them is really weird: it seems that there are problems with ext=
ern
"C" declarations (from C++) and with functions _named_ "error" (namely, t=
hat
when you try to call the function, the interpreter aborts, without except=
ions
or anything).
Here is my tiny example:
-- errortest.c / errortest.cxx ------ 8< --------------------------------=
-----
#include <stdio.h>
void error(const char *fmt, ...)
{
printf("I won't be called\n");
}
------------------------------------- >8 --------------------------------=
-----
-- errortest.i ---------------------- 8< --------------------------------=
-----
%module errortest
%{
#include "errortest.h"
%}
#ifdef __cplusplus
extern "C" {
#endif
void error(const char *format, ...);
#ifdef __cplusplus
}
#endif
------------------------------------- >8 --------------------------------=
-----
-- errortest.h ---------------------- 8< --------------------------------=
-----
#ifdef __cplusplus
extern "C" {
#endif
void error(const char *format, ...);
#ifdef __cplusplus
}
#endif
------------------------------------- >8 --------------------------------=
-----
-- Makefile ------------------------- 8< --------------------------------=
-----
BASENAME =3D errortest
WRAPPER_BASENAME =3D $(BASENAME)_wrap
SWIG =3D swig
INTERFACE =3D $(BASENAME).i
CXXSRCS =3D errortest.cpp
SRCDIR =3D .
OBJS =3D $(CXXSRCS:.cpp=3D.o)
INCLUDES =3D -I.
LINK =3D=20
CPP =3D 1
GCC =3D $(if $(CPP),g++,gcc)
SWIG_OPTS =3D $(if $(CPP),-c++,)
SRC_EXT =3D $(if $(CPP),cxx,c)
all: $(BASENAME).so
$(BASENAME).so: $(BASENAME).o $(WRAPPER_BASENAME).o
$(GCC) -shared $(OBJS) $(WRAPPER_BASENAME).o $(LINK) -o $@
$(BASENAME).o: $(SRCDIR)/$(BASENAME).$(SRC_EXT)
$(GCC) -fpic -fPIC -DHAVE_CONFIG_H -c -shared $(INCLUDES) $<
$(WRAPPER_BASENAME).o: $(WRAPPER_BASENAME).$(SRC_EXT)
$(GCC) -fpic -fPIC -DHAVE_CONFIG_H -c -shared $(INCLUDES) -I/usr/lib/rub=
y/1.8/i386-linux $(WRAPPER_BASENAME).$(SRC_EXT)
$(WRAPPER_BASENAME).$(SRC_EXT): $(INTERFACE)
$(SWIG) $(SWIG_OPTS) -ruby $(INTERFACE)
clean:
$(RM) $(BASENAME).so $(BASENAME).o $(WRAPPER_BASENAME).o $(WRAPPER_BASEN=
AME).$(SRC_EXT)
PHONY: all clean
------------------------------------- >8 --------------------------------=
-----
When I try to use it:
------------------------------------- 8< --------------------------------=
-----
zoso@velutha:~/tmp/swig$ ruby -rerrortest -e "puts Errortest::error('some=
error')"
ruby:
zoso@velutha:~/tmp/swig$
------------------------------------- >8 --------------------------------=
-----
When I rename the "error" function to "error2", it works great :-?
Also, if I remove the extern "C" declarations (from both errortest.{h,=
i}, I
don't know if it makes sense leaving it in errortest.i) but leave the fun=
ction
as "error", it works, too (note that I tried both with C and C++, hence t=
he
errortest.c{,xx} file and the $(CPP) variable in the Makefile).
Can anyone shed some light on this?
--=20
Esteban Manchado Vel=E1zquez <[email protected]> - http://www.foton.es
EuropeSwPatentFree - http://EuropeSwPatentFree.hispalinux.es
I'm doing some experiments with SWIG (trying to make a Ruby interface =
for
MuSE low-level sound classes), and I'm having some problems.
One of them is really weird: it seems that there are problems with ext=
ern
"C" declarations (from C++) and with functions _named_ "error" (namely, t=
hat
when you try to call the function, the interpreter aborts, without except=
ions
or anything).
Here is my tiny example:
-- errortest.c / errortest.cxx ------ 8< --------------------------------=
-----
#include <stdio.h>
void error(const char *fmt, ...)
{
printf("I won't be called\n");
}
------------------------------------- >8 --------------------------------=
-----
-- errortest.i ---------------------- 8< --------------------------------=
-----
%module errortest
%{
#include "errortest.h"
%}
#ifdef __cplusplus
extern "C" {
#endif
void error(const char *format, ...);
#ifdef __cplusplus
}
#endif
------------------------------------- >8 --------------------------------=
-----
-- errortest.h ---------------------- 8< --------------------------------=
-----
#ifdef __cplusplus
extern "C" {
#endif
void error(const char *format, ...);
#ifdef __cplusplus
}
#endif
------------------------------------- >8 --------------------------------=
-----
-- Makefile ------------------------- 8< --------------------------------=
-----
BASENAME =3D errortest
WRAPPER_BASENAME =3D $(BASENAME)_wrap
SWIG =3D swig
INTERFACE =3D $(BASENAME).i
CXXSRCS =3D errortest.cpp
SRCDIR =3D .
OBJS =3D $(CXXSRCS:.cpp=3D.o)
INCLUDES =3D -I.
LINK =3D=20
CPP =3D 1
GCC =3D $(if $(CPP),g++,gcc)
SWIG_OPTS =3D $(if $(CPP),-c++,)
SRC_EXT =3D $(if $(CPP),cxx,c)
all: $(BASENAME).so
$(BASENAME).so: $(BASENAME).o $(WRAPPER_BASENAME).o
$(GCC) -shared $(OBJS) $(WRAPPER_BASENAME).o $(LINK) -o $@
$(BASENAME).o: $(SRCDIR)/$(BASENAME).$(SRC_EXT)
$(GCC) -fpic -fPIC -DHAVE_CONFIG_H -c -shared $(INCLUDES) $<
$(WRAPPER_BASENAME).o: $(WRAPPER_BASENAME).$(SRC_EXT)
$(GCC) -fpic -fPIC -DHAVE_CONFIG_H -c -shared $(INCLUDES) -I/usr/lib/rub=
y/1.8/i386-linux $(WRAPPER_BASENAME).$(SRC_EXT)
$(WRAPPER_BASENAME).$(SRC_EXT): $(INTERFACE)
$(SWIG) $(SWIG_OPTS) -ruby $(INTERFACE)
clean:
$(RM) $(BASENAME).so $(BASENAME).o $(WRAPPER_BASENAME).o $(WRAPPER_BASEN=
AME).$(SRC_EXT)
PHONY: all clean
------------------------------------- >8 --------------------------------=
-----
When I try to use it:
------------------------------------- 8< --------------------------------=
-----
zoso@velutha:~/tmp/swig$ ruby -rerrortest -e "puts Errortest::error('some=
error')"
ruby:
zoso@velutha:~/tmp/swig$
------------------------------------- >8 --------------------------------=
-----
When I rename the "error" function to "error2", it works great :-?
Also, if I remove the extern "C" declarations (from both errortest.{h,=
i}, I
don't know if it makes sense leaving it in errortest.i) but leave the fun=
ction
as "error", it works, too (note that I tried both with C and C++, hence t=
he
errortest.c{,xx} file and the $(CPP) variable in the Makefile).
Can anyone shed some light on this?
--=20
Esteban Manchado Vel=E1zquez <[email protected]> - http://www.foton.es
EuropeSwPatentFree - http://EuropeSwPatentFree.hispalinux.es