我只想测试网页中的回调函数示例。
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执行的Foo.java
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 我为什么要得到它?
发布于 2016-04-30 08:36:47
您会得到错误,因为库放在命令行的对象文件之前。
示例中的g++命令可能在某个地方工作,但在gcc/linux链接器中却不起作用。问题在于命令中的参数顺序。如果你跑
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它将成功编译和链接。
https://stackoverflow.com/questions/36928089
复制相似问题