We need to map C++ classes as static Java classes that extend
com.googlecode.javacpp.Pointer inside a top level class. Check this file
for simple examples of that:
http://code.google.com/p/javacv/source/browse/trunk/javacv/src/com/googlecode/javacv/cpp/ARToolKitPlus.java
And we need to declare allocate() methods that are mapped to the C++
constructors...
Let me know if something isn't clear, thanks
Samuel
On 2011-08-26 20:00, Jotschi wrote:
> Hi,
>
> i'm trying to get a very basic example up and running. But i have some
> trouble with compiling the jni sources.
>
> jotschi@Amilo:~/workspace/JavaCPPTest$ java -jar libs/javacpp.jar -
> properties linux-x86 -classpath bin -Dcompiler.includepath=/home/
> jotschi/workspace/JavaCPPTest/jni/ NativeCube
> Generating source file: /home/jotschi/workspace/JavaCPPTest/bin/
> jniNativeCube.cpp
> Building library file: /home/jotschi/workspace/JavaCPPTest/bin/linux-
> x86/libjniNativeCube.so
> g++ -I/usr/lib/jvm/java-6-sun-1.6.0.26/include -I/usr/lib/jvm/java-6-
> sun-1.6.0.26/include/linux -I/home/jotschi/workspace/JavaCPPTest/jni/ /
> home/jotschi/workspace/JavaCPPTest/bin/jniNativeCube.cpp -march=i686 -
> m32 -Wall -O3 -fPIC -shared -s -o /home/jotschi/workspace/JavaCPPTest/
> bin/linux-x86/libjniNativeCube.so
> /home/jotschi/workspace/JavaCPPTest/bin/jniNativeCube.cpp: In function
> �void Java_NativeCube_Cube(JNIEnv*, _jobject*)�:
> /home/jotschi/workspace/JavaCPPTest/bin/jniNativeCube.cpp:720: error:
> expected primary-expression before �*� token
> /home/jotschi/workspace/JavaCPPTest/bin/jniNativeCube.cpp:720: error:
> �pointer� was not declared in this scope
> /home/jotschi/workspace/JavaCPPTest/bin/jniNativeCube.cpp:720: error:
> expected primary-expression before �*� token
> /home/jotschi/workspace/JavaCPPTest/bin/jniNativeCube.cpp:720: error:
> expected primary-expression before �)� token
"native void allocate();" works just fine here.. What error do you get?
Samuel
jotschi@NeXuS:~/workspace/JavaCPPTest/jni$ echo _ZN4CubeC1Ev | c++filt
Cube::Cube()
It can't find the constructor of my Cube class. I just copied the
libCube.so into bin/linux-x86
I did a strace:
jotschi@NeXuS:~/workspace/JavaCPPTest$ strace -f java -classpath
bin:libs/javacpp.jar
-Djava.library.path=/home/jotschi/workspace/JavaCPPTest/jni/ CubeTest
It looks to me that the jvm tries to find libCube methods/class within
/tmp/libjniJavaCube7947671323921108730.so
objdump -dr /tmp/libjniJavaCube3498489508158541705.so | c++filt | less
000015ac <Cube::Cube()@plt>:
15ac: ff a3 38 00 00 00 jmp *0x38(%ebx)
15b2: 68 58 00 00 00 push $0x58
15b7: e9 30 ff ff ff jmp 14ec <_init+0x30>
I don't know what @plt stands for but the libCube constructor signature
looks like this:
00000000 <Cube::Cube()>:
0: 55 push %ebp
1: 89 e5 mov %esp,%ebp
3: 5d pop %ebp
4: c3 ret
5: 90 nop
Could that be the problem? Any ideas?
Jotschi
Strange, I get
Exception in thread "main" java.lang.NoSuchMethodError: main
which is perfectly normal given that there is no main() method...
It seems to me that you are trying to link the library using a C
compiler, in which case the C++ library isn't linked in by default and
you will get these unresolved symbols. Are you getting the same error
when you let JavaCPP compile/link the library for you? If so, what
platform and compiler?
Samuel
Ah, I see what you are missing. Your constructor is defined in Cube.cpp,
so you either need to add this source file to the @Platform(include=...)
annotation, *or* you compile it yourself, and add it to the
@Platform(link=...) annotation, of the top level class.
Samuel
BTW, I like to use static nested classes and such because it makes the
source files more compact and similar looking to a C++ include file, but
we can also make the top level class extend Pointer directly, e.g.:
http://code.google.com/p/javacpp/source/browse/trunk/javacpp/src/com/googlecode/javacpp/SizeTPointer.java
> If you take a look at JavaCube.java you see that i use JavaCube as a wrapper for the NativeCube static class. Is that the supposed way to work with the static/native class?
If it compiles without complaining, then it's probably correct enough :)
Samuel
On 2011-08-28 23:06, Johannes Sch�th wrote:
> Of course!
>
> Did not thought about that :)
>
> Now it works. I created a bundle with all sources which runs out of the
> box on linux x86
>
> https://github.com/Jotschi/javacpp-examples
>
> Jotschi
>
> 2011/8/28 Samuel Audet <samuel...@gmail.com
> <mailto:samuel...@gmail.com>>