Skip to content

C++: generated _cpp.h includes exports-*.h before the records they use #1658

Description

@MendyBerger

A world that exports a resource taking a record generates a _cpp.h that doesn't compile. The user-class header is #included above the record definitions it references.

Example

Given the following wit:

package foo:bar;
interface my-interface {
    record options {
        name: string
    }
    resource my-resource {
        constructor(o: options);
    }
}
world my-world { export my-interface; }

Generate wit bindings:

wit-bindgen cpp wit --world my-world 

Run the output through the compiler

$WASI_SDK_PATH/bin/clang++ --target=wasm32-wasip2 -std=c++23 -fsyntax-only my_world.cpp

Gives the following error:

In file included from my_world.cpp:11:
In file included from ./my_world_cpp.h:4:
./exports-foo-bar-my_interface-MyResource.h:18:36: error: no type named 'Options' in namespace
      'exports::foo::bar::my_interface'
   18 |           MyResource(my_interface::Options o);
      |                      ~~~~~~~~~~~~~~^
./exports-foo-bar-my_interface-MyResource.h:19:42: error: no type named 'Options' in namespace
      'exports::foo::bar::my_interface'
   19 |           static Owned New(my_interface::Options o){return Owned(new MyResource(std::move(o)));}
      |                            ~~~~~~~~~~~~~~^
2 errors generated.

Tested with wit-bindgen v0.59.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    gen-cppRelated to the C++ code generator

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions