views:

79

answers:

3

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?

A: 

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.

Mark E
I did some more research and I think anonymous unions like the way I'm doing it (not like the way your link talks about) are C++ standard not C. I was originally using the -nocpp option and I tried taking that out but it still won't generate the Union class unless I give a name to the union. The link you gave still gives a name to the union i.e. "U" but mine does not.I may end up having to just edit the header file each time I need to generate the java code like you suggested, but this is not ideal.I'm using JNAerator 0.9.3 by the way.
Nick
+1  A: 

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.

Nick
+1  A: 

Hi,

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)