包装 C 头文件以将其与 dll 和 JNA(erator) 一起使用

发布于 2024-11-10 09:40:31 字数 1467 浏览 0 评论 0原文

我有一个我需要访问的某个库的 C 头文件。因此,在我读到人们最近推荐它之后,我使用 JNAerator 来完成无聊的代码转换。从我所看到的看来似乎相当可靠:

public class Z3_apiLibrary implements Library {
public static final String JNA_LIBRARY_NAME = LibraryExtractor.getLibraryPath("z3_api", true, z3_api.Z3_apiLibrary.class);

    public static final NativeLibrary JNA_NATIVE_LIB = NativeLibrary.getInstance(JNA_LIBRARY_NAME, com.ochafik.lang.jnaerator.runtime.MangledFunctionMapper.DEFAULT_OPTIONS);

static {
    Native.register(JNA_LIBRARY_NAME);
    }
public static interface Z3_lbool {
public static final int Z3_L_FALSE = -1;
public static final int Z3_L_UNDEF = 0;
public static final int Z3_L_TRUE = 1;
};

public static interface Z3_symbol_kind {
public static final int Z3_INT_SYMBOL = 0;
public static final int Z3_STRING_SYMBOL = 1;
};

完整的代码位于我的 GitHub

现在我想将 dll 实例化为对象,并从我编写的接口中将标头信息作为包装器传递:

public class z3_Solver {

   public static void main(String[] args) {
    Z3_apiLibrary solver = (Z3_apiLibrary) Native.loadLibrary("z3", Z3_apiLibrary.class);
    Z3_apiLibrary config = new Z3_apiLibrary.Z3_config(); // will not work!


   }

令我惊讶的是,这不起作用。 .Z3_config() 是抽象的。 mk_config 是静态的和本机的。所以我也无法解决这个问题......实际上我认为需要将路径传递给 Native.loadLibrary 函数作为参数来定位 dll。我将 dll 放在与 Java 类相同的路径中。这令人困惑,我怀疑也是错误的。

那么实例化 JNAerator 生成的接口的正确方法是什么?

I have a C header to a certain library I need to access. So I used JNAerator to do the boring transitions of code - after I read that people recommend it these days. Seems to be quite solid from what I see:

public class Z3_apiLibrary implements Library {
public static final String JNA_LIBRARY_NAME = LibraryExtractor.getLibraryPath("z3_api", true, z3_api.Z3_apiLibrary.class);

    public static final NativeLibrary JNA_NATIVE_LIB = NativeLibrary.getInstance(JNA_LIBRARY_NAME, com.ochafik.lang.jnaerator.runtime.MangledFunctionMapper.DEFAULT_OPTIONS);

static {
    Native.register(JNA_LIBRARY_NAME);
    }
public static interface Z3_lbool {
public static final int Z3_L_FALSE = -1;
public static final int Z3_L_UNDEF = 0;
public static final int Z3_L_TRUE = 1;
};

public static interface Z3_symbol_kind {
public static final int Z3_INT_SYMBOL = 0;
public static final int Z3_STRING_SYMBOL = 1;
};

The complete code is at my GitHub.

Now I want to instantiate the dll as an object, and pass the header information from my written interface as a wrapper:

public class z3_Solver {

   public static void main(String[] args) {
    Z3_apiLibrary solver = (Z3_apiLibrary) Native.loadLibrary("z3", Z3_apiLibrary.class);
    Z3_apiLibrary config = new Z3_apiLibrary.Z3_config(); // will not work!


   }

To my surprise this isn't working. .Z3_config() is abstract. mk_config is static and native. So I cannot resolve that either.... Actually I thought to need to pass the Path to the Native.loadLibrary function as a parameter to locate the dll. I put the dll in the same path as the Java class. Which is confusing and I suspect also wrong.

So what's the right way to instantiate that JNAerator generated interface?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半窗疏影 2024-11-17 09:40:31

您发布的标题没有定义 Z3_config 是什么,它只说 DEFINE_TYPE(Z3_config);此标头没有足够的信息来生成有效的绑定。清理标头,删除所有 #ifdef 等,包括这些类型实际应该是什么,然后再次尝试生成代码。

The header you posted does not define what Z3_config is, it only says DEFINE_TYPE(Z3_config); This header does not have enough information to generate valid binding. Clean up the header, remove all #ifdef etc, include what those types actually should be, and then try generating code again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文