编译示例回调函数的 JavaCPP 问题
JavaCPP problems compiling the example Callback function
我只是想测试网页上的回调函数示例。
https://github.com/bytedeco/javacpp#creating-callback-functions
在文件中 foo.cpp
#include <iostream>
#include "jniFoo.h"
int main() {
JavaCPP_init(0, NULL);
try {
foo(6, 7);
} catch (std::exception &e) {
std::cout << e.what() << std::endl;
}
JavaCPP_uninit();
}
Foo.java 函数 foo 执行的地方
import org.bytedeco.javacpp.*;
import org.bytedeco.javacpp.annotation.*;
@Platform(include="<algorithm>")
@Namespace("std")
public class Foo {
static { Loader.load(); }
public static class Callback extends FunctionPointer {
// Loader.load() and allocate() are required only when explicitly creating an instance
static { Loader.load(); }
protected Callback() { allocate(); }
private native void allocate();
public @Name("foo") boolean call(int a, int b) throws Exception {
throw new Exception("bar " + a * b);
}
}
// We can also pass (or get) a FunctionPointer as argument to (or return value from) other functions
public static native void stable_sort(IntPointer first, IntPointer last, Callback compare);
// And to pass (or get) it as a C++ function object, annotate with @ByVal or @ByRef
public static native void sort(IntPointer first, IntPointer last, @ByVal Callback compare);
}
使用 Linux x86_64:
下的这些命令构建和 运行 此示例代码
javac -cp javacpp.jar Foo.java
java -jar javacpp.jar Foo -header
g++ -I/usr/lib/jvm/java-8-oracle/include/ -I/usr/lib/jvm/java-8-oracle/include/linux/ linux-x86_64/libjniFoo.so foo.cpp -o Foo
第三条命令出现错误:
/tmp/ccvrmILI.o: In function `main':
foo.cpp:(.text+0x14): undefined reference to `JavaCPP_init'
foo.cpp:(.text+0x1e): undefined reference to `onAddTwoInteger'
foo.cpp:(.text+0x23): undefined reference to `JavaCPP_uninit'
collect2: error: ld returned 1 exit status
为什么我会得到它?
您收到错误消息是因为在命令行中库位于目标文件之前。
示例中的 g++
命令可能在某些地方有效,但在 gcc/linux-linker 中不起作用。问题是命令中的参数顺序。如果你运行
g++ -I/usr/lib/jvm/java-8-oracle/include/ -I/usr/lib/jvm/java-8-oracle/include/linux/ foo.cpp linux-x86_64/libjniFoo.so -o Foo
它将编译并 link 成功。
我只是想测试网页上的回调函数示例。
https://github.com/bytedeco/javacpp#creating-callback-functions
在文件中 foo.cpp
#include <iostream>
#include "jniFoo.h"
int main() {
JavaCPP_init(0, NULL);
try {
foo(6, 7);
} catch (std::exception &e) {
std::cout << e.what() << std::endl;
}
JavaCPP_uninit();
}
Foo.java 函数 foo 执行的地方
import org.bytedeco.javacpp.*;
import org.bytedeco.javacpp.annotation.*;
@Platform(include="<algorithm>")
@Namespace("std")
public class Foo {
static { Loader.load(); }
public static class Callback extends FunctionPointer {
// Loader.load() and allocate() are required only when explicitly creating an instance
static { Loader.load(); }
protected Callback() { allocate(); }
private native void allocate();
public @Name("foo") boolean call(int a, int b) throws Exception {
throw new Exception("bar " + a * b);
}
}
// We can also pass (or get) a FunctionPointer as argument to (or return value from) other functions
public static native void stable_sort(IntPointer first, IntPointer last, Callback compare);
// And to pass (or get) it as a C++ function object, annotate with @ByVal or @ByRef
public static native void sort(IntPointer first, IntPointer last, @ByVal Callback compare);
}
使用 Linux x86_64:
下的这些命令构建和 运行 此示例代码javac -cp javacpp.jar Foo.java
java -jar javacpp.jar Foo -header
g++ -I/usr/lib/jvm/java-8-oracle/include/ -I/usr/lib/jvm/java-8-oracle/include/linux/ linux-x86_64/libjniFoo.so foo.cpp -o Foo
第三条命令出现错误:
/tmp/ccvrmILI.o: In function `main':
foo.cpp:(.text+0x14): undefined reference to `JavaCPP_init'
foo.cpp:(.text+0x1e): undefined reference to `onAddTwoInteger'
foo.cpp:(.text+0x23): undefined reference to `JavaCPP_uninit'
collect2: error: ld returned 1 exit status
为什么我会得到它?
您收到错误消息是因为在命令行中库位于目标文件之前。
示例中的 g++
命令可能在某些地方有效,但在 gcc/linux-linker 中不起作用。问题是命令中的参数顺序。如果你运行
g++ -I/usr/lib/jvm/java-8-oracle/include/ -I/usr/lib/jvm/java-8-oracle/include/linux/ foo.cpp linux-x86_64/libjniFoo.so -o Foo
它将编译并 link 成功。