JNAerator 结构中缺少未命名联合

发布于 2024-08-25 22:21:37 字数 317 浏览 3 评论 0原文

我试图让 JNAerator 从 C 共享库生成一些 JNA 支持的 Java 代码,除了无法生成嵌套在结构内的未命名联合之外,一切都很好。

示例:

typedef struct MY_STRUCTURE {
  union {
    My_Type1 var1;
    My_Type2 var2;
  };

}MY_STRUCTURE;

如果我更改标头以使联合具有名称,它将起作用。但出于显而易见的原因,我不能只更改标头而不破坏我尝试使用的共享库。

除了将头文件和共享库更改为命名联合之外,还有其他解决方案吗?

I'm trying to get JNAerator to generate some JNA backed Java code from a C shared library and everything is fine except that it failed to generate an unnamed union nested inside a structure.

Example:

typedef struct MY_STRUCTURE {
  union {
    My_Type1 var1;
    My_Type2 var2;
  };

}MY_STRUCTURE;

If I change the header to make the union have a name it will work. But for obvious reasons I can't just change the header without breaking the shared library I'm trying to use.

Any solutions other than changing the header file and shared library to named union?

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

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

发布评论

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

评论(3

微凉 2024-09-01 22:21:38

经过更多研究,我确定我的问题是未命名联合的问题,而不是匿名联合的问题。 JNAerator 声称支持匿名联合,但我没有在未命名联合上找到任何内容。根据我的经验,我会说它不支持未命名的工会。

旁注:标准 C 不支持未命名联合。一些编译器支持,但不是大多数。它是 C++ 中的标准。

匿名联合:

typedef struct MY_STRUCTURE {
  int i;
  char c; 
  union { 
    My_Type1 var1; 
    My_Type2 var2; 
  }UnionName; 

}MY_STRUCTURE;

匿名和未命名联合:

typedef struct MY_STRUCTURE { 
  int i;
  char c;
  union { 
    My_Type1 var1; 
    My_Type2 var2; 
  }; 

}MY_STRUCTURE;

结论: 标记解决方案

将标头中的未命名联合更改为命名联合,然后 JNAerate Java 代码,然后将标头更改回原来的样子。就像马克说的,它不会改变内存布局,所以你可以改变名称。

After more research I determined that my problem is a problem with unnamed unions not anonymous unions. JNAerator claims support for anonymous unions but I haven't found anything on unnamed unions. Based on my experience I would say it doesn't support unnamed unions.

Side note: Unnamed unions aren't supported in standard C. Some compilers support it but not most. It is standard in C++.

Anonymous Union:

typedef struct MY_STRUCTURE {
  int i;
  char c; 
  union { 
    My_Type1 var1; 
    My_Type2 var2; 
  }UnionName; 

}MY_STRUCTURE;

Anonymous and Unnamed Union:

typedef struct MY_STRUCTURE { 
  int i;
  char c;
  union { 
    My_Type1 var1; 
    My_Type2 var2; 
  }; 

}MY_STRUCTURE;

Conclusion: Marks solution

Change the unnamed union in the header to a named union then JNAerate the Java code and then change the header back to how it was. Like Mark said it won't change the memory layout, so you can change the name.

夏至、离别 2024-09-01 22:21:38

添加名称不会更改内存布局,因此您可以更改名称、JNAerate Java 代码(这将适当地映射内存/结构),然后撤消更改。添加名称不会影响 JNA 将结构映射到实际库的情况。

编辑:您的结果有点奇怪,因为 JNAerator 文档明确解决了 匿名类型问题,似乎表明它应该产生正确的结果。

Adding a name won't change the memory layout, so you can change the name, JNAerate the Java code (which will map the memory/structure appropriately) and then undo your change. JNA's mapping of the structure to your actual library won't be impacted by adding a name.

edit: your results are a little strange, since the JNAerator documentation clearly addresses the anonymous types problem and seems to indicate that it should produce the correct results.

虚拟世界 2024-09-01 22:21:37

这是 JNAerator 中的一个错误。
我已在 JNAerator 项目的跟踪器中输入了一个问题,因此,如果您想在错误修复后收到通知,可以为其加注星标:

http://code.google.com/p/jnaerator/issues/detail?id=60

干杯

Olivier(JNAerator 的作者)

This is a bug in JNAerator.
I've entered an issue in JNAerator project's tracker, so if you want to be notified when the bug is fixed you can star it :

http://code.google.com/p/jnaerator/issues/detail?id=60

Cheers

Olivier (author of JNAerator)

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