简体   繁体   English

在Eclipse for Java中使用静态C ++库

[英]Use static C++ library in Eclipse for Java

I would like to use an existing static C++ library in my Java program. 我想在我的Java程序中使用现有的静态C ++库。 I read that it should be possible . 我读到它应该是可能的 And the steps would be (nearly) the same as for linking dynamic libraries . 这些步骤(几乎)与链接动态库的步骤相同。 Unfortunately if I try to load the library with: 不幸的是,如果我尝试加载库:

static { 
    System.loadLibrary("mylibrary");    
}

I get an exception: 我得到一个例外:

Exception in thread "main" java.lang.UnsatisfiedLinkError: no mylibrary in java.library.path

I tried setting the java.library.path before the System.loadLibrary call manually, but it does not help: 我尝试在手动调用System.loadLibrary之前设置java.library.path,但它没有帮助:

System.setProperty("java.library.path", "/workspace/LibraryTest/lib/");

Setting LD_LIBRARY_PATH in run configurations for the project does also not fix the problem. 在项目的运行配置中设置LD_LIBRARY_PATH也无法解决问题。 I get the same error. 我犯了同样的错误。 For shared libraries it works fine though. 对于共享库,它工作正常。 Currently I'm using Oracle Java8 in Eclipse Mars on Debian Jessie. 目前我在Debian Jessie的Eclipse Mars中使用Oracle Java8。 Does the library need to hava a JNI_OnLoad_mylibrary method? 图书馆是否需要采用JNI_OnLoad_mylibrary方法? Anyone experienced similar problems? 有人遇到类似问题吗?

EDIT 编辑

tried /usr/lib directory and added JNI_OnLoad_mylibrary . 尝试/ usr / lib目录并添加了JNI_OnLoad_mylibrary No success. 没有成功。

Perhaps your code isn't relocatable? 也许你的代码不可重定位? That seems like it might upset the JVM. 这似乎可能会破坏JVM。 Recompile with -fPIC and see what happens. -fPIC编译,看看会发生什么。

From the answer to the other question, you need JNI_OnLoad_name-of-your-library to be defined in the binary for this to work. 从答案到另一个问题,您需要在二进制文件中定义JNI_OnLoad_name-of-your-library才能使其正常工作。 Did C++ name mangling ruin your day? C ++名称毁了你的一天吗? You'll probably need to extern "C" that function for it to be findable to the JVM. 您可能需要extern "C" ,它可以让JVM找到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM