From: William S F. <ws...@fu...> - 2009-04-20 19:47:49
|
Fabian Bernhard wrote: > Dear list > > I'm new to Swig and have limited knowledge of it. > > I have to implement an observable pattern. The observer is a Java class > and the notifier is implemented in C++. I can basically break the > problem down to the callback example provided with the Swig source code [0]. > > Unfortunately even the simple example does not work for me. I can > generate and compile both the C++ and Java code but, when I run the Java > application: > > (1) The C++ Callback class is always equals to 0 and thus not called by > the Caller class > (2) The Java Callback class is never passed down to the C++ Caller class > > As a result, the line in example.h > > if (_callback) _callback->run(); > > is never executed (because _callback == false). > > I must assume that it has either something to do with my machine > (GNU/Linux system with g++ 4.3.2 compiler) or something I missed due to > my lack of knowledge about Swig, i.e. missing compile flags. > > How can I make the example work? > Not sure what you are doing wrong. Below is a compile and run session that works. Hope that helps. William william@caracal:~/swig/trunk/Examples/java/callback$ g++ --version g++ (Ubuntu 4.3.2-1ubuntu12) 4.3.2 Copyright (C) 2008 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. william@caracal:~/swig/trunk/Examples/java/callback$ java -version java version "1.6.0_10" Java(TM) SE Runtime Environment (build 1.6.0_10-b33) Java HotSpot(TM) Server VM (build 11.0-b15, mixed mode) william@caracal:~/swig/trunk/Examples/java/callback$ make clean all && java runme make -f ../../Makefile java_clean make[1]: Entering directory `/home/william/swig/trunk/Examples/java/callback' rm -f *_wrap* *~ .~* *.class `find . -name \*.java | grep -v runme.java` rm -f core rm -f *.o *.so make[1]: Leaving directory `/home/william/swig/trunk/Examples/java/callback' make -f ../../Makefile CXXSRCS='example.cxx' SWIG='../../../preinst-swig' \ SWIGOPT='' TARGET='example' INTERFACE='example.i' java_cpp make[1]: Entering directory `/home/william/swig/trunk/Examples/java/callback' ../../../preinst-swig -java -c++ example.i g++ -c -fpic example.cxx example_wrap.cxx -I"/usr/lib/jvm/java-6-sun-1.6.0.10/include" -I"/usr/lib/jvm/java-6-sun-1.6.0.10/include/linux" g++ -shared example.o example_wrap.o -o libexample.so make[1]: Leaving directory `/home/william/swig/trunk/Examples/java/callback' javac *.java Adding and calling a normal C++ callback ---------------------------------------- Callback::run() Callback::~Callback() Adding and calling a Java callback ------------------------------------ JavaCallback.run() Callback::~Callback() java exit |