Wrapping a recursive struct with Clang.jl

I am trying to wrap a C library containing several recursive structs like this:

struct B;

typedef struct A {
  int foo;
  struct B *b;
} A;

typedef struct B {
  int faa;
  A *a;
} B;

produces wrapper like this

# Automatically generated using Clang.jl

struct A
    foo::Cint
    b::Ptr{B}
end

struct B
    faa::Cint
    a::Ptr{A}
end

which (of course) gives an error message like

ERROR: LoadError: UndefVarError: B not defined

Here’s a short test setup for Clang.jl:

using Clang

const HEADERS = ["recursive.h"]

wc = init(; headers = HEADERS,
          output_file = joinpath(@__DIR__, "test_api.jl"),
          common_file = joinpath(@__DIR__, "test_common.jl"),
          clang_includes = [CLANG_INCLUDE],
          clang_args = [""],
          header_wrapped = (root, current)->root == current,
          header_library = x->"libtest",
          clang_diagnostics = true,
          )
run(wc)

Is there any (easy) way to get these structs working?

Julia doesn’t support forward declaration. A workaround is to change one of the Ptr{} to Ptr{Cvoid} and do some explicit type conversion.

1 Like