相关疑难解决方法(0)

Julia - C界面与非基本类型

我正在扩展使用C库的Julia包.我需要从Julia调用一些C函数.他们看起来像这样:

struct contained {
    int x;
    int y;
    int z;
};
struct mystruct {
    int n;
    contained* arr;
};
mystruct* mk_mystruct(int n, contained* arr);
void use_mystruct(mystruct* foo);
Run Code Online (Sandbox Code Playgroud)

我还在Julia中声明了相应的类型:

type contained
    x::Int64
    y::Int64
    z::Int64
end
type mystruct
    n::Int64
    arr::Array{contained, 1}
end
Run Code Online (Sandbox Code Playgroud)

ccall内搭一件功能contained*作为参数,一切正常治疗contained*Ptr{Int64}:

con = fill(0, 5, 3);
mys = ccall((:mk_mystruct, "mylib"), Ptr{mystruct}, (Int64, Ptr{Int64}), n, con)
Run Code Online (Sandbox Code Playgroud)

我认为这是有效的,因为它contained具有与Int64数组相同的内存布局.这也是Julia包中其他地方的完成方式.但我知道检查返回值的唯一方法mystruct是取消引用它unsafe_load,此时Julia从段错误中崩溃.在Julia中取消引用指针的正确方法是什么?

C库还包括漂亮的打印功能,因此我可以将指针视为不透明,而不是在Julia中取消引用指针,而是将其传递回此C函数:

void print_mystruct(mystruct* foo, FILE* outputfile)
Run Code Online (Sandbox Code Playgroud)

在C代码中,调用它outputfile=stdout.我该如何设置ccall …

c julia

5
推荐指数
1
解决办法
313
查看次数

标签 统计

c ×1

julia ×1