C-structures return by Julia value

I am trying to pass a C struct to Julia using ccall

Here is my file in C:

 #include <stdio.h> typedef struct { float a; float b; } TestStruct; TestStruct getStruct() { TestStruct s = {3.0f, 5.0f}; printf("Created struct a: %fb: %f\n", sa, sb); return s; } 

Then I compile this into a shared library for use with Julia.

Here is my Julia file:

 immutable TestStruct a::Cfloat b::Cfloat end struct = ccall((:getStruct, "libteststruct"), TestStruct, ()) println("Got struct a: ", struct.a, " b: ", struct.b) 

When I ran this file, I would expect to get

 Created struct a: 3.000000 b: 5.000000 Got struct a: 3.0 b: 5.0 

However, instead I get

 Created struct a: 3.000000 b: 5.000000 Got struct a: 3.0 b: 0.0 

a always correct, but b always 0 .

This works when I use doubles in a struct instead of a float, but I need to use a float.

Thanks.

+6
source share
2 answers

If you are on Julia v0.3.x, ccall does not handle return structures through the calling convention correctly. You can try changing the use of ccall for this:

 struct_buffer = Array(TestStruct) ccall((:getStruct, "libteststruct"), Void, (Ptr{TestStruct},), struct_buffer) struct = struct_buffer[] 

This problem can be fixed on Julia master (0.4-dev), so you can also try this and see how it happens.

+4
source

This works fine for me on Julia master (0.4-dev) - on Windows to boot. Full support for the data structure has only recently been combined with the wizard . It may seem that (view) works at 0.3, but is not officially supported and should probably be a mistake.

+5
source

Source: https://habr.com/ru/post/984516/


All Articles