# Wrapping a recursive struct with Clang.jl

**URL:** <https://discourse.julialang.org/t/wrapping-a-recursive-struct-with-clang-jl/40680>\
**Category:** General Usage\
**Tags:** question, c, clang\
**Created:** [June 3, 2020, 3:22pm UTC](https://discourse.julialang.org/t/wrapping-a-recursive-struct-with-clang-jl/40680 "2020-06-03T15:22:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![KajWiik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kajwiik/32/199_2.png) [@KajWiik](https://discourse.julialang.org/u/KajWiik)\
**Post date:** [June 3, 2020, 3:22pm UTC](https://discourse.julialang.org/t/wrapping-a-recursive-struct-with-clang-jl/40680/1 "2020-06-03T15:22:49Z")

</div>

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

```nohighlight
struct B;

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

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

```

produces wrapper like this

```julia
# 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

```julia
ERROR: LoadError: UndefVarError: B not defined

```

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

```julia
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?

---

<div class="post-metadata">

**Author:** ![Gnimuc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnimuc/32/2194_2.png) [@Gnimuc](https://discourse.julialang.org/u/Gnimuc)\
**Post date:** [June 3, 2020, 3:38pm UTC](https://discourse.julialang.org/t/wrapping-a-recursive-struct-with-clang-jl/40680/2 "2020-06-03T15:38:59Z")

</div>

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